Skip to content

Commit

Permalink
handle text files correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterirving committed Sep 6, 2024
1 parent e1607c3 commit a655c92
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 0 additions & 10 deletions extensions/waybackmachine/waybackmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,6 @@ def handle_request(req):
# Return the processed content, status code, and appropriate Content-Type
return processed_content, response.status_code, {'Content-Type': mime_type}

except Exception as e:
print("Error occurred:", str(e))
error_message = f"Error fetching archived page: {str(e)}"
return f"<html><body><p>{error_message}</p></body></html>", 500, {'Content-Type': 'text/html'}

except Exception as e:
print("Error occurred:", str(e))
error_message = f"Error fetching archived page: {str(e)}"
return f"<html><body><p>{error_message}</p></body></html>", 500, {'Content-Type': 'text/html'}

except Exception as e:
print("Error occurred:", str(e))
error_message = f"Error fetching archived page: {str(e)}"
Expand Down
1 change: 1 addition & 0 deletions html_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"⁄": b"/",
"°": b"*",
"&deg;": b"*",
"&amp;": b"&",
"′": b"'",
"&prime;": b"'",
"″": b"''",
Expand Down
17 changes: 14 additions & 3 deletions proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,23 @@ def process_response(response):
status_code = 200
headers = {}

if isinstance(content, str):
content = transcode_html(content, app.config["DISABLE_CHAR_CONVERSION"])

content_type = headers.get('Content-Type', '').lower()

# Transcode content unless it's explicitly text/plain
if not content_type.startswith('text/plain'):
if isinstance(content, str):
content = transcode_html(content, app.config["DISABLE_CHAR_CONVERSION"])
elif isinstance(content, bytes):
content = transcode_html(content.decode('utf-8', errors='replace'), app.config["DISABLE_CHAR_CONVERSION"])
else:
# For text/plain, ensure content is in bytes
if isinstance(content, str):
content = content.encode('utf-8')

response = Response(content, status_code)
for key, value in headers.items():
response.headers[key] = value

return response

def handle_default_request():
Expand Down

0 comments on commit a655c92

Please sign in to comment.