2020-06-21 19:42:32 +00:00
|
|
|
from datetime import datetime, timezone
|
2020-02-23 16:22:13 +00:00
|
|
|
|
|
|
|
|
2021-04-29 16:18:18 +00:00
|
|
|
class BodySizeError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-02-23 18:29:17 +00:00
|
|
|
class Controller(object):
|
2020-02-23 17:32:37 +00:00
|
|
|
def __init__(self, handler, request, options):
|
2020-02-23 16:22:13 +00:00
|
|
|
self.handler = handler
|
|
|
|
self.request = request
|
2020-02-23 17:32:37 +00:00
|
|
|
self.options = options
|
2021-05-03 21:07:27 +00:00
|
|
|
self.responseCookies = None
|
2020-02-23 16:22:13 +00:00
|
|
|
|
2021-01-20 16:01:46 +00:00
|
|
|
def send_response(
|
|
|
|
self, content, code=200, content_type="text/html", last_modified: datetime = None, max_age=None, headers=None
|
|
|
|
):
|
2020-02-23 16:22:13 +00:00
|
|
|
self.handler.send_response(code)
|
2020-06-10 20:50:16 +00:00
|
|
|
if headers is None:
|
|
|
|
headers = {}
|
2020-02-23 16:22:13 +00:00
|
|
|
if content_type is not None:
|
2020-06-10 20:50:16 +00:00
|
|
|
headers["Content-Type"] = content_type
|
2020-02-23 16:22:13 +00:00
|
|
|
if last_modified is not None:
|
2020-06-21 19:42:32 +00:00
|
|
|
headers["Last-Modified"] = last_modified.astimezone(tz=timezone.utc).strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2020-02-23 16:22:13 +00:00
|
|
|
if max_age is not None:
|
2020-09-04 12:46:27 +00:00
|
|
|
headers["Cache-Control"] = "max-age={0}".format(max_age)
|
2020-06-10 20:50:16 +00:00
|
|
|
for key, value in headers.items():
|
2020-07-09 19:32:57 +00:00
|
|
|
self.handler.send_header(key, value)
|
2021-05-03 21:07:27 +00:00
|
|
|
if self.responseCookies is not None:
|
|
|
|
self.handler.send_header("Set-Cookie", self.responseCookies.output(header=""))
|
2020-02-23 16:22:13 +00:00
|
|
|
self.handler.end_headers()
|
|
|
|
if type(content) == str:
|
|
|
|
content = content.encode()
|
2021-08-03 13:03:20 +00:00
|
|
|
while len(content):
|
|
|
|
w = self.handler.wfile.write(content)
|
|
|
|
content = content[w:]
|
2020-02-23 16:22:13 +00:00
|
|
|
|
2021-05-03 21:07:27 +00:00
|
|
|
def send_redirect(self, location, code=303):
|
2020-02-23 18:23:18 +00:00
|
|
|
self.handler.send_response(code)
|
2021-05-03 21:07:27 +00:00
|
|
|
if self.responseCookies is not None:
|
|
|
|
self.handler.send_header("Set-Cookie", self.responseCookies.output(header=""))
|
2020-02-23 18:23:18 +00:00
|
|
|
self.handler.send_header("Location", location)
|
|
|
|
self.handler.end_headers()
|
|
|
|
|
2021-05-03 21:07:27 +00:00
|
|
|
def set_response_cookies(self, cookies):
|
|
|
|
self.responseCookies = cookies
|
|
|
|
|
2021-04-29 16:18:18 +00:00
|
|
|
def get_body(self, max_size=None):
|
2020-02-23 19:52:32 +00:00
|
|
|
if "Content-Length" not in self.handler.headers:
|
|
|
|
return None
|
|
|
|
length = int(self.handler.headers["Content-Length"])
|
2021-04-29 16:18:18 +00:00
|
|
|
if max_size is not None and length > max_size:
|
|
|
|
raise BodySizeError("HTTP body exceeds maximum allowed size")
|
2020-02-23 19:52:32 +00:00
|
|
|
return self.handler.rfile.read(length)
|
|
|
|
|
2020-02-23 16:22:13 +00:00
|
|
|
def handle_request(self):
|
2020-02-23 17:32:37 +00:00
|
|
|
action = "indexAction"
|
|
|
|
if "action" in self.options:
|
|
|
|
action = self.options["action"]
|
|
|
|
getattr(self, action)()
|