rr1 commited on
Commit
da31e7c
·
verified ·
1 Parent(s): abb4d1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -48
app.py CHANGED
@@ -9,16 +9,12 @@ app = Flask(__name__)
9
  TARGET_API = os.getenv("TARGET_API", "https://huggingface.co")
10
 
11
  # Path mappings from environment variable
12
- # Expected format: {"path1": "mapped_path1", "path2": "mapped_path2"}
13
  def get_path_mappings():
14
  mappings_str = os.getenv("PATH_MAPPINGS", '{"/": "/"}')
15
  try:
16
  return json.loads(mappings_str)
17
  except json.JSONDecodeError:
18
- # Fallback to default mappings if JSON is invalid
19
- return {
20
- "/": "/",
21
- }
22
 
23
  PATH_MAPPINGS = get_path_mappings()
24
 
@@ -37,48 +33,31 @@ def proxy(path):
37
  # Construct target URL
38
  target_url = f"{TARGET_API}{full_path}"
39
 
40
- # Forward the request to the target API
41
- headers = {key: value for key, value in request.headers if key != 'Host'}
42
-
43
- # Handle streaming response
44
- if request.method == 'POST':
45
- response = requests.post(
46
- target_url,
47
- headers=headers,
48
- json=request.get_json(silent=True),
49
- params=request.args,
50
- stream=True
51
- )
52
- elif request.method == 'GET':
53
- response = requests.get(
54
- target_url,
55
- headers=headers,
56
- params=request.args,
57
- stream=True
58
- )
59
- elif request.method == 'PUT':
60
- response = requests.put(
61
- target_url,
62
- headers=headers,
63
- json=request.get_json(silent=True),
64
- params=request.args,
65
- stream=True
66
- )
67
- elif request.method == 'DELETE':
68
- response = requests.delete(
69
- target_url,
70
- headers=headers,
71
- params=request.args,
72
- stream=True
73
- )
74
- elif request.method == 'PATCH':
75
- response = requests.patch(
76
- target_url,
77
- headers=headers,
78
- json=request.get_json(silent=True),
79
- params=request.args,
80
- stream=True
81
- )
82
 
83
  # Create a response with the same status code, headers, and streaming content
84
  def generate():
@@ -105,4 +84,4 @@ def index():
105
 
106
 
107
  if __name__ == '__main__':
108
- app.run(host='0.0.0.0', port=7860, debug=False)
 
9
  TARGET_API = os.getenv("TARGET_API", "https://huggingface.co")
10
 
11
  # Path mappings from environment variable
 
12
  def get_path_mappings():
13
  mappings_str = os.getenv("PATH_MAPPINGS", '{"/": "/"}')
14
  try:
15
  return json.loads(mappings_str)
16
  except json.JSONDecodeError:
17
+ return {"/": "/"}
 
 
 
18
 
19
  PATH_MAPPINGS = get_path_mappings()
20
 
 
33
  # Construct target URL
34
  target_url = f"{TARGET_API}{full_path}"
35
 
36
+ # ==================== HEADER 过滤逻辑 ====================
37
+ # 定义允许转发的必要 Header 白名单(统一小写,方便匹配)
38
+ ESSENTIAL_HEADERS = {'content-type', 'accept', 'user-agent'}
39
+
40
+ headers = {}
41
+ for key, value in request.headers.items():
42
+ if key.lower() in ESSENTIAL_HEADERS:
43
+ headers[key] = value
44
+ # ========================================================
45
+
46
+ # 统一构建 requests 请求参数
47
+ request_kwargs = {
48
+ "method": request.method,
49
+ "url": target_url,
50
+ "headers": headers,
51
+ "params": request.args,
52
+ "stream": True
53
+ }
54
+
55
+ # 仅在有请求体的动词中带上 JSON 数据
56
+ if request.method in ['POST', 'PUT', 'PATCH']:
57
+ request_kwargs["json"] = request.get_json(silent=True)
58
+
59
+ # 发起请求
60
+ response = requests.request(**request_kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  # Create a response with the same status code, headers, and streaming content
63
  def generate():
 
84
 
85
 
86
  if __name__ == '__main__':
87
+ app.run(host='0.0.0.0', port=7860, debug=False)