-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
225 lines (193 loc) · 7.59 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
Server for hosting website locally
On windows, run `server.exe`.
(executable created with: `pyinstaller --onefile server.py`)
Otherwise, requires python 3.3 or greater.
Run `python3 server.py`
Visit `http://localhost:8000` in your web browser.
"""
import argparse
import http.client
import json
import os
import posixpath
import sys
import threading
import time
import urllib.parse
import urllib.request
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
DIR = os.path.abspath(os.path.dirname(__file__))
# make sure our working directory for this server is the same as this file is in
os.chdir(DIR)
with open('metadata.json') as f:
VERSION_INFO = json.load(f)
DEFAULT_SEARCH_URL = VERSION_INFO['meta']['canonical-urls']['html']
PORTAL_CLIENT_CLASS = None
PORTAL_HOST = None
SEARCH_CLIENT_CLASS = None
SEARCH_HOST = None
filetypes = {
'html',
'pdf',
'jpg',
'svg',
'png',
'gif',
'css',
'js',
'mustache',
'json',
'bulk',
'map',
'ttf',
'eot',
'woff',
'woff2',
}
HISTORICAL_VERSIONS_PATH_PREFIXES = ('/_publication', '/_date', '/_compare')
PORTAL_PATH_PREFIXES = ('/_portal', '/_api') + HISTORICAL_VERSIONS_PATH_PREFIXES
class RequestHandler(SimpleHTTPRequestHandler):
def _proxy(self, Client, host, upstream_name):
"""
proxy the current request to the given host using the given
http.client Client class. 404 if not configured to proxy
the path.
"""
if Client is None:
self.send_response(400)
self.end_headers()
message = 'Local server not configured to proxy {0}. Please run with the "--{0}-proxy-url" flag '.format(
upstream_name)
self.wfile.write(message.encode('utf-8'))
return
client = Client(host)
req_headers = {}
req_headers.update(self.headers)
req_headers.pop('Host', None)
req_headers.update({
'X-Forwarded-For': 'self.address_string()',
'X-Forwarded-Host': self.headers['Host'],
'X-Forwarded-Proto': 'http',
})
client.request('GET', self.path, headers=req_headers)
try:
resp = client.getresponse()
except:
self.send_response(500)
self.end_headers()
host = client.host
scheme = type(client).__name__[:-10].lower()
url = urllib.parse.urlunsplit((host, scheme, '', ''))
message = 'Something went wrong proxying to {}'.format(url)
self.wfile.write(message.encode('utf-8'))
else:
self.send_response(resp.code)
for k, v in resp.headers.items():
if k not in ('Transfer-Encoding', 'Connection'):
self.send_header(k, v)
self.end_headers()
self.copyfile(resp, self.wfile)
def do_GET(self):
if self.path.startswith(PORTAL_PATH_PREFIXES):
return self._proxy(PORTAL_CLIENT_CLASS, PORTAL_HOST, 'portal')
if self.path.startswith('/_search'):
return self._proxy(SEARCH_CLIENT_CLASS, SEARCH_HOST, 'search')
redirect = self.server.redirects.get(self.path)
if redirect:
sa = self.server.socket.getsockname()
location = 'http://{}:{}{}'.format(*sa, redirect)
self.send_response(302)
self.send_header('Location', location)
self.end_headers()
else:
# default to html if no valid filetype - this is not the right way to do this - it should be a retry.
if not self.path.endswith('/') and ('.' not in self.path or self.path.rsplit('.', 1)[1] not in filetypes):
self.path = self.path + '.html'
super().do_GET()
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# replace colons (not allowed in win paths) with tilde
path = path.replace(':', '~')
# abandon query parameters
path = path.split('?', 1)[0]
path = path.split('#', 1)[0]
# Don't forget explicit trailing slash when normalizing. Issue17324
trailing_slash = path.rstrip().endswith('/')
try:
path = urllib.parse.unquote(path, errors='surrogatepass')
except UnicodeDecodeError:
path = urllib.parse.unquote(path)
path = posixpath.normpath(path)
words = path.split('/')
words = filter(None, words)
path = DIR
for word in words:
if os.path.dirname(word) or word in (os.curdir, os.pardir):
# Ignore components that are not a simple file/directory name
continue
path = os.path.join(path, word)
if trailing_slash:
path += '/'
return path
def get_http_client_info(upstream_name, url):
"""
return the http.client class and host needed to reach the given url
"""
if not url:
return None, None
scheme, host, *_ = urllib.parse.urlparse(url)
if not scheme:
print('Must include scheme in {}-proxy-url (e.g. https://example.com, rather than example.com)'.format(upstream_name))
Client = getattr(http.client, scheme.upper() + 'Connection')
print('PROXYING: "/_{}" to {}'.format(upstream_name, url))
return Client, host
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--bind', '-b', default='127.0.0.1', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: localhost]')
parser.add_argument('--port', action='store',
default=8000, type=int,
nargs='?',
help='Specify alternate port [default: 8000]')
parser.add_argument('--search-proxy-url', default=DEFAULT_SEARCH_URL, metavar='SEARCH_URL',
help='url to proxy search requests to. [default: {}'.format(DEFAULT_SEARCH_URL))
parser.add_argument('--portal-proxy-url', default=None, metavar='PORTAL_URL',
help='url to proxy portal requests to. [default: None]')
parser.add_argument('--no-open-browser', default=False, action="store_true",
help='do not open the library in default browser after starting server.')
args = parser.parse_args()
server_address = (args.bind, args.port)
httpd = HTTPServer(server_address, RequestHandler)
raw_redirects = []
try:
with open(os.path.join(DIR, 'redirects.json')) as f:
raw_redirects = json.load(f)
except:
pass
redirects = {r[0]: r[1] for r in raw_redirects}
httpd.redirects = redirects
sa = httpd.socket.getsockname()
url = 'http://{}:{}'.format(sa[0], sa[1])
print("Visit {} in your webbrowser to view library...".format(url))
print("\n\n*** This server is designed for local use. Do not use in production. ***\n\n")
PORTAL_CLIENT_CLASS, PORTAL_HOST = get_http_client_info('portal', args.portal_proxy_url)
SEARCH_CLIENT_CLASS, SEARCH_HOST = get_http_client_info('search', args.search_proxy_url)
def visit_library():
time.sleep(2)
webbrowser.open(url, new=2)
if not args.no_open_browser:
thread = threading.Thread(target=visit_library)
thread.start()
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting.")
httpd.server_close()
sys.exit(0)