From 038ed7c5d3887919fcd8c528deee57ba159f52b4 Mon Sep 17 00:00:00 2001 From: nathansmith117 Date: Sun, 16 Mar 2025 22:14:36 -0600 Subject: Using http status correctly (hopefully) --- cgi-bin/webring_redirect.cgi | 46 +++++++++----------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) (limited to 'cgi-bin') diff --git a/cgi-bin/webring_redirect.cgi b/cgi-bin/webring_redirect.cgi index 2b114cc..299ed14 100755 --- a/cgi-bin/webring_redirect.cgi +++ b/cgi-bin/webring_redirect.cgi @@ -14,9 +14,9 @@ def handle_be_crime_do_gay_webring(): req.raise_for_status return req.json() except requests.exceptions.Timeout: - return "Status: 503 Service Unavailable" + return "Status: 503 Service Unavailable\n" except Exception: - return "Status: 500 Internal Error" + return "Status: 500 Internal Error\n" def handle_fields(): form = cgi.FieldStorage() @@ -26,7 +26,7 @@ def handle_fields(): direction = form.getvalue("direction") if name is None or direction is None: - return "Not enough optinos" + return "Status: 400 Bad Request\n" # Callbacks to handle webrings. webrings = { @@ -36,7 +36,7 @@ def handle_fields(): website_list = webrings[name]() if website_list == None or website_list == []: - return "Can't get website list for webring" + return "Status: 500 Internal Error\n" if type(website_list) is str: return website_list @@ -46,7 +46,7 @@ def handle_fields(): try: position = website_list.index(WEBSITE_URL) except ValueError: - return "Website not in webring" + return "Status: 400 Bad Request\n\nSite not added to webring yet" # Handle direction redirect_website = "" @@ -59,38 +59,10 @@ def handle_fields(): website_list.pop(position) redirect_website = random.choice(website_list) else: - return "Invalid direction" + return "Status: 400 Bad Request\n" - return [ - f"redirecting to {redirect_website}", - f"" - ] + return f"Status: 302 Found\nLocation: {redirect_website}\n" -def display_html(fields_reponse): - print("Content-Type: text/html") - - html_text = """ - - - - - {redirect} - Webrings - - - - {fields_reponse} - - - """ - - if type(fields_reponse) is list: - html_text = html_text.format(fields_reponse=fields_reponse[0], redirect=fields_reponse[1]) - else: - html_text = html_text.format(fields_reponse=fields_reponse, redirect="") - - print(html_text) - -fields_reponse = handle_fields() -display_html(fields_reponse) +print("Content-Type: text/html") +print(handle_fields()) -- cgit v1.2.3