feat: better error page and headers handling

This commit is contained in:
2026-04-12 13:34:07 +02:00
parent f3b0b40251
commit 64adac02a2
2 changed files with 25 additions and 2 deletions
+1
View File
@@ -82,6 +82,7 @@ curl -X DELETE \
- [ ] proper doc - [ ] proper doc
- [ ] log visits (and store accross sessions) - [ ] log visits (and store accross sessions)
- [ ] deliver visits in /page/visits - [ ] deliver visits in /page/visits
- [x] better error page
### Makefile targets ### Makefile targets
+23 -1
View File
@@ -51,6 +51,9 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler):
return self.send_error(http.HTTPStatus.UNAUTHORIZED, "Invalid token") return self.send_error(http.HTTPStatus.UNAUTHORIZED, "Invalid token")
if (sub_path := self.get_subpath()) is None: if (sub_path := self.get_subpath()) is None:
return self.send_error(http.HTTPStatus.BAD_REQUEST, "Invalid path") return self.send_error(http.HTTPStatus.BAD_REQUEST, "Invalid path")
if not self.headers["Content-Length"]:
content_length = 0
else:
content_length = int(self.headers["Content-Length"]) content_length = int(self.headers["Content-Length"])
if content_length == 0: if content_length == 0:
return self.send_error(http.HTTPStatus.LENGTH_REQUIRED, "No body found") return self.send_error(http.HTTPStatus.LENGTH_REQUIRED, "No body found")
@@ -66,7 +69,7 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler):
except Exception as e: except Exception as e:
return self.send_error(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e)) return self.send_error(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e))
self.send_status_only(http.HTTPStatus.CREATED, f"Resource /{sub_path}/ updated") self.send_status_only(http.HTTPStatus.CREATED, f"Resource /{sub_path}/ updated")
if self.headers["X-Host"]: if self.headers["X-Host"] is not None:
self.registry.set_host(sub_path, self.headers["X-Host"]) self.registry.set_host(sub_path, self.headers["X-Host"])
self.registry.add(sub_path) self.registry.add(sub_path)
@@ -96,6 +99,8 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler):
return None return None
def get_host(self) -> str: def get_host(self) -> str:
if self.headers["Host"] is None:
return self.default_host
return self.headers["Host"].split(":")[0] return self.headers["Host"].split(":")[0]
def server_index(self): def server_index(self):
@@ -119,3 +124,20 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler):
self.send_response(code, message) self.send_response(code, message)
self.send_header("Content-Length", "0") self.send_header("Content-Length", "0")
self.end_headers() self.end_headers()
def send_error(
self, code: int, message: str | None = None, explain: str | None = None
):
shortmsg, longmsg = self.responses[code]
if message is None:
message = shortmsg
if explain is None:
explain = longmsg
if "Accept" not in self.headers["Accept"] or "text/" in self.headers["Accept"]:
self.send_basic_body(
f"{code} {message}\n{explain}\n{self.server_version}",
code=code,
message=message,
)
else:
self.send_status_only(code, message)