Debian Codegraph @

testenv/server/http/http_server.py

raw
1 from http.server import HTTPServer, BaseHTTPRequestHandler
2 from exc.server_error import ServerError, AuthError, NoBodyServerError
3 from socketserver import BaseServer
4 from posixpath import basename, splitext
5 from base64 import b64encode
6 from random import random
7 from hashlib import md5
8 import threading
9 import socket
10 import os
11
12
13 class StoppableHTTPServer(HTTPServer):
14 """ This class extends the HTTPServer class from default http.server library
15 in Python 3. The StoppableHTTPServer class is capable of starting an HTTP
16 server that serves a virtual set of files made by the WgetFile class and
17 has most of its properties configurable through the server_conf()
18 method. """
19
20 request_headers = list()
21
22 """ Define methods for configuring the Server. """
23
24 def server_conf(self, filelist, conf_dict):
25 """ Set Server Rules and File System for this instance. """
26 self.server_configs = conf_dict
27 self.fileSys = filelist
28
29 def get_req_headers(self):
30 return self.request_headers
31
32
33 class HTTPSServer(StoppableHTTPServer):
34 """ The HTTPSServer class extends the StoppableHTTPServer class with
35 additional support for secure connections through SSL. """
36
37 def __init__(self, address, handler):
38 import ssl
39 BaseServer.__init__(self, address, handler)
40 # step one up because test suite change directory away from $srcdir
41 # (don't do that !!!)
42 CERTFILE = os.path.abspath(os.path.join('..',
43 os.getenv('srcdir', '.'),
44 'certs',
45 'server-cert.pem'))
46 KEYFILE = os.path.abspath(os.path.join('..',
47 os.getenv('srcdir', '.'),
48 'certs',
49 'server-key.pem'))
50 ctx = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_SERVER)
51 ctx.load_cert_chain(CERTFILE, KEYFILE)
52 self.socket = ctx.wrap_socket(
53 sock=socket.socket(self.address_family, self.socket_type),
54 server_side=True
55 )
56 self.server_bind()
57 self.server_activate()
58
59
60 class _Handler(BaseHTTPRequestHandler):
61 """ This is a private class which tells the server *HOW* to handle each
62 request. For each HTTP Request Command that the server should be capable of
63 responding to, there must exist a do_REQUESTNAME() method which details the
64 steps in which such requests should be processed. The rest of the methods
65 in this class are auxiliary methods created to help in processing certain
66 requests. """
67
68 def get_rule_list(self, name):
69 return self.rules.get(name)
70
71 # The default protocol version of the server we run is HTTP/1.1 not
72 # HTTP/1.0 which is the default with the http.server module.
73 protocol_version = 'HTTP/1.1'
74
75 """ Define functions for various HTTP Requests. """
76
77 def do_HEAD(self):
78 self.send_head("HEAD")
79
80 def do_GET(self):
81 """ Process HTTP GET requests. This is the same as processing HEAD
82 requests and then actually transmitting the data to the client. If
83 send_head() does not specify any "start" offset, we send the complete
84 data, else transmit only partial data. """
85
86 content, start = self.send_head("GET")
87 if content:
88 if start is None:
89 self.wfile.write(content.encode('utf-8'))
90 else:
91 self.wfile.write(content.encode('utf-8')[start:])
92
93 def do_POST(self):
94 """ According to RFC 7231 sec 4.3.3, if the resource requested in a POST
95 request does not exist on the server, the first POST request should
96 create that resource. PUT requests are otherwise used to create a
97 resource. Hence, we call the handle for processing PUT requests if the
98 resource requested does not already exist.
99
100 Currently, when the server receives a POST request for a resource, we
101 simply append the body data to the existing file and return the new
102 file to the client. If the file does not exist, a new file is created
103 using the contents of the request body. """
104
105 path = self.path[1:]
106 if path in self.server.fileSys:
107 self.rules = self.server.server_configs.get(path)
108 if not self.rules:
109 self.rules = dict()
110
111 if not self.custom_response():
112 return(None, None)
113
114 body_data = self.get_body_data()
115 self.send_response(200)
116 self.add_header("Content-type", "text/plain")
117 content = self.server.fileSys.pop(path) + "\n" + body_data
118 total_length = len(content)
119 self.server.fileSys[path] = content
120 self.add_header("Content-Length", total_length)
121 self.add_header("Location", self.path)
122 self.finish_headers()
123 try:
124 self.wfile.write(content.encode('utf-8'))
125 except Exception:
126 pass
127 else:
128 self.send_put(path)
129
130 def do_PUT(self):
131 path = self.path[1:]
132 self.rules = self.server.server_configs.get(path)
133 if not self.custom_response():
134 return(None, None)
135 self.send_put(path)
136
137 """ End of HTTP Request Method Handlers. """
138
139 """ Helper functions for the Handlers. """
140
141 def parse_range_header(self, header_line, length):
142 import re
143 if header_line is None:
144 return None
145 if not header_line.startswith("bytes="):
146 raise ServerError("Cannot parse header Range: %s" %
147 (header_line))
148 regex = re.match(r"^bytes=(\d*)\-$", header_line)
149 range_start = int(regex.group(1))
150 if range_start >= length:
151 raise ServerError("Range Overflow")
152 return range_start
153
154 def get_body_data(self):
155 cLength_header = self.headers.get("Content-Length")
156 cLength = int(cLength_header) if cLength_header is not None else 0
157 body_data = self.rfile.read(cLength).decode('utf-8')
158 return body_data
159
160 def send_put(self, path):
161 if path in self.server.fileSys:
162 self.server.fileSys.pop(path, None)
163 self.send_response(204)
164 else:
165 self.rules = dict()
166 self.send_response(201)
167 body_data = self.get_body_data()
168 self.server.fileSys[path] = body_data
169 self.add_header("Location", self.path)
170 self.finish_headers()
171
172 """ This empty method is called automatically when all the rules are
173 processed for a given request. However, send_header() should only be called
174 AFTER a response has been sent. But, at the moment of processing the rules,
175 the appropriate response has not yet been identified. As a result, we defer
176 the processing of this rule till later. Each do_* request handler MUST call
177 finish_headers() instead of end_headers(). The finish_headers() method
178 takes care of sending the appropriate headers before completing the
179 response. """
180 def SendHeader(self, header_obj):
181 pass
182
183 def send_cust_headers(self):
184 header_obj = self.get_rule_list('SendHeader')
185 if header_obj:
186 for header in header_obj.headers:
187 self.add_header(header, header_obj.headers[header])
188
189 def finish_headers(self):
190 self.send_cust_headers()
191 try:
192 for keyword, value in self._headers_dict.items():
193 if isinstance(value, list):
194 for value_el in value:
195 self.send_header(keyword, value_el)
196 else:
197 self.send_header(keyword, value)
198 # Clear the dictionary of existing headers for the next request
199 self._headers_dict.clear()
200 except AttributeError:
201 pass
202 self.end_headers()
203
204 def Response(self, resp_obj):
205 self.send_response(resp_obj.response_code)
206 if resp_obj.response_code == 304:
207 raise NoBodyServerError("Conditional get falling to head")
208 raise ServerError("Custom Response code sent.")
209
210 def custom_response(self):
211 codes = self.get_rule_list('Response')
212 if codes:
213 self.send_response(codes.response_code)
214 self.finish_headers()
215 return False
216 else:
217 return True
218
219 def add_header(self, keyword, value):
220 if not hasattr(self, "_headers_dict"):
221 self._headers_dict = dict()
222 self._headers_dict[keyword.lower()] = value
223
224 def base64(self, data):
225 string = b64encode(data.encode('utf-8'))
226 return string.decode('utf-8')
227
228 """ Send an authentication challenge.
229 This method calls self.send_header() directly instead of using the
230 add_header() method because sending multiple WWW-Authenticate headers
231 actually makes sense and we do use that feature in some tests. """
232 def send_challenge(self, auth_type, auth_parm):
233 auth_type = auth_type.lower()
234 if auth_type == "both":
235 self.send_challenge("basic", auth_parm)
236 self.send_challenge("digest", auth_parm)
237 return
238 if auth_type == "basic":
239 challenge_str = 'BasIc realm="Wget-Test"'
240 elif auth_type == "digest" or auth_type == "both_inline":
241 self.nonce = md5(str(random()).encode('utf-8')).hexdigest()
242 self.opaque = md5(str(random()).encode('utf-8')).hexdigest()
243 # 'DIgest' to provoke a Wget failure with turkish locales
244 challenge_str = 'DIgest realm="Test", nonce="%s", opaque="%s"' % (
245 self.nonce,
246 self.opaque)
247 try:
248 if auth_parm['qop']:
249 challenge_str += ', qop="%s"' % auth_parm['qop']
250 except KeyError:
251 pass
252 if auth_type == "both_inline":
253 # 'BasIc' to provoke a Wget failure with turkish locales
254 challenge_str = 'BasIc realm="Wget-Test", ' + challenge_str
255 self.send_header("WWW-Authenticate", challenge_str)
256
257 def authorize_basic(self, auth_header, auth_rule):
258 if auth_header is None or auth_header.split(' ')[0].lower() != 'basic':
259 return False
260 else:
261 self.user = auth_rule.auth_user
262 self.passw = auth_rule.auth_pass
263 auth_str = "basic " + self.base64(self.user + ":" + self.passw)
264 return True if auth_str.lower() == auth_header.lower() else False
265
266 def parse_auth_header(self, auth_header):
267 n = len("digest ")
268 auth_header = auth_header[n:].strip()
269 items = auth_header.split(", ")
270 keyvals = [i.split("=", 1) for i in items]
271 keyvals = [(k.strip(), v.strip().replace('"', '')) for k, v in keyvals]
272 return dict(keyvals)
273
274 def KD(self, secret, data):
275 return self.H(secret + ":" + data)
276
277 def H(self, data):
278 return md5(data.encode('utf-8')).hexdigest()
279
280 def A1(self):
281 return "%s:%s:%s" % (self.user, "Test", self.passw)
282
283 def A2(self, params):
284 return "%s:%s" % (self.command, params["uri"])
285
286 def check_response(self, params):
287 if "qop" in params:
288 data_str = params['nonce'] \
289 + ":" + params['nc'] \
290 + ":" + params['cnonce'] \
291 + ":" + params['qop'] \
292 + ":" + self.H(self.A2(params))
293 else:
294 data_str = params['nonce'] + ":" + self.H(self.A2(params))
295 resp = self.KD(self.H(self.A1()), data_str)
296
297 return True if resp == params['response'] else False
298
299 def authorize_digest(self, auth_header, auth_rule):
300 if auth_header is None or \
301 auth_header.split(' ')[0].lower() != 'digest':
302 return False
303 else:
304 self.user = auth_rule.auth_user
305 self.passw = auth_rule.auth_pass
306 params = self.parse_auth_header(auth_header)
307 if self.user != params['username'] or \
308 self.nonce != params['nonce'] or \
309 self.opaque != params['opaque']:
310 return False
311 req_attribs = ['username', 'realm', 'nonce', 'uri', 'response']
312 for attrib in req_attribs:
313 if attrib not in params:
314 return False
315 if not self.check_response(params):
316 return False
317
318 def authorize_both(self, auth_header, auth_rule):
319 return False
320
321 def authorize_both_inline(self, auth_header, auth_rule):
322 return False
323
324 def Authentication(self, auth_rule):
325 try:
326 self.handle_auth(auth_rule)
327 except AuthError as se:
328 self.send_response(401, "Authorization Required")
329 self.send_challenge(auth_rule.auth_type, auth_rule.auth_parm)
330 raise se
331
332 def handle_auth(self, auth_rule):
333 is_auth = True
334 auth_header = self.headers.get("Authorization")
335 required_auth = auth_rule.auth_type.lower()
336 if required_auth == "both" or required_auth == "both_inline":
337 if auth_header:
338 auth_type = auth_header.split(' ')[0].lower()
339 else:
340 auth_type = required_auth
341 else:
342 auth_type = required_auth
343 try:
344 assert hasattr(self, "authorize_" + auth_type)
345 is_auth = getattr(self, "authorize_" + auth_type)(auth_header,
346 auth_rule)
347 except AssertionError:
348 raise AuthError("Authentication Mechanism %s not supported" %
349 auth_type)
350 except AttributeError as ae:
351 raise AuthError(ae.__str__())
352 if is_auth is False:
353 raise AuthError("Unable to Authenticate")
354
355 def ExpectHeader(self, header_obj):
356 exp_headers = header_obj.headers
357 for header_line in exp_headers:
358 header_recd = self.headers.get(header_line)
359 if header_recd is None or header_recd != exp_headers[header_line]:
360 self.send_error(400, "Expected Header %s not found" %
361 header_line)
362 raise ServerError("Header " + header_line + " not found")
363
364 def RejectHeader(self, header_obj):
365 rej_headers = header_obj.headers
366 for header_line in rej_headers:
367 header_recd = self.headers.get(header_line)
368 if header_recd and header_recd == rej_headers[header_line]:
369 self.send_error(400, 'Blacklisted Header %s received' %
370 header_line)
371 raise ServerError("Header " + header_line + ' received')
372
373 def __log_request(self, method):
374 req = method + " " + self.path
375 self.server.request_headers.append(req)
376
377 def send_head(self, method):
378 """ Common code for GET and HEAD Commands.
379 This method is overridden to use the fileSys dict.
380
381 The method variable contains whether this was a HEAD or a GET Request.
382 According to RFC 2616, the server should not differentiate between
383 the two requests, however, we use it here for a specific test.
384 """
385
386 if self.path == "/":
387 path = "index.html"
388 else:
389 path = self.path[1:]
390
391 self.__log_request(method)
392
393 if path in self.server.fileSys:
394 self.rules = self.server.server_configs.get(path)
395
396 content = self.server.fileSys.get(path)
397 content_length = len(content)
398
399 for rule_name in self.rules:
400 try:
401 assert hasattr(self, rule_name)
402 getattr(self, rule_name)(self.rules[rule_name])
403 except AssertionError as ae:
404 msg = "Rule " + rule_name + " not defined"
405 self.send_error(500, msg)
406 return(None, None)
407 except AuthError as ae:
408 print(ae.__str__())
409 self.finish_headers()
410 return(None, None)
411 except NoBodyServerError as nbse:
412 print(nbse.__str__())
413 self.finish_headers()
414 return(None, None)
415 except ServerError as se:
416 print(se.__str__())
417 self.add_header("Content-Length", content_length)
418 self.finish_headers()
419 return(content, None)
420
421 try:
422 self.range_begin = self.parse_range_header(
423 self.headers.get("Range"), content_length)
424 except ServerError as ae:
425 # self.log_error("%s", ae.err_message)
426 if ae.err_message == "Range Overflow":
427 try:
428 self.overflows += 1
429 except AttributeError as s:
430 self.overflows = 0
431 self.send_response(416)
432 if self.overflows > 0:
433 self.add_header("Content-Length", 17)
434 self.finish_headers()
435 if self.overflows > 0:
436 return("Range Unsatisfied", 0)
437 return(None, None)
438 else:
439 self.range_begin = None
440 if self.range_begin is None:
441 self.send_response(200)
442 else:
443 self.send_response(206)
444 self.add_header("Accept-Ranges", "bytes")
445 self.add_header("Content-Range",
446 "bytes %d-%d/%d" % (self.range_begin,
447 content_length - 1,
448 content_length))
449 content_length -= self.range_begin
450 cont_type = self.guess_type(path)
451 self.add_header("Content-Type", cont_type)
452 self.add_header("Content-Length", content_length)
453 self.finish_headers()
454 return(content, self.range_begin)
455 else:
456 self.send_error(404, "Not Found")
457 return(None, None)
458
459 def guess_type(self, path):
460 base_name = basename("/" + path)
461 name, ext = splitext(base_name)
462 extension_map = {
463 ".txt": "text/plain",
464 ".css": "text/css",
465 ".html": "text/html"
466 }
467 return extension_map.get(ext, "text/plain")
468
469
470 class HTTPd(threading.Thread):
471 server_class = StoppableHTTPServer
472 handler = _Handler
473
474 def __init__(self, addr=None):
475 threading.Thread.__init__(self)
476 if addr is None:
477 addr = ('localhost', 0)
478 self.server_inst = self.server_class(addr, self.handler)
479 self.server_address = self.server_inst.socket.getsockname()[:2]
480
481 def run(self):
482 self.server_inst.serve_forever()
483
484 def server_conf(self, file_list, server_rules):
485 self.server_inst.server_conf(file_list, server_rules)
486
487
488 class HTTPSd(HTTPd):
489
490 server_class = HTTPSServer
491
492 # vim: set ts=4 sts=4 sw=4 tw=79 et :