self.wfile.write(bytes(html, 'utf-8'))
@staticmethod
- def _wrap(method: str, not_found_msg: str
- ) -> Callable[..., Callable[..., None]]:
- def decorator(http_method_handler: Callable[..., str]
- ) -> Callable[..., None]:
+ def _request_wrapper(http_method: str, not_found_msg: str
+ ) -> Callable[..., Callable[[TaskHandler], None]]:
+ def decorator(f: Callable[..., str | None]
+ ) -> Callable[[TaskHandler], None]:
def wrapper(self: TaskHandler) -> None:
try:
self.conn = DatabaseConnection(self.server.db)
self.site = path_split(parsed_url.path)[1]
params = parse_qs(parsed_url.query, strict_parsing=True)
self.params = InputsParser(params, False)
- handler_name = f'do_{method}_{self.site}'
+ handler_name = f'do_{http_method}_{self.site}'
if hasattr(self, handler_name):
handler = getattr(self, handler_name)
- redir_target = http_method_handler(self, handler)
+ redir_target = f(self, handler)
if redir_target:
self.send_response(302)
self.send_header('Location', redir_target)
return wrapper
return decorator
- @_wrap('GET', 'Unknown page')
- def do_GET(self, handler: Callable[..., str | dict[str, object]]
+ @_request_wrapper('GET', 'Unknown page')
+ def do_GET(self, handler: Callable[[], str | dict[str, object]]
) -> str | None:
"""Render page with result of handler, or redirect if result is str."""
template = f'{self.site}.html'
self.send_html(html)
return None
- @_wrap('POST', 'Unknown POST target')
- def do_POST(self, handler: Callable[..., str]) -> str:
+ @_request_wrapper('POST', 'Unknown POST target')
+ def do_POST(self, handler: Callable[[], str]) -> str:
"""Handle POST with handler, prepare redirection to result."""
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length).decode(),