Coder Social home page Coder Social logo

apisslinger's Introduction

Hi there ๐Ÿ‘‹ I'm Matt!

This is my free time work, not my best work

apisslinger's People

Contributors

matthewjohn avatar

Watchers

 avatar  avatar  avatar

apisslinger's Issues

Investigate suport via http proxy

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'))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.