-
-
Save moos3/fb7897e93534a22ffbe6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib3 | |
import re | |
from flask import Flask, request, make_response, url_for | |
from flask.ext.cors import CORS | |
app = Flask(__name__) | |
app.config['IGNORE_REQUEST_HEADERS'] = ['content-length', 'host'] | |
app.config['IGNORE_RESPONSE_HEADERS'] = ['connection', 'transfer-encoding', | |
'keep-alive', 'content-encoding'] | |
cors = CORS(app) | |
@app.route('/api/<path:url>', | |
methods=['OPTIONS', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE']) | |
def api(url): | |
http = urllib3.PoolManager() | |
if request.query_string: | |
url += '?' + request.query_string | |
data = request.get_data() | |
headers = {key: value for (key, value) in request.headers.items() | |
if key.lower() not in app.config['IGNORE_REQUEST_HEADERS']} | |
if not data: | |
r = http.request(request.method, url, headers=headers) | |
else: | |
headers['Content-Length'] = str(len(data)) | |
r = http.urlopen(request.method, url, headers=headers, body=data) | |
body = re.sub('(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)', | |
url_for('api', url='', _external=True) + '\\1', r.data) | |
headers = {key: value for (key, value) in r.headers.items() | |
if key.lower() not in app.config['IGNORE_RESPONSE_HEADERS']} | |
return body, r.status, headers | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=443, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment