diff options
author | nathansmith117 <nathansmith@posteo.com> | 2025-03-16 22:14:36 -0600 |
---|---|---|
committer | nathansmith117 <nathansmith@posteo.com> | 2025-03-16 22:14:36 -0600 |
commit | 038ed7c5d3887919fcd8c528deee57ba159f52b4 (patch) | |
tree | 2eb8b6e862ac1e8309cea8f1a51b3650f698edad /cgi-bin | |
parent | 86eef6e8bc15bd44f4872e66625c0b0ab73d5c2e (diff) |
Using http status correctly (hopefully)
Diffstat (limited to 'cgi-bin')
-rwxr-xr-x | cgi-bin/webring_redirect.cgi | 46 |
1 files changed, 9 insertions, 37 deletions
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 <a href=\"{redirect_website}\">{redirect_website}</a>", - f"<meta http-equiv=\"refresh\" content=\"0; URL={redirect_website}\"/>" - ] + return f"Status: 302 Found\nLocation: {redirect_website}\n" -def display_html(fields_reponse): - print("Content-Type: text/html") - - html_text = """ - <!DOCTYPE html> - <html> - - <head> - {redirect} - <title>Webrings</title> - </head> - - <body> - {fields_reponse} - </body> - </html> - """ - - 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()) |