cheekeong2025 commited on
Commit
ae17e86
·
verified ·
1 Parent(s): fd6d8ba

Update loader.py

Browse files
Files changed (1) hide show
  1. loader.py +13 -6
loader.py CHANGED
@@ -33,8 +33,12 @@ def _with_q(url: str, req: Request) -> str:
33
  def _looks_file(path: str) -> bool:
34
  return "." in path.split("/")[-1]
35
 
 
 
 
 
36
  def _mime(path: str, default="application/octet-stream") -> str:
37
- if path.endswith("/") or path == "":
38
  return "text/html; charset=utf-8"
39
  m, _ = mimetypes.guess_type(path)
40
  return m or default
@@ -113,19 +117,22 @@ async def root(request: Request):
113
  except Exception as e:
114
  return PlainTextResponse(f"Error fetching root: {e}", status_code=500)
115
 
116
- ctype = r.headers.get("content-type", "text/html; charset=utf-8")
117
- if "text/html" in ctype:
118
- return HTMLResponse(r.text, status_code=r.status_code, media_type=ctype)
119
- return Response(content=r.content, status_code=r.status_code, media_type=ctype)
120
 
121
  @app.get("/{path:path}")
122
  async def proxy(path: str, request: Request):
123
  try:
124
  async with httpx.AsyncClient(timeout=None, follow_redirects=True) as client:
125
  r, tried = await _fetch(client, path, request)
126
- print(f"[proxy] path={path} status={r.status_code}")
127
  except Exception as e:
128
  return PlainTextResponse(f"Error fetching {path}: {e}", status_code=500)
129
 
 
 
 
 
 
130
  ctype = r.headers.get("content-type") or _mime(path)
131
  return Response(content=r.content, media_type=ctype, status_code=r.status_code)
 
33
  def _looks_file(path: str) -> bool:
34
  return "." in path.split("/")[-1]
35
 
36
+ def _is_html_path(path: str) -> bool:
37
+ # treat root/dirs and *.html as HTML routes
38
+ return (not _looks_file(path)) or path.lower().endswith(".html") or path == ""
39
+
40
  def _mime(path: str, default="application/octet-stream") -> str:
41
+ if _is_html_path(path):
42
  return "text/html; charset=utf-8"
43
  m, _ = mimetypes.guess_type(path)
44
  return m or default
 
117
  except Exception as e:
118
  return PlainTextResponse(f"Error fetching root: {e}", status_code=500)
119
 
120
+ # Force HTML for the root page (even if upstream says text/plain)
121
+ return HTMLResponse(r.text, status_code=r.status_code, media_type="text/html; charset=utf-8")
 
 
122
 
123
  @app.get("/{path:path}")
124
  async def proxy(path: str, request: Request):
125
  try:
126
  async with httpx.AsyncClient(timeout=None, follow_redirects=True) as client:
127
  r, tried = await _fetch(client, path, request)
128
+ print(f"[proxy] path={path} status={r.status_code}, upstream-ctype={r.headers.get('content-type')}")
129
  except Exception as e:
130
  return PlainTextResponse(f"Error fetching {path}: {e}", status_code=500)
131
 
132
+ if _is_html_path(path):
133
+ # Ensure HTML renders even if upstream mislabels as text/plain
134
+ return HTMLResponse(r.text, status_code=r.status_code, media_type="text/html; charset=utf-8")
135
+
136
+ # Non-HTML: keep upstream/guessed MIME and bytes
137
  ctype = r.headers.get("content-type") or _mime(path)
138
  return Response(content=r.content, media_type=ctype, status_code=r.status_code)