Skip to content

Commit

Permalink
send 404, fix 403 being sent on page not found
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Aug 6, 2024
1 parent b42c1c4 commit daba742
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions routers/static_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import gooey_gui as gui
import requests
from fastapi import HTTPException
from starlette.requests import Request
from starlette.responses import (
Response,
RedirectResponse,
HTMLResponse,
PlainTextResponse,
Expand Down Expand Up @@ -38,8 +38,11 @@ def serve_static_file(request: Request, path: str):

# if the path has no extension, try to serve a .html file
if not os.path.splitext(gcs_path)[1]:
html_url = bucket.blob(gcs_path + ".html").public_url
r = requests.get(html_url)
html_blob = bucket.blob(gcs_path + ".html")
if not html_blob.exists():
raise HTTPException(status_code=404)

r = requests.get(html_blob.public_url)
if r.ok:
html = r.content.decode()
# replace sign in button with user's name if logged in
Expand All @@ -51,12 +54,13 @@ def serve_static_file(request: Request, path: str):
)
return HTMLResponse(html, status_code=r.status_code)

url = bucket.blob(gcs_path).public_url
r = requests.head(url)
if r.ok:
return RedirectResponse(url, status_code=HTTP_308_PERMANENT_REDIRECT)
blob = bucket.blob(gcs_path)
if blob.exists():
return RedirectResponse(
blob.public_url, status_code=HTTP_308_PERMANENT_REDIRECT
)

return Response(status_code=r.status_code)
raise HTTPException(status_code=404)


@gui.route(app, "/internal/webflow-upload/")
Expand Down

0 comments on commit daba742

Please sign in to comment.