fix gzip
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, request, Response
|
||||
import requests
|
||||
import gzip
|
||||
import io
|
||||
from flask import Flask, request, Response
|
||||
|
||||
# Setup logging
|
||||
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s")
|
||||
@@ -21,8 +23,10 @@ def proxy(subpath):
|
||||
params = request.args.to_dict()
|
||||
params["dkey"] = DKEY
|
||||
|
||||
# Forward headers (except Host) and body
|
||||
# Forward headers (except Host), remove Accept-Encoding to force uncompressed response
|
||||
headers = {k: v for k, v in request.headers if k.lower() != "host"}
|
||||
headers.pop("Accept-Encoding", None) # Prevent gzip response from upstream
|
||||
|
||||
data = request.get_data() if request.method in ["POST", "PUT", "PATCH"] else None
|
||||
|
||||
# Debug logs
|
||||
@@ -36,14 +40,28 @@ def proxy(subpath):
|
||||
try:
|
||||
# Forward the request
|
||||
resp = requests.request(
|
||||
method=request.method, url=url, params=params, headers=headers, data=data
|
||||
method=request.method, url=url, params=params, headers=headers, data=data, stream=True
|
||||
)
|
||||
|
||||
# Handle gzip responses
|
||||
content_encoding = resp.headers.get("Content-Encoding", "")
|
||||
if "gzip" in content_encoding:
|
||||
logging.debug("Decompressing Gzip response")
|
||||
buffer = io.BytesIO(resp.content)
|
||||
with gzip.GzipFile(fileobj=buffer, mode="rb") as gzipped_file:
|
||||
decompressed_content = gzipped_file.read()
|
||||
else:
|
||||
decompressed_content = resp.content
|
||||
|
||||
logging.debug(f"Response Status: {resp.status_code}")
|
||||
logging.debug(f"Response Headers: {dict(resp.headers)}")
|
||||
logging.debug(f"Response Body: {resp.text[:500]}") # Limit log size
|
||||
logging.debug(f"Response Body (first 500 chars): {decompressed_content[:500]}")
|
||||
|
||||
# Remove gzip encoding from response headers
|
||||
response_headers = {k: v for k, v in resp.headers.items() if k.lower() != "content-encoding"}
|
||||
|
||||
return Response(decompressed_content, resp.status_code, response_headers.items())
|
||||
|
||||
return Response(resp.content, resp.status_code, resp.headers.items())
|
||||
except requests.RequestException as e:
|
||||
logging.error(f"Request failed: {e}")
|
||||
return Response(f"Error: {str(e)}", status=500)
|
||||
@@ -51,3 +69,4 @@ def proxy(subpath):
|
||||
if __name__ == "__main__":
|
||||
logging.info(f"Starting proxy on port 80, forwarding to {TARGET_URL}")
|
||||
app.run(host="0.0.0.0", port=80, debug=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user