1
0
Fork 0
forked from s3lph/matemat

Merge branch 'webserver-impl' into pagelet-return-api

This commit is contained in:
s3lph 2018-07-08 15:18:17 +02:00
commit 7c078f2c63
3 changed files with 23 additions and 9 deletions

View file

@ -1,5 +1,5 @@
from typing import Any, Callable, Dict, Tuple, Union from typing import Any, Callable, Dict, Tuple, Type, Union
import traceback import traceback
@ -89,6 +89,23 @@ def pagelet(path: str):
return http_handler return http_handler
class MatematHTTPServer(HTTPServer):
"""
A http.server.HTTPServer subclass that acts as a container for data that must be persistent between requests.
"""
def __init__(self,
server_address: Any,
handler: Type[BaseHTTPRequestHandler],
webroot: str,
bind_and_activate: bool = True) -> None:
super().__init__(server_address, handler, bind_and_activate)
# Resolve webroot directory
self.webroot = os.path.abspath(webroot)
# Set up session vars dict
self.session_vars: Dict[str, Tuple[datetime, Dict[str, Any]]] = dict()
class MatematWebserver(object): class MatematWebserver(object):
""" """
Then main webserver class, internally uses Python's http.server. Then main webserver class, internally uses Python's http.server.
@ -120,11 +137,7 @@ class MatematWebserver(object):
# Rewrite IPv4 address to IPv6-mapped form # Rewrite IPv4 address to IPv6-mapped form
listen = f'::ffff:{listen}' listen = f'::ffff:{listen}'
# Create the http server # Create the http server
self._httpd = HTTPServer((listen, port), HttpHandler) self._httpd = MatematHTTPServer((listen, port), HttpHandler, webroot)
# Set up session vars dict
self._httpd.session_vars: Dict[str, Tuple[datetime, Dict[str, Any]]] = dict()
# Resolve webroot directory
self._httpd.webroot = os.path.abspath(webroot)
def start(self) -> None: def start(self) -> None:
""" """
@ -143,6 +156,7 @@ class HttpHandler(BaseHTTPRequestHandler):
def __init__(self, request: bytes, client_address: Tuple[str, int], server: HTTPServer) -> None: def __init__(self, request: bytes, client_address: Tuple[str, int], server: HTTPServer) -> None:
super().__init__(request, client_address, server) super().__init__(request, client_address, server)
self.server: MatematHTTPServer
@property @property
def server_version(self) -> str: def server_version(self) -> str:

View file

@ -50,7 +50,7 @@ class RequestArguments(object):
""" """
return _View.of(self.__container[key]) return _View.of(self.__container[key])
def __iter__(self) -> Iterator['RequestArguments']: def __iter__(self) -> Iterator['RequestArgument']:
""" """
Returns an iterator over the values in this instance. Values are represented as immutable views. Returns an iterator over the values in this instance. Values are represented as immutable views.

View file

@ -97,7 +97,7 @@ def parse_args(request: str, postbody: Optional[bytes] = None, enctype: str = 't
tokens = urllib.parse.urlparse(request) tokens = urllib.parse.urlparse(request)
# Parse the GET arguments # Parse the GET arguments
if len(tokens.query) == 0: if len(tokens.query) == 0:
getargs = dict() getargs: Dict[str, List[str]] = dict()
else: else:
getargs = urllib.parse.parse_qs(tokens.query, strict_parsing=True, keep_blank_values=True, errors='strict') getargs = urllib.parse.parse_qs(tokens.query, strict_parsing=True, keep_blank_values=True, errors='strict')
@ -112,7 +112,7 @@ def parse_args(request: str, postbody: Optional[bytes] = None, enctype: str = 't
# Parse the POST body # Parse the POST body
pb: str = postbody.decode('utf-8') pb: str = postbody.decode('utf-8')
if len(pb) == 0: if len(pb) == 0:
postargs = dict() postargs: Dict[str, List[str]] = dict()
else: else:
postargs = urllib.parse.parse_qs(pb, strict_parsing=True, keep_blank_values=True, errors='strict') postargs = urllib.parse.parse_qs(pb, strict_parsing=True, keep_blank_values=True, errors='strict')
# Write all POST values into the dict, overriding potential duplicates from GET # Write all POST values into the dict, overriding potential duplicates from GET