-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (57 loc) · 1.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
import logging
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
unused_headers = (
"User-Agent",
"Accept",
"Cache-Control",
"Postman-Token",
"Host",
"Accept-Encoding",
"Connection",
"Content-Length",
)
@app.route("/apply-cors/<path:url>", methods=["OPTIONS"])
def cors_options(url):
logging.info(f"OPTIONS call, url is: {url}")
return jsonify()
@app.route("/apply-cors/<path:url>", methods=["POST"])
def cors_post(url):
try:
url = request.view_args.get("url")
if "://" not in url:
url = url.replace(":/", "://")
request_headers = dict(request.headers)
headers = {k: v for k, v in request_headers.items() if k not in unused_headers}
try:
data = json.dumps(request.json)
resp = requests.post(url, data=data, headers=headers)
except Exception:
resp = requests.post(url, headers=headers)
return jsonify(resp.json()), resp.status_code
except Exception as e:
logging.error(e)
return jsonify({"message": "failed"}), 400
@app.route("/apply-cors/<path:url>", methods=["GET"])
def cors_get(url):
try:
url = request.view_args.get("url")
if "://" not in url:
url = url.replace(":/", "://")
request_headers = dict(request.headers)
headers = {k: v for k, v in request_headers.items() if k not in unused_headers}
try:
data = json.dumps(request.json)
resp = requests.get(url, data=data, headers=headers)
except Exception:
resp = requests.get(url, headers=headers)
return jsonify(resp.json()), resp.status_code
except Exception as e:
logging.error(e)
return jsonify({"message": "failed"}), 400
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)