From 8d6acca5e94fb415ab8e437dbc112dd8c4220154 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Sat, 11 Oct 2025 18:55:24 +1100 Subject: [PATCH] feat: Add error message to header for HTML error responses --- server.py | 30 ++++++++++++++++-------------- tools.py | 20 +++++++++++++++----- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/server.py b/server.py index e0e5727..65e6d43 100644 --- a/server.py +++ b/server.py @@ -149,6 +149,18 @@ def javascript_get(name): return error_response(request) return send_from_directory("templates/assets/js", request.path.split("/")[-1]) + +@app.route("/download/") +def download_get(path): + if path not in DOWNLOAD_ROUTES: + return error_response(request, message="Invalid download") + # Check if file exists + path = DOWNLOAD_ROUTES[path] + if os.path.isfile(path): + return send_file(path) + + return error_response(request, message="File not found") + # endregion # region PWA routes @@ -553,7 +565,7 @@ def qrcode_get(data): qr.make() qr_image: Image.Image = qr.make_image( - fill_color="black", back_color="white").convert('RGB') # type: ignore + fill_color="black", back_color="white").convert('RGB') # type: ignore # Add logo logo = Image.open("templates/assets/img/favicon/logo.png") @@ -583,18 +595,6 @@ def supersecretpath_get(): return render_template("ascii.html", ascii_art=ascii_art_html) -@app.route("/download/") -def download_get(path): - if path not in DOWNLOAD_ROUTES: - return error_response(request, message="Invalid download") - # Check if file exists - path = DOWNLOAD_ROUTES[path] - if os.path.isfile(path): - return send_file(path) - - return error_response(request, message="File not found") - - @app.route("/hosting/send-enquiry", methods=["POST"]) def hosting_post(): global EMAIL_REQUEST_COUNT @@ -681,7 +681,7 @@ def hosting_post(): webhook_url = os.getenv("HOSTING_WEBHOOK") if not webhook_url: return json_response(request, "Hosting webhook not set", 500) - + data = { "content": "", "embeds": [ @@ -756,11 +756,13 @@ def catch_all_get(path: str): return error_response(request) + @app.errorhandler(404) def not_found(e): return error_response(request) # endregion + if __name__ == "__main__": app.run(debug=True, port=5000, host="127.0.0.1") diff --git a/tools.py b/tools.py index aae6e93..1160003 100644 --- a/tools.py +++ b/tools.py @@ -1,7 +1,8 @@ -from flask import Request, render_template, jsonify +from flask import Request, render_template, jsonify, make_response import os from functools import cache + def getClientIP(request): x_forwarded_for = request.headers.get("X-Forwarded-For") if x_forwarded_for: @@ -10,6 +11,7 @@ def getClientIP(request): ip = request.remote_addr return ip + def getGitCommit(): # if .git exists, get the latest commit hash if os.path.isdir(".git"): @@ -34,7 +36,7 @@ def getGitCommit(): def isCurl(request: Request) -> bool: """ Check if the request is from curl - + Args: request (Request): The Flask request object Returns: @@ -47,6 +49,7 @@ def isCurl(request: Request) -> bool: return True return False + def isCrawler(request: Request) -> bool: """ Check if the request is from a web crawler (e.g., Googlebot, Bingbot) @@ -79,6 +82,7 @@ def getFilePath(name, path): if name in files: return os.path.join(root, name) + def json_response(request: Request, message: str = "404 Not Found", code: int = 404): return jsonify( { @@ -92,8 +96,14 @@ def json_response(request: Request, message: str = "404 Not Found", code: int = def error_response(request: Request, message: str = "404 Not Found", code: int = 404, force_json: bool = False): if force_json or isCurl(request): return json_response(request, message, code) - + # Check if .html exists in templates + response = make_response(render_template( + "404.html", code=code, message=message), code) if os.path.isfile(f"templates/{code}.html"): - return render_template(f"{code}.html"), code - return render_template("404.html"), code \ No newline at end of file + response = make_response(render_template( + f"{code}.html", code=code, message=message), code) + + # Add message to response headers + response.headers["X-Error-Message"] = message + return response