izuemon commited on
Commit
cf85907
·
verified ·
1 Parent(s): 35fee35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -53,27 +53,31 @@ def corsproxy():
53
  if not url:
54
  return "url パラメータが必要です", 400
55
 
56
- # URLが http/https のみ許可(安全のため)
57
  if not url.startswith("http://") and not url.startswith("https://"):
58
  return "http または https のURLのみ使用できます", 400
59
 
60
  try:
61
- # 元URLへ転送
62
  resp = requests.get(url, headers=request.headers, timeout=10)
63
 
64
- # CORS対応レスポンスヘッダを付与
65
- headers = {
66
- "Access-Control-Allow-Origin": "*",
67
- "Access-Control-Allow-Headers": "*",
68
- "Access-Control-Allow-Methods": "GET, POST, OPTIONS"
69
- }
70
 
71
- return Response(resp.content, resp.status_code, headers=headers)
 
 
 
 
 
 
 
 
 
72
 
73
  except Exception as e:
74
  return f"エラー: {str(e)}", 500
75
 
76
 
 
77
  @app.route("/cors-proxy", methods=["POST"])
78
  def corsproxy_post():
79
  url = request.args.get("url")
 
53
  if not url:
54
  return "url パラメータが必要です", 400
55
 
 
56
  if not url.startswith("http://") and not url.startswith("https://"):
57
  return "http または https のURLのみ使用できます", 400
58
 
59
  try:
 
60
  resp = requests.get(url, headers=request.headers, timeout=10)
61
 
62
+ # 元のレスポンスヘッダを継承
63
+ response = Response(resp.content, resp.status_code)
 
 
 
 
64
 
65
+ # CORSヘッダを追記(上書きしない)
66
+ response.headers["Access-Control-Allow-Origin"] = "*"
67
+ response.headers["Access-Control-Allow-Headers"] = "*"
68
+ response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
69
+
70
+ # ★ Content-Typeを元のままに設定 ★
71
+ if "Content-Type" in resp.headers:
72
+ response.headers["Content-Type"] = resp.headers["Content-Type"]
73
+
74
+ return response
75
 
76
  except Exception as e:
77
  return f"エラー: {str(e)}", 500
78
 
79
 
80
+
81
  @app.route("/cors-proxy", methods=["POST"])
82
  def corsproxy_post():
83
  url = request.args.get("url")