forked from XX-net/XX-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_http_server.py
More file actions
397 lines (340 loc) · 13.9 KB
/
Copy pathsimple_http_server.py
File metadata and controls
397 lines (340 loc) · 13.9 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import urllib.parse
import datetime
import threading
import http.client
import socket
import errno
import sys
import select
import time
import json
import xlog
logging = xlog.getLogger("simple_http_server")
class HttpServerHandler():
default_request_version = "HTTP/1.1"
rbufsize = -1
wbufsize = 0
command = ""
path = ""
def __init__(self, sock, client, args, https=False):
self.connection = sock
self.rfile = sock.makefile("rb", self.rbufsize)
self.wfile = sock.makefile("wb", self.wbufsize)
self.client_address = client
self.args = args
self.https = https
self.setup()
def setup(self):
pass
def handle(self):
#logging.info('Connected from %r', self.client_address)
while True:
try:
self.close_connection = 1
self.handle_one_request()
except Exception as e:
#logging.warn("handle err:%r close", e)
self.close_connection = 1
if self.close_connection:
break
self.connection.close()
#logging.debug("closed from %s:%d", self.client_address[0], self.client_address[1])
def address_string(self):
return '%s:%s' % self.client_address[:2]
def parse_request(self):
self.command = None # set in case of error on the first line
self.request_version = version = self.default_request_version
requestline = self.raw_requestline
requestline = requestline.rstrip('\r\n')
self.requestline = requestline
words = requestline.split()
if len(words) == 3:
command, path, version = words
if version[:5] != 'HTTP/':
self.send_error(400, "Bad request version (%r)" % version)
return False
try:
base_version_number = version.split('/', 1)[1]
version_number = base_version_number.split(".")
# RFC 2145 section 3.1 says there can be only one "." and
# - major and minor numbers MUST be treated as
# separate integers;
# - HTTP/2.4 is a lower version than HTTP/2.13, which in
# turn is lower than HTTP/12.3;
# - Leading zeros MUST be ignored by recipients.
if len(version_number) != 2:
raise ValueError
version_number = int(version_number[0]), int(version_number[1])
except (ValueError, IndexError):
self.send_error(400, "Bad request version (%r)" % version)
return False
if version_number >= (1, 1):
self.close_connection = 0
if version_number >= (2, 0):
self.send_error(505,
"Invalid HTTP Version (%s)" % base_version_number)
return False
elif len(words) == 2:
command, path = words
self.close_connection = 1
if command != 'GET':
self.send_error(400,
"Bad HTTP/0.9 request type (%r)" % command)
return False
elif not words:
return False
else:
self.send_error(400, "Bad request syntax (%r)" % requestline)
return False
self.command, self.path, self.request_version = command, path, version
# Examine the headers and look for a Connection directive
self.headers = http.client.parse_headers(self.rfile)
conntype = self.headers.get('Connection', "")
if conntype.lower() == 'close':
self.close_connection = 1
elif conntype.lower() == 'keep-alive':
self.close_connection = 0
return True
def handle_one_request(self):
try:
try:
line = self.rfile.readline(65535)
self.raw_requestline = line.decode('iso-8859-1')
except Exception as e:
#logging.exception("simple server handle except %r", e)
return
if len(self.raw_requestline) > 65536:
#logging.warn("recv command line too large")
return
if not self.raw_requestline:
#logging.warn("closed")
return
self.parse_request()
if self.command == "GET":
self.do_GET()
elif self.command == "POST":
self.do_POST()
elif self.command == "CONNECT":
self.do_CONNECT()
elif self.command == "HEAD":
self.do_HEAD()
elif self.command == "DELETE":
self.do_DELETE()
elif self.command == "OPTIONS":
self.do_OPTIONS()
elif self.command == "PUT":
self.do_PUT()
else:
logging.warn("unhandler cmd:%s path:%s from:%s", self.command, self.path, self.address_string())
return
self.wfile.flush() #actually send the response if not already done.
self.close_connection = 0
except socket.error as e:
#logging.warn("socket error:%r", e)
pass
except IOError as e:
if e.errno == errno.EPIPE:
logging.warn("PIPE error:%r", e)
pass
else:
logging.warn("IOError:%r", e)
pass
#except OpenSSL.SSL.SysCallError as e:
# logging.warn("socket error:%r", e)
except Exception as e:
logging.exception("handler:%r from:%s", e, self.address_string())
pass
def do_GET(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def do_POST(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def do_PUT(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def do_DELETE(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def do_OPTIONS(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def do_HEAD(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def do_CONNECT(self):
logging.warn("unhandler cmd:%s from:%s", self.command, self.address_string())
def send_not_found(self):
self.wfile.write(b'HTTP/1.1 404\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n404 Not Found')
def send_error(self, code, message=None):
self.wfile.write(('HTTP/1.1 %d\r\n' % code).encode())
self.wfile.write(b'Connection: close\r\n\r\n')
if message:
self.wfile.write(message.encode())
def send_response(self, mimetype="", content="", headers="", status=200):
data = []
data.append('HTTP/1.1 %d\r\n' % status)
if len(mimetype):
data.append('Content-Type: %s\r\n' % mimetype)
data.append('Content-Length: %s\r\n' % len(content.encode('utf-8')))
if len(headers):
if isinstance(headers, dict):
for key in headers:
data.append("%s: %s\r\n" % (key, headers[key]))
elif isinstance(headers, str):
data.append(headers)
data.append("\r\n")
if len(content) < 1024:
data.append(content)
data_str = "".join(data)
self.wfile.write(data_str.encode())
else:
data_str = "".join(data)
self.wfile.write(data_str.encode())
if len(content):
self.wfile.write(content.encode())
def send_file(self, filename, mimetype):
try:
if not os.path.isfile(filename):
self.send_not_found()
return
file_size = os.path.getsize(filename)
tme = (datetime.datetime.today()+datetime.timedelta(minutes=330)).strftime('%a, %d %b %Y %H:%M:%S GMT')
head = 'HTTP/1.1 200\r\nAccess-Control-Allow-Origin: *\r\nCache-Control:public, max-age=31536000\r\n'
head += 'Expires: %s\r\nContent-Type: %s\r\nContent-Length: %s\r\n\r\n' % (tme, mimetype, file_size)
self.wfile.write(head.encode())
with open(filename, 'rb') as fp:
while True:
data = fp.read(65535)
if not data:
break
self.wfile.write(data)
except:
pass
#logging.warn("download broken")
def response_json(self, res_arr):
data = json.dumps(res_arr, indent=0, sort_keys=True)
self.send_response('application/json', data)
class HTTPServer():
def __init__(self, address, handler, args=(), use_https=False, cert=""):
self.sockets = []
self.running = True
if isinstance(address, tuple):
self.server_address = [address]
else:
#server can listen multi-port
self.server_address = address
self.handler = handler
self.args = args
self.use_https = use_https
self.cert = cert
self.init_socket()
#logging.info("server %s:%d started.", address[0], address[1])
def init_socket(self):
for addr in self.server_address:
self.add_listen(addr)
def add_listen(self, addr):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind(addr)
except Exception as e:
logging.error("bind to %s:%d fail", addr[0], addr[1])
raise e
if self.use_https:
import OpenSSL
if hasattr(OpenSSL.SSL, "TLSv1_2_METHOD"):
ssl_version = OpenSSL.SSL.TLSv1_2_METHOD
elif hasattr(OpenSSL.SSL, "TLSv1_1_METHOD"):
ssl_version = OpenSSL.SSL.TLSv1_1_METHOD
elif hasattr(OpenSSL.SSL, "TLSv1_METHOD"):
ssl_version = OpenSSL.SSL.TLSv1_METHOD
ctx = OpenSSL.SSL.Context(ssl_version)
#server.pem's location (containing the server private key and the server certificate).
fpem = self.cert
ctx.use_privatekey_file(fpem)
ctx.use_certificate_file(fpem)
sock = OpenSSL.SSL.Connection(ctx, sock)
sock.listen(200)
self.sockets.append(sock)
logging.info("server %s:%d started.", addr[0], addr[1])
def serve_forever(self):
while self.running:
r, w, e = select.select(self.sockets, [], [], 3)
for rsock in r:
try:
(sock, address) = rsock.accept()
except IOError as e:
logging.warn("socket accept fail(errno: %s).", e.args[0])
if e.args[0] == 10022:
logging.info("restart socket server.")
self.server_close()
self.init_socket()
break
self.process_connect(sock, address)
def process_connect(self, sock, address):
#logging.debug("connect from %s:%d", address[0], address[1])
client_obj = self.handler(sock, address, self.args, self.use_https)
client_thread = threading.Thread(target=client_obj.handle)
client_thread.start()
def shutdown(self):
self.running = False
def server_close(self):
for sock in self.sockets:
sock.close()
self.sockets = []
class TestHttpServer(HttpServerHandler):
def __init__(self, sock, client, args):
self.data_path = args
HttpServerHandler.__init__(self, sock, client, args)
def generate_random_lowercase(self, n):
min_lc = ord(b'a')
len_lc = 26
ba = bytearray(os.urandom(n))
for i, b in enumerate(ba):
ba[i] = min_lc + b % len_lc # convert 0..255 to 97..122
#sys.stdout.buffer.write(ba)
return ba
def do_GET(self):
url_path = urllib.parse.urlparse(self.path).path
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
logging.debug("GET %s from %s:%d", self.path, self.client_address[0], self.client_address[1])
if url_path == '/':
data = "OK\r\n"
self.wfile.write('HTTP/1.1 200\r\nAccess-Control-Allow-Origin: *\r\nContent-Length: %d\r\n\r\n%s' %(len(data.encode('utf-8')), data) )
elif url_path == '/null':
mimetype = "application/x-binary"
if "size" in reqs:
file_size = int(reqs['size'][0])
else:
file_size = 1024 * 1024 * 1024
self.wfile.write('HTTP/1.1 200\r\nContent-Type: %s\r\nContent-Length: %s\r\n\r\n' % (mimetype, file_size))
start = 0
data = self.generate_random_lowercase(65535)
while start < file_size:
left = file_size - start
send_batch = min(left, 65535)
self.wfile.write(data[:send_batch])
start += send_batch
else:
target = os.path.abspath(os.path.join(self.data_path, url_path[1:]))
if os.path.isfile(target):
self.send_file(target, "application/x-binary")
else:
self.wfile.write('HTTP/1.1 404\r\nContent-Length: 0\r\n\r\n' )
def main(data_path="."):
logging.info("listen http on 8880")
httpd = HTTPServer(('', 8880), TestHttpServer, data_path)
http_thread = threading.Thread(target=httpd.serve_forever)
http_thread.setDaemon(True)
http_thread.start()
while True:
time.sleep(10)
if __name__ == "__main__":
if len(sys.argv) > 2:
data_path = sys.argv[1]
else:
data_path = "."
try:
main(data_path=data_path)
except Exception:
import traceback
traceback.print_exc(file=sys.stdout)
except KeyboardInterrupt:
sys.exit()