Beispiel #1
0
 def _json_dumps(self, obj, **kwargs):
     """Dump object to json string."""
     kwargs.setdefault("ensure_ascii", False)
     kwargs.setdefault("cls", JSONEncoder)
     kwargs.setdefault("indent", 4)
     kwargs.setdefault("separators", (",", ": "))
     return json.dumps(obj, **kwargs)
Beispiel #2
0
 def _json_dumps(self, obj, **kwargs):
     """Dump object to json string."""
     kwargs.setdefault('ensure_ascii', False)
     kwargs.setdefault('cls', JSONEncoder)
     kwargs.setdefault('indent', 4)
     kwargs.setdefault('separators', (',', ': '))
     return json.dumps(obj, **kwargs)
Beispiel #3
0
def proxy_request(path, proxy_host, proxy_port, proxy_prefix, proxy_auth):
    request_headers = {}
    for h in ["Cookie", "Referer", "X-Csrf-Token"]:
        if h in request.headers:
            request_headers[h] = request.headers[h]

    proxy_path = path
    if request.query_string:
        proxy_path = "%s?%s" % (path, request.query_string)

    if proxy_prefix:
        proxy_path = "/%s/%s" % (proxy_prefix.strip('/'), proxy_path)
    else:
        proxy_path = "/%s" + path

    logger.info("Forward request to : '%s@%s:%s%s" %
                (proxy_auth,proxy_host, proxy_port, proxy_path))
    request_headers["Authorization"] = 'Basic %s' % base64.b64encode(proxy_auth)
    request_headers["Content-Type"] = 'application/json'
    if request.method == "POST" or request.method == "PUT":
        form_data = json.dumps(request.json)
        request_headers["Content-Length"] = len(form_data)
    else:
        form_data = None

    conn = httplib.HTTPConnection(proxy_host, proxy_port)
    conn.request(request.method,
                 proxy_path,
                 body=form_data,
                 headers=request_headers)
    resp = conn.getresponse()

    # Clean up response headers for forwarding
    d = {}
    response_headers = Headers()
    for key, value in resp.getheaders():
        logger.debug(" | %s: %s" % (key, value))
        d[key.lower()] = value
        if key in ["content-length", "connection", "content-type", "transfer-encoding","connection"]:
            continue

        if key == "set-cookie":
            cookies = value.split(",")
            [response_headers.add(key, c) for c in cookies]
        else:
            response_headers.add(key, value)

    # If this is a redirect, munge the Location URL
    if "location" in response_headers:
        redirect = response_headers["location"]
        parsed = urlparse(request.url)
        redirect_parsed = urlparse(redirect)

        redirect_host = redirect_parsed.netloc
        if not redirect_host:
            redirect_host = "%s:%d" % (proxy_host, proxy_port)

        redirect_path = redirect_parsed.path
        if redirect_parsed.query:
            redirect_path += "?" + redirect_parsed.query

        munged_path = url_for("proxy_old_api",
                              path=redirect_path[1:])

        url = "%s://%s%s" % (parsed.scheme, parsed.netloc, munged_path)
        response_headers["location"] = url

    contents = resp.read()

    # Restructing Contents.
    if d["content-type"].find("application/json") >= 0:
        kwargs = {'ensure_ascii': False,
                  'cls': JSONEncoder,
                  'indent': 4,
                  'separators': (',', ': ')}
        contents = json.dumps(json.loads(contents), **kwargs)
        root_prefix = "/%s/%s" % (proxy_prefix.strip('/'), path)
        contents = contents.replace(root_prefix, "")

    flask_response = Response(response=contents,
                              status=resp.status,
                              headers=response_headers,
                              content_type=resp.getheader('content-type'))
    return flask_response