Hi there ๐ I'm Matt!
This is my free time work, not my best work
The main module can be rewrittent to support proxies:
import os
import flask
from flask import request
import requests
class ApiSslinger(object):
"""docstring for ApiSslinger"""
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = '5000'
IGNORED_REQ_HEADERS = ['HOST']
IGNORED_RES_HEADERS = [
'CONTENT-LENGTH', 'UPGRADE-INSECURE-REQUESTS', 'CONTENT-ENCODING']
HTTP_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']
@property
def proxies(self):
"""Obtain proxies dict config."""
proxies = {}
https_proxy = os.environ.get('https_proxy', '')
if https_proxy:
proxies['https'] = https_proxy
return proxies
def __init__(self):
"""Setup flask app"""
self.app = flask.Flask(__name__)
#self.app.route('/<path:url>', methods=self.HTTP_METHODS)(self.handle_request)
self.app.errorhandler(404)(self.handle_request)
def handle_request(self, url=None):
"""Handle API request"""
host_header = None
url = f"https://{request.host}/{request.path}"
# Remove ignored request headers
u_req_headers = dict(flask.request.headers)
req_headers = {}
[req_headers.update({h: u_req_headers[h]}) if h.upper() not in self.IGNORED_REQ_HEADERS else None for h in u_req_headers]
# Add new host header
if host_header:
req_headers['Host'] = host_header
r = requests.request(
# Copy request method
flask.request.method,
# Create https URL
url,
# Pass headers
headers=req_headers,
# ...and data/form data from request
data=(dict(flask.request.args)
if flask.request.args else
dict(flask.request.form)),
# Ensure redirects are returned to user
allow_redirects=False,
# Set proxies config
proxies=self.proxies
)
# Remove _banned_ headers.
# Define response headers and upstream response headers
res_headers = {}
u_res_headers = dict(r.headers)
[res_headers.update({h: u_res_headers[h]}) if h.upper() not in self.IGNORED_RES_HEADERS else None for h in u_res_headers]
return flask.Response(
# Passthrough response content, staus code and headers
response=r.content,
status=r.status_code,
headers=res_headers,
# Add content type, if they exist
content_type=res_headers.get('Content-Type', None),
# This seems necessary, since we writing a bunch
# of headers that flask will try to set.
direct_passthrough=True
)
def start(self, host=None, port=None, debug=False):
"""Start app."""
self.app.run(
host=(host if host else self.DEFAULT_HOST),
port=(port if port else self.DEFAULT_PORT),
debug=debug,
threaded=(os.environ.get('THREADING', True) != 'false'))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.