From f1ff14d29cc124f2ca4e698d1ac28ff79eea97d2 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 7 Jul 2018 18:58:23 +0200 Subject: [PATCH 1/8] More user-friendly return value in the pagelet API. --- matemat/exceptions/AuthenticatonError.py | 2 +- matemat/exceptions/HttpException.py | 20 ++++ matemat/exceptions/__init__.py | 1 + matemat/webserver/httpd.py | 106 ++++++++++++------ matemat/webserver/pagelets/login.py | 19 ++-- matemat/webserver/pagelets/logout.py | 5 +- matemat/webserver/pagelets/main.py | 39 +++---- matemat/webserver/pagelets/touchkey.py | 19 ++-- matemat/webserver/test/abstract_httpd_test.py | 8 +- matemat/webserver/test/test_post.py | 2 +- matemat/webserver/test/test_serve.py | 10 +- matemat/webserver/test/test_session.py | 2 +- 12 files changed, 141 insertions(+), 92 deletions(-) create mode 100644 matemat/exceptions/HttpException.py diff --git a/matemat/exceptions/AuthenticatonError.py b/matemat/exceptions/AuthenticatonError.py index 0d23c2e..d332fdc 100644 --- a/matemat/exceptions/AuthenticatonError.py +++ b/matemat/exceptions/AuthenticatonError.py @@ -2,7 +2,7 @@ from typing import Optional -class AuthenticationError(BaseException): +class AuthenticationError(Exception): def __init__(self, msg: Optional[str] = None) -> None: super().__init__() diff --git a/matemat/exceptions/HttpException.py b/matemat/exceptions/HttpException.py new file mode 100644 index 0000000..4f792ce --- /dev/null +++ b/matemat/exceptions/HttpException.py @@ -0,0 +1,20 @@ + +class HttpException(Exception): + + def __init__(self, status: int = 500, title: str = 'An error occurred', message: str = None) -> None: + super().__init__() + self.__status: int = status + self.__title: str = title + self.__message: str = message + + @property + def status(self) -> int: + return self.__status + + @property + def title(self) -> str: + return self.__title + + @property + def message(self) -> str: + return self.__message diff --git a/matemat/exceptions/__init__.py b/matemat/exceptions/__init__.py index ebae437..9b10b5f 100644 --- a/matemat/exceptions/__init__.py +++ b/matemat/exceptions/__init__.py @@ -4,3 +4,4 @@ This package provides custom exception classes used in the Matemat codebase. from .AuthenticatonError import AuthenticationError from .DatabaseConsistencyError import DatabaseConsistencyError +from .HttpException import HttpException diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index b703f72..a161d42 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -1,5 +1,5 @@ -from typing import Any, Callable, Dict, Optional, Tuple, Union +from typing import Any, Callable, Dict, Tuple, Union import traceback @@ -13,10 +13,10 @@ from uuid import uuid4 from datetime import datetime, timedelta from matemat import __version__ as matemat_version +from matemat.exceptions import HttpException from matemat.webserver import RequestArguments from matemat.webserver.util import parse_args - # # Python internal class hacks # @@ -27,15 +27,16 @@ TCPServer.address_family = socket.AF_INET6 BaseHTTPRequestHandler.log_request = lambda self, code='-', size='-': None BaseHTTPRequestHandler.log_error = lambda self, fstring='', *args: None - # Dictionary to hold registered pagelet paths and their handler functions _PAGELET_PATHS: Dict[str, Callable[[str, # HTTP method (GET, POST, ...) str, # Request path RequestArguments, # HTTP Request arguments Dict[str, Any], # Session vars Dict[str, str]], # Response headers - Tuple[int, Union[bytes, str]]]] = dict() # Returns: (status code, response body) - + Union[ # Return type: either a response body, or a redirect + bytes, str, # Response body: will assign HTTP/1.0 200 OK + Tuple[int, str] # Redirect: First element must be 301, second the redirect path + ]]] = dict() # Inactivity timeout for client sessions _SESSION_TIMEOUT: int = 3600 @@ -54,15 +55,17 @@ def pagelet(path: str): args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str]) - -> (int, Optional[Union[str, bytes]]) + -> Union[bytes, str, Tuple[int, str]] method: The HTTP method (GET, POST) that was used. path: The path that was requested. args: The arguments that were passed with the request (as GET or POST arguments). session_vars: The session storage. May be read from and written to. headers: The dictionary of HTTP response headers. Add headers you wish to send with the response. - returns: A tuple consisting of the HTTP status code (as an int) and the response body (as str or bytes, - may be None) + returns: One of the following: + - A HTTP Response body as str or bytes + - A HTTP redirect: A tuple of 301 (an int) and the path to redirect to (a str) + raises: HttpException: If a non-200 HTTP status code should be returned :param path: The path to register the function for. """ @@ -72,11 +75,15 @@ def pagelet(path: str): RequestArguments, Dict[str, Any], Dict[str, str]], - Tuple[int, Optional[Union[bytes, str]]]]): + Union[ + bytes, str, + Tuple[int, str] + ]]): # Add the function to the dict of pagelets _PAGELET_PATHS[path] = fun # Don't change the function itself at all return fun + # Return the inner function (Python requires a "real" function annotation to not have any arguments except # the function itself) return http_handler @@ -167,7 +174,7 @@ class HttpHandler(BaseHTTPRequestHandler): if session_id not in self.server.session_vars: self.server.session_vars[session_id] = (now + timedelta(seconds=_SESSION_TIMEOUT)), dict() else: - self.server.session_vars[session_id] =\ + self.server.session_vars[session_id] = \ (now + timedelta(seconds=_SESSION_TIMEOUT), self.server.session_vars[session_id][1]) # Return the session ID and timeout return session_id, self.server.session_vars[session_id][0] @@ -181,6 +188,45 @@ class HttpHandler(BaseHTTPRequestHandler): if session_id in self.server.session_vars: del self.server.session_vars[session_id] + @staticmethod + def _parse_pagelet_result(pagelet_res: Union[bytes, str, Tuple[int, str]], headers: Dict[str, str]) \ + -> Tuple[int, bytes]: + """ + Process the return value of a pagelet function call. + + :param pagelet_res: The pagelet return value. + :param headers: The dict of HTTP response headers, needed for setting the redirect header. + :return: The HTTP Response status code (an int) and body (a bytes). + :raises TypeError: If the pagelet result was not in the expected form. + """ + # The HTTP Response Status Code, defaults to 200 OK + hsc: int = 200 + # The HTTP Response body, defaults to None + data: Union[bytes, str] = None + if isinstance(pagelet_res, tuple): + # If the return type is a tuple, the first element must be 301 (the HTTP Redirect status code) + head, tail = pagelet_res + if head == 301: + # Set the HTTP Response Status Code, and the redirect header + hsc = 301 + headers['Location'] = tail + else: + raise TypeError(f'Return value of pagelet not understood: {pagelet_res}') + elif isinstance(pagelet_res, str) or isinstance(pagelet_res, bytes): + # Return value is a response body + data = pagelet_res + else: + # Return value is not a response body or a redirect + raise TypeError(f'Return value of pagelet not understood: {pagelet_res}') + # The pagelet may return None as data as a shorthand for an empty response + if data is None: + data = bytes() + # If the pagelet returns a Python str, convert it to an UTF-8 encoded bytes object + if isinstance(data, str): + data = data.encode('utf-8') + # Return the resulting status code and body + return hsc, data + def _handle(self, method: str, path: str, args: RequestArguments) -> None: """ Handle a HTTP request by either dispatching it to the appropriate pagelet or by serving a static resource. @@ -209,13 +255,9 @@ class HttpHandler(BaseHTTPRequestHandler): 'Cache-Control': 'no-cache' } # Call the pagelet function - hsc, data = _PAGELET_PATHS[path](method, path, args, self.session_vars, headers) - # The pagelet may return None as data as a shorthand for an empty response - if data is None: - data = bytes() - # If the pagelet returns a Python str, convert it to an UTF-8 encoded bytes object - if isinstance(data, str): - data = data.encode('utf-8') + pagelet_res = _PAGELET_PATHS[path](method, path, args, self.session_vars, headers) + # Parse the pagelet's return value, vielding a HTTP status code and a response body + hsc, data = HttpHandler._parse_pagelet_result(pagelet_res, headers) # Send the HTTP status code self.send_response(hsc) # Format the session cookie timeout string and send the session cookie header @@ -272,16 +314,18 @@ class HttpHandler(BaseHTTPRequestHandler): path, args = parse_args(self.path) self._handle('GET', path, args) # Special handling for some errors + except HttpException as e: + self.send_error(e.status, e.title, e.message) except PermissionError: - self.send_response(403, 'Forbidden') - self.end_headers() + self.send_error(403, 'Forbidden') except ValueError: - self.send_response(400, 'Bad Request') - self.end_headers() - except BaseException: + self.send_error(400, 'Bad Request') + except BaseException as e: # Generic error handling - self.send_response(500, 'Internal Server Error') - self.end_headers() + self.send_error(500, 'Internal Server Error') + print(e) + + traceback.print_tb(e.__traceback__) # noinspection PyPep8Naming def do_POST(self) -> None: @@ -299,19 +343,15 @@ class HttpHandler(BaseHTTPRequestHandler): # Parse the request and hand it to the handle function self._handle('POST', path, args) # Special handling for some errors + except HttpException as e: + self.send_error(e.status, e.title, e.message) except PermissionError: - self.send_response(403, 'Forbidden') - self.end_headers() + self.send_error(403, 'Forbidden') except ValueError: - self.send_response(400, 'Bad Request') - self.end_headers() - except TypeError: - self.send_response(400, 'Bad Request') - self.end_headers() + self.send_error(400, 'Bad Request') except BaseException as e: # Generic error handling - self.send_response(500, 'Internal Server Error') - self.end_headers() + self.send_error(500, 'Internal Server Error') print(e) traceback.print_tb(e.__traceback__) diff --git a/matemat/webserver/pagelets/login.py b/matemat/webserver/pagelets/login.py index 7d0cc2d..150b174 100644 --- a/matemat/webserver/pagelets/login.py +++ b/matemat/webserver/pagelets/login.py @@ -1,7 +1,7 @@ -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Dict, Tuple, Union -from matemat.exceptions import AuthenticationError +from matemat.exceptions import AuthenticationError, HttpException from matemat.webserver import pagelet, RequestArguments from matemat.primitives import User from matemat.db import MatematDatabase @@ -13,10 +13,9 @@ def login_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Tuple[int, Optional[Union[str, bytes]]]: + -> Union[bytes, str, Tuple[int, str]]: if 'user' in session_vars: - headers['Location'] = '/' - return 301, None + return 301, '/' if method == 'GET': data = ''' @@ -41,15 +40,13 @@ def login_page(method: str, ''' - return 200, data.format(msg=str(args.msg) if 'msg' in args else '') + return data.format(msg=str(args.msg) if 'msg' in args else '') elif method == 'POST': with MatematDatabase('test.db') as db: try: user: User = db.login(str(args.username), str(args.password)) except AuthenticationError: - headers['Location'] = '/login?msg=Username%20or%20password%20wrong.%20Please%20try%20again.' - return 301, bytes() + return 301, '/login?msg=Username%20or%20password%20wrong.%20Please%20try%20again.' session_vars['user'] = user - headers['Location'] = '/' - return 301, bytes() - return 405, None + return 301, '/' + raise HttpException(405) diff --git a/matemat/webserver/pagelets/logout.py b/matemat/webserver/pagelets/logout.py index beb86a3..05bcfe2 100644 --- a/matemat/webserver/pagelets/logout.py +++ b/matemat/webserver/pagelets/logout.py @@ -10,8 +10,7 @@ def logout(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Tuple[int, Optional[Union[str, bytes]]]: + -> Union[bytes, str, Tuple[int, str]]: if 'user' in session_vars: del session_vars['user'] - headers['Location'] = '/' - return 301, None + return 301, '/' diff --git a/matemat/webserver/pagelets/main.py b/matemat/webserver/pagelets/main.py index e22c872..db4c49a 100644 --- a/matemat/webserver/pagelets/main.py +++ b/matemat/webserver/pagelets/main.py @@ -12,7 +12,7 @@ def main_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Tuple[int, Optional[Union[str, bytes]]]: + -> Union[bytes, str, Tuple[int, str]]: data = ''' @@ -34,24 +34,19 @@ def main_page(method: str, ''' - try: - with MatematDatabase('test.db') as db: - if 'user' in session_vars: - user: User = session_vars['user'] - products = db.list_products() - plist = '\n'.join([f'
  • {p.name} ' + - f'{p.price_member//100 if user.is_member else p.price_non_member//100}' + - f'.{p.price_member%100 if user.is_member else p.price_non_member%100}' - for p in products]) - uname = f'{user.name} (Logout)' - data = data.format(user=uname, list=plist) - else: - users = db.list_users() - ulist = '\n'.join([f'
  • {u.name}' for u in users]) - ulist = ulist + '
  • Password login' - data = data.format(user='', list=ulist) - return 200, data - except BaseException as e: - import traceback - traceback.print_tb(e.__traceback__) - return 500, None + with MatematDatabase('test.db') as db: + if 'user' in session_vars: + user: User = session_vars['user'] + products = db.list_products() + plist = '\n'.join([f'
  • {p.name} ' + + f'{p.price_member//100 if user.is_member else p.price_non_member//100}' + + f'.{p.price_member%100 if user.is_member else p.price_non_member%100}' + for p in products]) + uname = f'{user.name} (Logout)' + data = data.format(user=uname, list=plist) + else: + users = db.list_users() + ulist = '\n'.join([f'
  • {u.name}' for u in users]) + ulist = ulist + '
  • Password login' + data = data.format(user='', list=ulist) + return data diff --git a/matemat/webserver/pagelets/touchkey.py b/matemat/webserver/pagelets/touchkey.py index 4de8009..280610e 100644 --- a/matemat/webserver/pagelets/touchkey.py +++ b/matemat/webserver/pagelets/touchkey.py @@ -1,7 +1,7 @@ -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Dict, Tuple, Union -from matemat.exceptions import AuthenticationError +from matemat.exceptions import AuthenticationError, HttpException from matemat.webserver import pagelet, RequestArguments from matemat.primitives import User from matemat.db import MatematDatabase @@ -13,10 +13,9 @@ def touchkey_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Tuple[int, Optional[Union[str, bytes]]]: + -> Union[bytes, str, Tuple[int, str]]: if 'user' in session_vars: - headers['Location'] = '/' - return 301, bytes() + return 301, '/' if method == 'GET': data = ''' @@ -40,15 +39,13 @@ def touchkey_page(method: str, ''' - return 200, data.format(username=str(args.username) if 'username' in args else '') + return data.format(username=str(args.username) if 'username' in args else '') elif method == 'POST': with MatematDatabase('test.db') as db: try: user: User = db.login(str(args.username), touchkey=str(args.touchkey)) except AuthenticationError: - headers['Location'] = f'/touchkey?username={args["username"]}&msg=Please%20try%20again.' - return 301, bytes() + return 301, f'/touchkey?username={args["username"]}&msg=Please%20try%20again.' session_vars['user'] = user - headers['Location'] = '/' - return 301, None - return 405, None + return 301, '/' + raise HttpException(405) diff --git a/matemat/webserver/test/abstract_httpd_test.py b/matemat/webserver/test/abstract_httpd_test.py index 103979b..ff4c0c6 100644 --- a/matemat/webserver/test/abstract_httpd_test.py +++ b/matemat/webserver/test/abstract_httpd_test.py @@ -16,6 +16,8 @@ class HttpResponse: """ A really basic HTTP response container and parser class, just good enough for unit testing a HTTP server, if even. + DO NOT USE THIS OUTSIDE UNIT TESTING! + Usage: response = HttpResponse() while response.parse_phase != 'done' @@ -161,16 +163,16 @@ def test_pagelet(path: str): RequestArguments, Dict[str, Any], Dict[str, str]], - Tuple[int, Union[bytes, str]]]): + Union[bytes, str, Tuple[int, str]]]): @pagelet(path) def testing_wrapper(method: str, path: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str]): - status, body = fun(method, path, args, session_vars, headers) headers['X-Test-Pagelet'] = fun.__name__ - return status, body + result = fun(method, path, args, session_vars, headers) + return result return testing_wrapper return with_testing_headers diff --git a/matemat/webserver/test/test_post.py b/matemat/webserver/test/test_post.py index 0bc5d16..e105354 100644 --- a/matemat/webserver/test/test_post.py +++ b/matemat/webserver/test/test_post.py @@ -24,7 +24,7 @@ def post_test_pagelet(method: str, dump += f'{a.name}: {a.get_str()}\n' else: dump += f'{a.name}: {codecs.encode(a.get_bytes(), "hex").decode("utf-8")}\n' - return 200, dump + return dump class TestPost(AbstractHttpdTest): diff --git a/matemat/webserver/test/test_serve.py b/matemat/webserver/test/test_serve.py index 722870b..b507df7 100644 --- a/matemat/webserver/test/test_serve.py +++ b/matemat/webserver/test/test_serve.py @@ -3,6 +3,7 @@ from typing import Any, Dict import os import os.path +from matemat.exceptions import HttpException from matemat.webserver import HttpHandler, RequestArguments from matemat.webserver.test.abstract_httpd_test import AbstractHttpdTest, test_pagelet @@ -14,7 +15,7 @@ def serve_test_pagelet_ok(method: str, session_vars: Dict[str, Any], headers: Dict[str, str]): headers['Content-Type'] = 'text/plain' - return 200, 'serve test pagelet ok' + return 'serve test pagelet ok' @test_pagelet('/just/testing/serve_pagelet_fail') @@ -25,7 +26,7 @@ def serve_test_pagelet_fail(method: str, headers: Dict[str, str]): session_vars['test'] = 'hello, world!' headers['Content-Type'] = 'text/plain' - return 500, 'serve test pagelet fail' + raise HttpException() class TestServe(AbstractHttpdTest): @@ -62,11 +63,8 @@ class TestServe(AbstractHttpdTest): HttpHandler(self.client_sock, ('::1', 45678), self.server) packet = self.client_sock.get_response() - # Make sure the correct pagelet was called - self.assertEqual('serve_test_pagelet_fail', packet.pagelet) - # Make sure the expected content is served + # Make sure an error is raised self.assertEqual(500, packet.statuscode) - self.assertEqual(b'serve test pagelet fail', packet.body) def test_serve_static_ok(self): # Request a static resource diff --git a/matemat/webserver/test/test_session.py b/matemat/webserver/test/test_session.py index 5cf408e..2e64ceb 100644 --- a/matemat/webserver/test/test_session.py +++ b/matemat/webserver/test/test_session.py @@ -16,7 +16,7 @@ def session_test_pagelet(method: str, headers: Dict[str, str]): session_vars['test'] = 'hello, world!' headers['Content-Type'] = 'text/plain' - return 200, 'session test' + return 'session test' class TestSession(AbstractHttpdTest): From a53144797020584fc9d2d1e5ac3bdfea79cd4031 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 7 Jul 2018 19:10:41 +0200 Subject: [PATCH 2/8] Added the changed pagelet API to the external documentation. --- doc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc b/doc index 14b8380..51e9404 160000 --- a/doc +++ b/doc @@ -1 +1 @@ -Subproject commit 14b8380090858c3bed5c3b2ee7cf1408aaa133df +Subproject commit 51e940460ddbaebb7f2ffc48d00d9ef19cf8d33f From 14f339e63052e8d05c53741b7493327aef648542 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 7 Jul 2018 19:24:00 +0200 Subject: [PATCH 3/8] Added unit test for redirection testing. --- matemat/webserver/httpd.py | 1 - matemat/webserver/test/test_serve.py | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index a161d42..3892ac8 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -324,7 +324,6 @@ class HttpHandler(BaseHTTPRequestHandler): # Generic error handling self.send_error(500, 'Internal Server Error') print(e) - traceback.print_tb(e.__traceback__) # noinspection PyPep8Naming diff --git a/matemat/webserver/test/test_serve.py b/matemat/webserver/test/test_serve.py index b507df7..5bcdd4d 100644 --- a/matemat/webserver/test/test_serve.py +++ b/matemat/webserver/test/test_serve.py @@ -18,6 +18,15 @@ def serve_test_pagelet_ok(method: str, return 'serve test pagelet ok' +@test_pagelet('/just/testing/serve_pagelet_redirect') +def serve_test_pagelet_redirect(method: str, + path: str, + args: RequestArguments, + session_vars: Dict[str, Any], + headers: Dict[str, str]): + return 301, '/foo/bar' + + @test_pagelet('/just/testing/serve_pagelet_fail') def serve_test_pagelet_fail(method: str, path: str, @@ -66,6 +75,18 @@ class TestServe(AbstractHttpdTest): # Make sure an error is raised self.assertEqual(500, packet.statuscode) + def test_serve_pagelet_redirect(self): + # Call the test pagelet that redirects to another path + self.client_sock.set_request(b'GET /just/testing/serve_pagelet_redirect HTTP/1.1\r\n\r\n') + HttpHandler(self.client_sock, ('::1', 45678), self.server) + packet = self.client_sock.get_response() + + # Make sure the correct redirect is issued + self.assertEqual(301, packet.statuscode) + self.assertEqual('/foo/bar', packet.headers['Location']) + # Make sure the response body is empty + self.assertEqual(0, len(packet.body)) + def test_serve_static_ok(self): # Request a static resource self.client_sock.set_request(b'GET /static_resource.txt HTTP/1.1\r\n\r\n') From 079d9909c0ce233a3375e20869c795b7812afc29 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 8 Jul 2018 15:10:22 +0200 Subject: [PATCH 4/8] Some typing fixes that make mypy a litte happier --- matemat/webserver/httpd.py | 28 +++++++++++++++++++++------- matemat/webserver/requestargs.py | 2 +- matemat/webserver/util.py | 4 ++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index b703f72..3215ad2 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -1,5 +1,5 @@ -from typing import Any, Callable, Dict, Optional, Tuple, Union +from typing import Any, Callable, Dict, Optional, Tuple, Type, Union import traceback @@ -34,7 +34,7 @@ _PAGELET_PATHS: Dict[str, Callable[[str, # HTTP method (GET, POST, ...) RequestArguments, # HTTP Request arguments Dict[str, Any], # Session vars Dict[str, str]], # Response headers - Tuple[int, Union[bytes, str]]]] = dict() # Returns: (status code, response body) + Tuple[int, Optional[Union[bytes, str]]]]] = dict() # Returns: (status code, response body) # Inactivity timeout for client sessions @@ -82,6 +82,23 @@ def pagelet(path: str): 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): """ Then main webserver class, internally uses Python's http.server. @@ -113,11 +130,7 @@ class MatematWebserver(object): # Rewrite IPv4 address to IPv6-mapped form listen = f'::ffff:{listen}' # Create the http server - self._httpd = HTTPServer((listen, port), HttpHandler) - # 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) + self._httpd = MatematHTTPServer((listen, port), HttpHandler, webroot) def start(self) -> None: """ @@ -136,6 +149,7 @@ class HttpHandler(BaseHTTPRequestHandler): def __init__(self, request: bytes, client_address: Tuple[str, int], server: HTTPServer) -> None: super().__init__(request, client_address, server) + self.server: MatematHTTPServer @property def server_version(self) -> str: diff --git a/matemat/webserver/requestargs.py b/matemat/webserver/requestargs.py index 2150b31..373db90 100644 --- a/matemat/webserver/requestargs.py +++ b/matemat/webserver/requestargs.py @@ -50,7 +50,7 @@ class RequestArguments(object): """ 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. diff --git a/matemat/webserver/util.py b/matemat/webserver/util.py index 6e19d7b..c95d303 100644 --- a/matemat/webserver/util.py +++ b/matemat/webserver/util.py @@ -97,7 +97,7 @@ def parse_args(request: str, postbody: Optional[bytes] = None, enctype: str = 't tokens = urllib.parse.urlparse(request) # Parse the GET arguments if len(tokens.query) == 0: - getargs = dict() + getargs: Dict[str, List[str]] = dict() else: 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 pb: str = postbody.decode('utf-8') if len(pb) == 0: - postargs = dict() + postargs: Dict[str, List[str]] = dict() else: 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 From a0d1520ecfb93cc81711b4cb46b4f203d3398e6e Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 8 Jul 2018 15:13:58 +0200 Subject: [PATCH 5/8] Fixed a line-to-long style error --- matemat/webserver/httpd.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index 3215ad2..2bae715 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -34,7 +34,8 @@ _PAGELET_PATHS: Dict[str, Callable[[str, # HTTP method (GET, POST, ...) RequestArguments, # HTTP Request arguments Dict[str, Any], # Session vars Dict[str, str]], # Response headers - Tuple[int, Optional[Union[bytes, str]]]]] = dict() # Returns: (status code, response body) + # Returns: (status code, response body) + Tuple[int, Optional[Union[bytes, str]]]]] = dict() # Inactivity timeout for client sessions From 4d2d2d30c1a7943d25dcd204a8a38bca870126fe Mon Sep 17 00:00:00 2001 From: s3lph Date: Mon, 9 Jul 2018 00:11:40 +0200 Subject: [PATCH 6/8] Added Jinja2 dependency --- README.md | 1 + requirements.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index d1eec08..b27f921 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ This project intends to provide a well-tested and maintainable alternative to - Python 3 (>=3.6) - Python dependencies: - apsw + - jinja2 ## Usage diff --git a/requirements.txt b/requirements.txt index b8ab4ea..7663204 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ apsw +jinja2 From e3c65776b55835b2831c5b2305825e3fc830b796 Mon Sep 17 00:00:00 2001 From: s3lph Date: Mon, 9 Jul 2018 01:09:53 +0200 Subject: [PATCH 7/8] A first, semi-sane integration of Jinja2 templates --- matemat/webserver/httpd.py | 55 +++++++++++++++++++------- matemat/webserver/pagelets/login.py | 29 ++------------ matemat/webserver/pagelets/logout.py | 2 +- matemat/webserver/pagelets/main.py | 35 ++-------------- matemat/webserver/pagelets/touchkey.py | 31 +++------------ templates/login.html | 20 ++++++++++ templates/main.html | 33 ++++++++++++++++ templates/touchkey.html | 20 ++++++++++ 8 files changed, 127 insertions(+), 98 deletions(-) create mode 100644 templates/login.html create mode 100644 templates/main.html create mode 100644 templates/touchkey.html diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index 1f99aa1..c82ba10 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -12,6 +12,8 @@ from http.cookies import SimpleCookie from uuid import uuid4 from datetime import datetime, timedelta +import jinja2 + from matemat import __version__ as matemat_version from matemat.exceptions import HttpException from matemat.webserver import RequestArguments @@ -35,7 +37,8 @@ _PAGELET_PATHS: Dict[str, Callable[[str, # HTTP method (GET, POST, ...) Dict[str, str]], # Response headers Union[ # Return type: either a response body, or a redirect bytes, str, # Response body: will assign HTTP/1.0 200 OK - Tuple[int, str] # Redirect: First element must be 301, second the redirect path + Tuple[int, str], # Redirect: First element must be 301, second the redirect path + Tuple[str, Dict[str, Any]] # Jinja template name and kwargs ]]] = dict() # Inactivity timeout for client sessions @@ -65,6 +68,8 @@ def pagelet(path: str): returns: One of the following: - A HTTP Response body as str or bytes - A HTTP redirect: A tuple of 301 (an int) and the path to redirect to (a str) + - A Jinja template call: A tuple of the template name (a string) and the template rendering + arguments (a kwargs dict) raises: HttpException: If a non-200 HTTP status code should be returned :param path: The path to register the function for. @@ -77,7 +82,8 @@ def pagelet(path: str): Dict[str, str]], Union[ bytes, str, - Tuple[int, str] + Tuple[int, str], + Tuple[str, Dict[str, Any]] ]]): # Add the function to the dict of pagelets _PAGELET_PATHS[path] = fun @@ -97,13 +103,18 @@ class MatematHTTPServer(HTTPServer): def __init__(self, server_address: Any, handler: Type[BaseHTTPRequestHandler], - webroot: str, + staticroot: str, + templateroot: str, bind_and_activate: bool = True) -> None: super().__init__(server_address, handler, bind_and_activate) # Resolve webroot directory - self.webroot = os.path.abspath(webroot) + self.webroot = os.path.abspath(staticroot) # Set up session vars dict self.session_vars: Dict[str, Tuple[datetime, Dict[str, Any]]] = dict() + # Set up the Jinja2 environment + self.jinja_env: jinja2.Environment = jinja2.Environment( + loader=jinja2.FileSystemLoader(os.path.abspath(templateroot)) + ) class MatematWebserver(object): @@ -121,13 +132,18 @@ class MatematWebserver(object): server.start() """ - def __init__(self, listen: str = '::', port: int = 80, webroot: str = './webroot') -> None: + def __init__(self, + listen: str = '::', + port: int = 80, + staticroot: str = './static', + templateroot: str = './templates') -> None: """ Instantiate a MatematWebserver. - :param listen: The IPv4 or IPv6 address to listen on - :param port: The TCP port to listen on - :param webroot: Path to the webroot directory + :param listen: The IPv4 or IPv6 address to listen on. + :param port: The TCP port to listen on. + :param staticroot: Path to the static webroot directory. + :param templateroot: Path to the Jinja2 templates root directory. """ if len(listen) == 0: # Empty string should be interpreted as all addresses @@ -137,7 +153,7 @@ class MatematWebserver(object): # Rewrite IPv4 address to IPv6-mapped form listen = f'::ffff:{listen}' # Create the http server - self._httpd = MatematHTTPServer((listen, port), HttpHandler, webroot) + self._httpd = MatematHTTPServer((listen, port), HttpHandler, staticroot, templateroot) def start(self) -> None: """ @@ -202,8 +218,12 @@ class HttpHandler(BaseHTTPRequestHandler): if session_id in self.server.session_vars: del self.server.session_vars[session_id] - @staticmethod - def _parse_pagelet_result(pagelet_res: Union[bytes, str, Tuple[int, str]], headers: Dict[str, str]) \ + def _parse_pagelet_result(self, + pagelet_res: Union[bytes, # Response body as bytes + str, # Response body as str + Tuple[int, str], # Redirect + Tuple[str, Dict[str, Any]]], # Jinja template name, kwargs dict + headers: Dict[str, str]) \ -> Tuple[int, bytes]: """ Process the return value of a pagelet function call. @@ -218,12 +238,19 @@ class HttpHandler(BaseHTTPRequestHandler): # The HTTP Response body, defaults to None data: Union[bytes, str] = None if isinstance(pagelet_res, tuple): - # If the return type is a tuple, the first element must be 301 (the HTTP Redirect status code) + # If the return type is a tuple, it has to be either a redirect, in which case the first element must be + # int(301), or it is a template call, in which casse the first element must be the template name and the + # second element must be the kwargs dict to the template's render function head, tail = pagelet_res - if head == 301: + if head == 301 and isinstance(tail, str): # Set the HTTP Response Status Code, and the redirect header hsc = 301 headers['Location'] = tail + elif isinstance(head, str) and isinstance(tail, dict): + # Load the Jinja2 template and render it with the provided arguments + template = self.server.jinja_env.get_template(head) + tail['matemat_version'] = self.server_version + data = template.render(**tail) else: raise TypeError(f'Return value of pagelet not understood: {pagelet_res}') elif isinstance(pagelet_res, str) or isinstance(pagelet_res, bytes): @@ -271,7 +298,7 @@ class HttpHandler(BaseHTTPRequestHandler): # Call the pagelet function pagelet_res = _PAGELET_PATHS[path](method, path, args, self.session_vars, headers) # Parse the pagelet's return value, vielding a HTTP status code and a response body - hsc, data = HttpHandler._parse_pagelet_result(pagelet_res, headers) + hsc, data = self._parse_pagelet_result(pagelet_res, headers) # Send the HTTP status code self.send_response(hsc) # Format the session cookie timeout string and send the session cookie header diff --git a/matemat/webserver/pagelets/login.py b/matemat/webserver/pagelets/login.py index 150b174..d22fcd7 100644 --- a/matemat/webserver/pagelets/login.py +++ b/matemat/webserver/pagelets/login.py @@ -13,40 +13,17 @@ def login_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str]]: + -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: if 'user' in session_vars: return 301, '/' if method == 'GET': - data = ''' - - - - Matemat - - - -

    Matemat

    - {msg} -
    - Username:
    - Password:
    - -
    - - - ''' - return data.format(msg=str(args.msg) if 'msg' in args else '') + return 'login.html', {} elif method == 'POST': with MatematDatabase('test.db') as db: try: user: User = db.login(str(args.username), str(args.password)) except AuthenticationError: - return 301, '/login?msg=Username%20or%20password%20wrong.%20Please%20try%20again.' + return 301, '/login' session_vars['user'] = user return 301, '/' raise HttpException(405) diff --git a/matemat/webserver/pagelets/logout.py b/matemat/webserver/pagelets/logout.py index 05bcfe2..766558a 100644 --- a/matemat/webserver/pagelets/logout.py +++ b/matemat/webserver/pagelets/logout.py @@ -10,7 +10,7 @@ def logout(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str]]: + -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: if 'user' in session_vars: del session_vars['user'] return 301, '/' diff --git a/matemat/webserver/pagelets/main.py b/matemat/webserver/pagelets/main.py index db4c49a..24e6f0b 100644 --- a/matemat/webserver/pagelets/main.py +++ b/matemat/webserver/pagelets/main.py @@ -12,41 +12,12 @@ def main_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str]]: - data = ''' - - - - Matemat - - - -

    Matemat

    - {user} -
      - {list} -
    - - - ''' + -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: with MatematDatabase('test.db') as db: if 'user' in session_vars: user: User = session_vars['user'] products = db.list_products() - plist = '\n'.join([f'
  • {p.name} ' + - f'{p.price_member//100 if user.is_member else p.price_non_member//100}' + - f'.{p.price_member%100 if user.is_member else p.price_non_member%100}' - for p in products]) - uname = f'{user.name} (Logout)' - data = data.format(user=uname, list=plist) + return 'main.html', {'user': user, 'list': products} else: users = db.list_users() - ulist = '\n'.join([f'
  • {u.name}' for u in users]) - ulist = ulist + '
  • Password login' - data = data.format(user='', list=ulist) - return data + return 'main.html', {'list': users} diff --git a/matemat/webserver/pagelets/touchkey.py b/matemat/webserver/pagelets/touchkey.py index 280610e..27c943e 100644 --- a/matemat/webserver/pagelets/touchkey.py +++ b/matemat/webserver/pagelets/touchkey.py @@ -1,6 +1,8 @@ from typing import Any, Dict, Tuple, Union +import urllib.parse + from matemat.exceptions import AuthenticationError, HttpException from matemat.webserver import pagelet, RequestArguments from matemat.primitives import User @@ -13,39 +15,18 @@ def touchkey_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str]]: + -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: if 'user' in session_vars: return 301, '/' if method == 'GET': - data = ''' - - - - Matemat - - - -

    Matemat

    -
    -
    - Touchkey:
    - -
    - - - ''' - return data.format(username=str(args.username) if 'username' in args else '') + return 'touchkey.html', {'username': str(args.username)} if 'username' in args else {} elif method == 'POST': with MatematDatabase('test.db') as db: try: user: User = db.login(str(args.username), touchkey=str(args.touchkey)) except AuthenticationError: - return 301, f'/touchkey?username={args["username"]}&msg=Please%20try%20again.' + quoted = urllib.parse.quote_plus(bytes(args.username)) + return 301, f'/touchkey?username={quoted}' session_vars['user'] = user return 301, '/' raise HttpException(405) diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..bdb7168 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,20 @@ + + + + Matemat + + + +

    Matemat

    +
    + Username:
    + Password:
    + +
    + + diff --git a/templates/main.html b/templates/main.html new file mode 100644 index 0000000..d9a6d45 --- /dev/null +++ b/templates/main.html @@ -0,0 +1,33 @@ + + + + Matemat + + + +

    Matemat

    + {{ user|default("") }} +
      + {% if user is defined %} + {% for l in list %} +
    • {{ l.name }} + {% if user.is_member %} + {{ l.price_member }} + {% else %} + {{ l.price_non_member }} + {% endif %} + {% endfor %} + {% else %} + {% for l in list %} +
    • {{ l.name }} + {% endfor %} +
    • Password login + {% endif %} +
    + + diff --git a/templates/touchkey.html b/templates/touchkey.html new file mode 100644 index 0000000..ab12308 --- /dev/null +++ b/templates/touchkey.html @@ -0,0 +1,20 @@ + + + + Matemat + + + +

    Matemat

    +
    +
    + Touchkey:
    + +
    + + From 1b00c80133b2cf53835824390db6b2f462ca182d Mon Sep 17 00:00:00 2001 From: s3lph Date: Mon, 9 Jul 2018 20:50:02 +0200 Subject: [PATCH 8/8] Implemented a more explicit Pagelet return API using class instances to describe the action to take. --- matemat/webserver/__init__.py | 1 + matemat/webserver/httpd.py | 68 ++++++++-------- matemat/webserver/pagelets/login.py | 14 ++-- matemat/webserver/pagelets/logout.py | 8 +- matemat/webserver/pagelets/main.py | 10 +-- matemat/webserver/pagelets/touchkey.py | 12 +-- matemat/webserver/responses.py | 63 +++++++++++++++ matemat/webserver/test/abstract_httpd_test.py | 6 ++ matemat/webserver/test/test_serve.py | 79 +++++++++++++++---- 9 files changed, 186 insertions(+), 75 deletions(-) create mode 100644 matemat/webserver/responses.py diff --git a/matemat/webserver/__init__.py b/matemat/webserver/__init__.py index c52368e..6059687 100644 --- a/matemat/webserver/__init__.py +++ b/matemat/webserver/__init__.py @@ -7,4 +7,5 @@ server will attempt to serve the request with a static resource in a previously """ from .requestargs import RequestArgument, RequestArguments +from .responses import PageletResponse, RedirectResponse, TemplateResponse from .httpd import MatematWebserver, HttpHandler, pagelet diff --git a/matemat/webserver/httpd.py b/matemat/webserver/httpd.py index c82ba10..41866de 100644 --- a/matemat/webserver/httpd.py +++ b/matemat/webserver/httpd.py @@ -16,7 +16,7 @@ import jinja2 from matemat import __version__ as matemat_version from matemat.exceptions import HttpException -from matemat.webserver import RequestArguments +from matemat.webserver import RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.webserver.util import parse_args # @@ -37,8 +37,7 @@ _PAGELET_PATHS: Dict[str, Callable[[str, # HTTP method (GET, POST, ...) Dict[str, str]], # Response headers Union[ # Return type: either a response body, or a redirect bytes, str, # Response body: will assign HTTP/1.0 200 OK - Tuple[int, str], # Redirect: First element must be 301, second the redirect path - Tuple[str, Dict[str, Any]] # Jinja template name and kwargs + PageletResponse, # A generic response ]]] = dict() # Inactivity timeout for client sessions @@ -67,9 +66,8 @@ def pagelet(path: str): headers: The dictionary of HTTP response headers. Add headers you wish to send with the response. returns: One of the following: - A HTTP Response body as str or bytes - - A HTTP redirect: A tuple of 301 (an int) and the path to redirect to (a str) - - A Jinja template call: A tuple of the template name (a string) and the template rendering - arguments (a kwargs dict) + - A PageletResponse class instance: An instance of (a subclass of) + matemat.webserver.PageletResponse, e.g. encapsulating a redirect or a Jinja2 template. raises: HttpException: If a non-200 HTTP status code should be returned :param path: The path to register the function for. @@ -82,8 +80,7 @@ def pagelet(path: str): Dict[str, str]], Union[ bytes, str, - Tuple[int, str], - Tuple[str, Dict[str, Any]] + PageletResponse ]]): # Add the function to the dict of pagelets _PAGELET_PATHS[path] = fun @@ -221,8 +218,7 @@ class HttpHandler(BaseHTTPRequestHandler): def _parse_pagelet_result(self, pagelet_res: Union[bytes, # Response body as bytes str, # Response body as str - Tuple[int, str], # Redirect - Tuple[str, Dict[str, Any]]], # Jinja template name, kwargs dict + PageletResponse], # Encapsulated or unresolved response body headers: Dict[str, str]) \ -> Tuple[int, bytes]: """ @@ -235,36 +231,34 @@ class HttpHandler(BaseHTTPRequestHandler): """ # The HTTP Response Status Code, defaults to 200 OK hsc: int = 200 - # The HTTP Response body, defaults to None - data: Union[bytes, str] = None - if isinstance(pagelet_res, tuple): - # If the return type is a tuple, it has to be either a redirect, in which case the first element must be - # int(301), or it is a template call, in which casse the first element must be the template name and the - # second element must be the kwargs dict to the template's render function - head, tail = pagelet_res - if head == 301 and isinstance(tail, str): - # Set the HTTP Response Status Code, and the redirect header - hsc = 301 - headers['Location'] = tail - elif isinstance(head, str) and isinstance(tail, dict): - # Load the Jinja2 template and render it with the provided arguments - template = self.server.jinja_env.get_template(head) - tail['matemat_version'] = self.server_version - data = template.render(**tail) - else: - raise TypeError(f'Return value of pagelet not understood: {pagelet_res}') - elif isinstance(pagelet_res, str) or isinstance(pagelet_res, bytes): - # Return value is a response body + # The HTTP Response body, defaults to empty + data: bytes = bytes() + + # If the response is a bytes object, it is used without further modification + if isinstance(pagelet_res, bytes): data = pagelet_res + + # If the response is a str object, it is encoded into a bytes object + elif isinstance(pagelet_res, str): + data = pagelet_res.encode('utf-8') + + # If the response is a PageletResponse object, the status code is extracted. Generation of the body depends + # on the subtype + elif isinstance(pagelet_res, PageletResponse): + hsc = pagelet_res.status + + # If the object is a RedirectResponse instance, no body is needed + if isinstance(pagelet_res, RedirectResponse): + headers['Location'] = pagelet_res.location + # If the object is a TemplateRespinse instance, pass the Jinja2 environment instance for rendering + elif isinstance(pagelet_res, TemplateResponse): + # noinspection PyProtectedMember + data = pagelet_res._render(self.server.jinja_env) + # else: Empty body + else: - # Return value is not a response body or a redirect raise TypeError(f'Return value of pagelet not understood: {pagelet_res}') - # The pagelet may return None as data as a shorthand for an empty response - if data is None: - data = bytes() - # If the pagelet returns a Python str, convert it to an UTF-8 encoded bytes object - if isinstance(data, str): - data = data.encode('utf-8') + # Return the resulting status code and body return hsc, data diff --git a/matemat/webserver/pagelets/login.py b/matemat/webserver/pagelets/login.py index d22fcd7..6ef9a9c 100644 --- a/matemat/webserver/pagelets/login.py +++ b/matemat/webserver/pagelets/login.py @@ -1,8 +1,8 @@ -from typing import Any, Dict, Tuple, Union +from typing import Any, Dict, Union from matemat.exceptions import AuthenticationError, HttpException -from matemat.webserver import pagelet, RequestArguments +from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.primitives import User from matemat.db import MatematDatabase @@ -13,17 +13,17 @@ def login_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: + -> Union[bytes, str, PageletResponse]: if 'user' in session_vars: - return 301, '/' + return RedirectResponse('/') if method == 'GET': - return 'login.html', {} + return TemplateResponse('login.html') elif method == 'POST': with MatematDatabase('test.db') as db: try: user: User = db.login(str(args.username), str(args.password)) except AuthenticationError: - return 301, '/login' + return RedirectResponse('/login') session_vars['user'] = user - return 301, '/' + return RedirectResponse('/') raise HttpException(405) diff --git a/matemat/webserver/pagelets/logout.py b/matemat/webserver/pagelets/logout.py index 766558a..cbd7cad 100644 --- a/matemat/webserver/pagelets/logout.py +++ b/matemat/webserver/pagelets/logout.py @@ -1,7 +1,7 @@ -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, Union -from matemat.webserver import pagelet, RequestArguments +from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse @pagelet('/logout') @@ -10,7 +10,7 @@ def logout(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: + -> Union[bytes, str, PageletResponse]: if 'user' in session_vars: del session_vars['user'] - return 301, '/' + return RedirectResponse('/') diff --git a/matemat/webserver/pagelets/main.py b/matemat/webserver/pagelets/main.py index 24e6f0b..be0bd60 100644 --- a/matemat/webserver/pagelets/main.py +++ b/matemat/webserver/pagelets/main.py @@ -1,7 +1,7 @@ -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Dict, Union -from matemat.webserver import pagelet, RequestArguments +from matemat.webserver import pagelet, RequestArguments, PageletResponse, TemplateResponse from matemat.primitives import User from matemat.db import MatematDatabase @@ -12,12 +12,12 @@ def main_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: + -> Union[bytes, str, PageletResponse]: with MatematDatabase('test.db') as db: if 'user' in session_vars: user: User = session_vars['user'] products = db.list_products() - return 'main.html', {'user': user, 'list': products} + return TemplateResponse('main.html', user=user, list=products) else: users = db.list_users() - return 'main.html', {'list': users} + return TemplateResponse('main.html', list=users) diff --git a/matemat/webserver/pagelets/touchkey.py b/matemat/webserver/pagelets/touchkey.py index 27c943e..2b81c89 100644 --- a/matemat/webserver/pagelets/touchkey.py +++ b/matemat/webserver/pagelets/touchkey.py @@ -4,7 +4,7 @@ from typing import Any, Dict, Tuple, Union import urllib.parse from matemat.exceptions import AuthenticationError, HttpException -from matemat.webserver import pagelet, RequestArguments +from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.primitives import User from matemat.db import MatematDatabase @@ -15,18 +15,18 @@ def touchkey_page(method: str, args: RequestArguments, session_vars: Dict[str, Any], headers: Dict[str, str])\ - -> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]: + -> Union[bytes, str, PageletResponse]: if 'user' in session_vars: - return 301, '/' + return RedirectResponse('/') if method == 'GET': - return 'touchkey.html', {'username': str(args.username)} if 'username' in args else {} + return TemplateResponse('touchkey.html', username=str(args.username) if 'username' in args else None) elif method == 'POST': with MatematDatabase('test.db') as db: try: user: User = db.login(str(args.username), touchkey=str(args.touchkey)) except AuthenticationError: quoted = urllib.parse.quote_plus(bytes(args.username)) - return 301, f'/touchkey?username={quoted}' + return RedirectResponse(f'/touchkey?username={quoted}') session_vars['user'] = user - return 301, '/' + return RedirectResponse('/') raise HttpException(405) diff --git a/matemat/webserver/responses.py b/matemat/webserver/responses.py new file mode 100644 index 0000000..f1e7f90 --- /dev/null +++ b/matemat/webserver/responses.py @@ -0,0 +1,63 @@ + +from jinja2 import Environment, Template + + +class PageletResponse: + """ + Base class for pagelet return values that require more action than simply sending plain data. + + An instance of this base class will result in an empty 200 OK response. + """ + + def __init__(self, status: int = 200): + """ + Create an empty response. + + :param status: The HTTP status code, defaults to 200 (OK). + """ + self.status: int = status + + +class RedirectResponse(PageletResponse): + """ + A pagelet response that causes the server to redirect to another location, using a 301 Permanently Moved (uncached) + response status, and a Location header. + """ + + def __init__(self, location: str): + """ + Create a redirection response with the given redirection location. + + :param location: The location to redirect to. + """ + super().__init__(status=301) + self.location: str = location + + +class TemplateResponse(PageletResponse): + """ + A pagelet response that causes the server to load a Jinja2 template and render it with the provided arguments, then + sending the result as response body, with a 200 OK response status. + """ + + def __init__(self, name: str, **kwargs): + """ + Create a template response with the given template name and arguments. + + :param name: Name of the template to load. + :param kwargs: Arguments for rendering the template, will be passed to jinja2.Template.render as is. + """ + super().__init__() + self.name: str = name + self.kwargs = kwargs + + def _render(self, jinja_env: Environment) -> bytes: + """ + Load and render the template using the Jinja2 environment managed by the web server instance. This method + should not be called by a pagelet. + + :param jinja_env: The Jinja2 environment. + :return: An UTF-8 encoded bytes object containing the template rendering result. + """ + template: Template = jinja_env.get_template(self.name) + return template.render(**self.kwargs).encode('utf-8') diff --git a/matemat/webserver/test/abstract_httpd_test.py b/matemat/webserver/test/abstract_httpd_test.py index ff4c0c6..b121dfd 100644 --- a/matemat/webserver/test/abstract_httpd_test.py +++ b/matemat/webserver/test/abstract_httpd_test.py @@ -9,6 +9,8 @@ from abc import ABC from datetime import datetime from http.server import HTTPServer +import jinja2 + from matemat.webserver import pagelet, RequestArguments @@ -107,6 +109,10 @@ class MockServer: self.session_vars: Dict[str, Tuple[datetime, Dict[str, Any]]] = dict() # Webroot for statically served content self.webroot: str = webroot + # Jinja environment with a single, static template + self.jinja_env = jinja2.Environment( + loader=jinja2.DictLoader({'test.txt': 'Hello, {{ what }}!'}) + ) class MockSocket(bytes): diff --git a/matemat/webserver/test/test_serve.py b/matemat/webserver/test/test_serve.py index 5bcdd4d..7b0b61d 100644 --- a/matemat/webserver/test/test_serve.py +++ b/matemat/webserver/test/test_serve.py @@ -1,21 +1,31 @@ -from typing import Any, Dict +from typing import Any, Dict, Union import os import os.path from matemat.exceptions import HttpException -from matemat.webserver import HttpHandler, RequestArguments +from matemat.webserver import HttpHandler, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse from matemat.webserver.test.abstract_httpd_test import AbstractHttpdTest, test_pagelet -@test_pagelet('/just/testing/serve_pagelet_ok') -def serve_test_pagelet_ok(method: str, - path: str, - args: RequestArguments, - session_vars: Dict[str, Any], - headers: Dict[str, str]): +@test_pagelet('/just/testing/serve_pagelet_str') +def serve_test_pagelet_str(method: str, + path: str, + args: RequestArguments, + session_vars: Dict[str, Any], + headers: Dict[str, str]) -> Union[bytes, str, PageletResponse]: headers['Content-Type'] = 'text/plain' - return 'serve test pagelet ok' + return 'serve test pagelet str' + + +@test_pagelet('/just/testing/serve_pagelet_bytes') +def serve_test_pagelet_bytes(method: str, + path: str, + args: RequestArguments, + session_vars: Dict[str, Any], + headers: Dict[str, str]) -> Union[bytes, str, PageletResponse]: + headers['Content-Type'] = 'application/octet-stream' + return b'serve\x80test\xffpagelet\xfebytes' @test_pagelet('/just/testing/serve_pagelet_redirect') @@ -23,16 +33,27 @@ def serve_test_pagelet_redirect(method: str, path: str, args: RequestArguments, session_vars: Dict[str, Any], - headers: Dict[str, str]): - return 301, '/foo/bar' + headers: Dict[str, str]) -> Union[bytes, str, PageletResponse]: + return RedirectResponse('/foo/bar') +@test_pagelet('/just/testing/serve_pagelet_template') +def serve_test_pagelet_template(method: str, + path: str, + args: RequestArguments, + session_vars: Dict[str, Any], + headers: Dict[str, str]) -> Union[bytes, str, PageletResponse]: + headers['Content-Type'] = 'text/plain' + return TemplateResponse('test.txt', what='World') + + +# noinspection PyTypeChecker @test_pagelet('/just/testing/serve_pagelet_fail') def serve_test_pagelet_fail(method: str, path: str, args: RequestArguments, session_vars: Dict[str, Any], - headers: Dict[str, str]): + headers: Dict[str, str]) -> Union[bytes, str, PageletResponse]: session_vars['test'] = 'hello, world!' headers['Content-Type'] = 'text/plain' raise HttpException() @@ -54,17 +75,29 @@ class TestServe(AbstractHttpdTest): f.write('This should not be readable') os.chmod(forbidden, 0) - def test_serve_pagelet_ok(self): + def test_serve_pagelet_str(self): # Call the test pagelet that produces a 200 OK result - self.client_sock.set_request(b'GET /just/testing/serve_pagelet_ok HTTP/1.1\r\n\r\n') + self.client_sock.set_request(b'GET /just/testing/serve_pagelet_str HTTP/1.1\r\n\r\n') HttpHandler(self.client_sock, ('::1', 45678), self.server) packet = self.client_sock.get_response() # Make sure the correct pagelet was called - self.assertEqual('serve_test_pagelet_ok', packet.pagelet) + self.assertEqual('serve_test_pagelet_str', packet.pagelet) # Make sure the expected content is served self.assertEqual(200, packet.statuscode) - self.assertEqual(b'serve test pagelet ok', packet.body) + self.assertEqual(b'serve test pagelet str', packet.body) + + def test_serve_pagelet_bytes(self): + # Call the test pagelet that produces a 200 OK result + self.client_sock.set_request(b'GET /just/testing/serve_pagelet_bytes HTTP/1.1\r\n\r\n') + HttpHandler(self.client_sock, ('::1', 45678), self.server) + packet = self.client_sock.get_response() + + # Make sure the correct pagelet was called + self.assertEqual('serve_test_pagelet_bytes', packet.pagelet) + # Make sure the expected content is served + self.assertEqual(200, packet.statuscode) + self.assertEqual(b'serve\x80test\xffpagelet\xfebytes', packet.body) def test_serve_pagelet_fail(self): # Call the test pagelet that produces a 500 Internal Server Error result @@ -81,12 +114,26 @@ class TestServe(AbstractHttpdTest): HttpHandler(self.client_sock, ('::1', 45678), self.server) packet = self.client_sock.get_response() + # Make sure the correct pagelet was called + self.assertEqual('serve_test_pagelet_redirect', packet.pagelet) # Make sure the correct redirect is issued self.assertEqual(301, packet.statuscode) self.assertEqual('/foo/bar', packet.headers['Location']) # Make sure the response body is empty self.assertEqual(0, len(packet.body)) + def test_serve_pagelet_template(self): + # Call the test pagelet that redirects to another path + self.client_sock.set_request(b'GET /just/testing/serve_pagelet_template HTTP/1.1\r\n\r\n') + HttpHandler(self.client_sock, ('::1', 45678), self.server) + packet = self.client_sock.get_response() + + # Make sure the correct pagelet was called + self.assertEqual('serve_test_pagelet_template', packet.pagelet) + self.assertEqual(200, packet.statuscode) + # Make sure the response body was rendered correctly by the templating engine + self.assertEqual(b'Hello, World!', packet.body) + def test_serve_static_ok(self): # Request a static resource self.client_sock.set_request(b'GET /static_resource.txt HTTP/1.1\r\n\r\n')