-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.py
325 lines (267 loc) · 9.22 KB
/
httpd.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/python
import BaseHTTPServer
import SimpleHTTPServer
import calendar
import cgi
import composer
import digestauth
import fs
import gc
import json
import params
import rest
import threading
import time
# For HTTPS
import os
import socket
import SocketServer
from SocketServer import BaseServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from OpenSSL import SSL
DEBUG = 4
class HttpHandler(SimpleHTTPServer.SimpleHTTPRequestHandler, object):
def get_output_format(self, qs):
ret = "HTML"
# TODO: deprecate output_format
if qs and "output_format" in qs:
of = qs["output_format"][0]
elif qs and "output" in qs:
of = qs["output"][0]
else:
of = self.headers.getheader("Accept")
if of == "application/json" or of == "json":
ret = "JSON"
else:
ret = "HTML"
return ret;
def output_auth_headers(self, status_code, headers):
if DEBUG >=5 :
print('STATUS_CODE={}'.format(status_code))
self.send_response(status_code)
for (key, value) in headers:
if DEBUG >=5 :
print('HEADER: {}={}'.format(key, value))
self.send_header(key, value)
self.end_headers()
def authorized(self, rest, db, http_method):
uri = self.path
auth_header = self.headers.getheader("Authorization")
auth_header = auth_header if auth_header else ""
da = digestauth.DigestAuth(params.HTTP_REALM, {})
# First check if the user is authoroized to access the path.
da.parseHeader(auth_header)
if "username" in da.params:
user = da.params["username"]
else:
user = None
if DEBUG >= 5:
print "'username' in auth_header: %s" % user
if not rest.can_access(user, self.path):
if DEBUG >= 5:
print "The user '%s' cannot access the path '%s'." % (user, self.path)
status_code, headers, user_r = da.authenticate(http_method, uri, auth_header)
self.output_auth_headers(status_code, headers)
return False
if user is None:
return True
password = db.get_password(self.path + "/*")
if password == "":
if DEBUG >= 5: print "No password is required for path %s" % self.path
return True
password = db.get_password(user)
# Then, authenticate this user.
da.setUsers({user: password})
status_code, headers, user_r = da.authenticate(http_method, uri, auth_header)
if status_code == 200:
if DEBUG >= 5:
print "User '%s' is authenticated as '%s'" % (user, user_r)
assert user_r == user
return True
# Not authenticated, challenge again
if DEBUG >= 5:
print "The user '%s' is not authenticated. status_code=%d." % \
(user, status_code)
self.output_auth_headers(status_code, headers)
return False
def handle_request(self, http_method):
# Short-cut for OPTIONS method
if http_method is 'OPTIONS':
allow = 'GET, PUT, UPDATE, DELETE, OPTIONS'
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*') # for CORS
self.send_header('Access-Control-Allow-Headers', '*') # for CORS
self.send_header('Access-Control-Allow-Methods', allow)
self.send_header('Allow', allow)
self.end_headers()
return
db = fs.FileSystem(params.HTTP_ROOT)
r = rest.Rest(db)
if not self.authorized(r, db, http_method):
if DEBUG >= 5:
print "self.authorized() == False"
return
# Parse query string in the URI.
a = self.path.split("?", 1)
path = a[0]
if len(a) >= 2:
qs = cgi.parse_qs(a[1])
else:
qs = None
# Parse POST, PUT, UPDATE, DELETE data
content_type = self.headers.getheader('content-type')
if content_type:
if DEBUG >= 5:
print('content_type={}'.format(content_type))
ctype, pdict = cgi.parse_header(content_type)
if ctype == 'multipart/form-data':
if DEBUG >= 5:
print('multipart/form-data')
postvars = cgi.parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers.getheader('content-length'))
postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values = 1)
if DEBUG >= 5:
print('application/x-www-form-urlencoded.length={}'.format(length))
else:
postvars = {}
else:
postvars = {}
if qs is None:
qs = postvars
else:
qs = dict(postvars.items() + qs.items())
if "longpoll" in qs:
longpoll = float(qs["longpoll"][0])
else:
longpoll = 0
if_modified_since = self.headers.getheader('If-Modified-Since')
if if_modified_since:
if_modified_since = calendar.timegm(
time.strptime(if_modified_since,
"%a, %d %b %Y %H:%M:%S GMT"))
else:
# if_modified_since is not specified, set to current time if
# longpoll is specified.
if longpoll:
if_modified_since = time.time()
while True:
# prepare Last-Modified data if the target exists.
if db.path_type(path.split("/")) == db.NOT_EXIST:
last_modified = None
else:
last_modified = db.get_last_modified(path.split("/"))
if longpoll and last_modified is None:
# If this is longpoll and the file is not existed yet, pass through
# for next iteration.
pass
elif if_modified_since and int(if_modified_since) >= int(last_modified):
status_code = 304
response_data = ""
else:
#
# Call REST handler !
(status_code, response_data) = r.handle(http_method, path, qs)
break
if longpoll <= 0:
break
time.sleep(1)
longpoll = longpoll - 1
output_format = self.get_output_format(qs)
c = composer.Composer(output_format, db)
(content_type, output) = c.compose(response_data, path)
self.send_response(status_code)
self.send_header("Content-type", content_type)
self.send_header("Access-Control-Allow-Origin", "*") # for CORS
if last_modified is not None:
self.send_header("Last-Modified",
time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(last_modified)))
self.end_headers()
self.wfile.write(output)
def do_GET(self):
self.handle_request("GET")
def do_POST(self):
self.handle_request("POST")
def do_PUT(self):
self.handle_request("PUT")
def do_UPDATE(self):
self.handle_request("UPDATE")
def do_DELETE(self):
self.handle_request("DELETE")
def do_OPTIONS(self):
self.handle_request("OPTIONS")
class SecureHTTPServer(BaseHTTPServer.HTTPServer):
"""
Kudos http://code.activestate.com/recipes/442473-simple-http-server-supporting-ssl-secure-communica/
openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
-newkey rsa:2048
BUG:
curl https://localhost:3443 --insecure: NO error
firefox 16.0.2:
File "/usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 851, in _raise_ssl_error
raise ZeroReturnError()
chrome 34.0.1847.131 m:
File "/usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 864, in _raise_ssl_error
raise SysCallError(-1, "Unexpected EOF")
"""
def __init__(self, server_address, HandlerClass):
BaseServer.__init__(self, server_address, HandlerClass)
ctx = SSL.Context(SSL.TLS_METHOD)
#server.pem's location (containing the server private key and
#the server certificate).
fpem = 'server.pem'
ctx.use_privatekey_file(fpem)
ctx.use_certificate_file(fpem)
self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
self.socket_type))
self.server_bind()
self.server_activate()
def shutdown_request(self,request):
request.shutdown()
class SecureHTTPRequestHandler(HttpHandler):
def setup(self):
self.connection = self.request
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
class ThreadingHTTP(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
class ThreadingHTTPS(SocketServer.ThreadingMixIn, SecureHTTPServer):
pass
def run_server(https, port,
ServerClass = None, HandlerClass = None, child = None):
server_address = ('', port)
if not https:
protocol = "HTTP"
if ServerClass is None:
ServerClass = ThreadingHTTP
if HandlerClass is None:
HandlerClass = HttpHandler
else:
protocol = "HTTPS"
if ServerClass is None:
ServerClass = ThreadingHTTPS
if HandlerClass is None:
HandlerClass = SecureHTTPRequestHandler
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
if DEBUG >= 5:
print "Serving %s on %s port %s ..." % (protocol, sa[0], sa[1])
# For unittest to update the server is up.
if child:
child.server = httpd
child.server.started = True
httpd.serve_forever()
def main():
def run_http():
run_server(False, params.HTTP_PORT)
def run_https():
run_server(True, params.HTTPS_PORT)
threading.Thread(target = run_http).start()
threading.Thread(target = run_https).start()
while True:
print("Garbage collecting: START ...")
gc.collect()
print("Garbage collecting: DONE.")
time.sleep(60)
if __name__ == '__main__':
main()