示例#1
0
    def test_remove_hop_by_hop_headers(self):
        headers1 = [("Connection", "closed"), ("Foo", "bar"), ("Keep-Alive", "wtf")]
        headers2 = datastructures.Headers(headers1)

        http.remove_hop_by_hop_headers(headers1)
        assert headers1 == [("Foo", "bar")]

        http.remove_hop_by_hop_headers(headers2)
        assert headers2 == datastructures.Headers([("Foo", "bar")])
示例#2
0
    def test_remove_hop_by_hop_headers(self):
        headers1 = [("Connection", "closed"), ("Foo", "bar"), ("Keep-Alive", "wtf")]
        headers2 = datastructures.Headers(headers1)

        http.remove_hop_by_hop_headers(headers1)
        assert headers1 == [("Foo", "bar")]

        http.remove_hop_by_hop_headers(headers2)
        assert headers2 == datastructures.Headers([("Foo", "bar")])
示例#3
0
    def test_remove_hop_by_hop_headers(self):
        headers1 = [('Connection', 'closed'), ('Foo', 'bar'),
                    ('Keep-Alive', 'wtf')]
        headers2 = datastructures.Headers(headers1)

        http.remove_hop_by_hop_headers(headers1)
        assert headers1 == [('Foo', 'bar')]

        http.remove_hop_by_hop_headers(headers2)
        assert headers2 == datastructures.Headers([('Foo', 'bar')])
示例#4
0
    def test_remove_hop_by_hop_headers(self):
        headers1 = [('Connection', 'closed'), ('Foo', 'bar'),
                    ('Keep-Alive', 'wtf')]
        headers2 = datastructures.Headers(headers1)

        http.remove_hop_by_hop_headers(headers1)
        assert headers1 == [('Foo', 'bar')]

        http.remove_hop_by_hop_headers(headers2)
        assert headers2 == datastructures.Headers([('Foo', 'bar')])
示例#5
0
def passthrough(payload=None,
                params=None,
                raise_exceptions=True,
                stream=False,
                final=False):

    # Some tests want to assert that we don't pass-through the request.
    # This asserts that.
    if g.pragmas.get('deny_passthrough'):
        raise Fault('passthrough denied via request pragmas')

    # Remove headers which we should not pass on.
    headers = [(k, v) for k, v in request.headers.iteritems()
               if k.lower() != 'host']
    remove_hop_by_hop_headers(headers)
    headers = dict(headers)

    if payload is None:
        payload = copy.deepcopy(g.api3_payload)
    if params is not None:
        payload['params'][-1] = params

    if not isinstance(payload, basestring):
        payload = json.dumps(payload)

    http_response = current_app.http_session.post(
        current_app.config['PASSTHROUGH_URL'],
        data=payload,
        headers=headers,
        stream=stream)

    if http_response.status_code != 200:
        raise ReturnPassthroughError(
            Response(
                http_response.iter_content(8192)
                if stream else http_response.text,
                http_response.status_code,
                mimetype='application/json',
            ))

    if stream or final:
        return Response(
            http_response.iter_content(8192) if stream else http_response.text,
            200,
            mimetype='application/json')

    else:
        response_data = json.loads(http_response.text)
        if raise_exceptions and isinstance(
                response_data, dict) and 'exception' in response_data:
            raise ReturnResponse(http_response.text, 200,
                                 [('Content-Type', 'application/json')])
        else:
            return response_data
示例#6
0
文件: core.py 项目: westernx/sgcache
def passthrough(payload=None, params=None, raise_exceptions=True, stream=False, final=False):

    # Some tests want to assert that we don't pass-through the request.
    # This asserts that.
    if g.pragmas.get('deny_passthrough'):
        raise Fault('passthrough denied via request pragmas')

    # Remove headers which we should not pass on.
    headers = [(k, v) for k, v in request.headers.iteritems() if k.lower() != 'host']
    remove_hop_by_hop_headers(headers)
    headers = dict(headers)

    if payload is None:
        payload = copy.deepcopy(g.api3_payload)
    if params is not None:
        payload['params'][-1] = params

    if not isinstance(payload, basestring):
        payload = json.dumps(payload)

    http_response = current_app.http_session.post(current_app.config['PASSTHROUGH_URL'], data=payload, headers=headers, stream=stream)

    if http_response.status_code != 200:
        raise ReturnPassthroughError(Response(
            http_response.iter_content(8192) if stream else http_response.text,
            http_response.status_code,
            mimetype='application/json',
        ))

    if stream or final:
        return Response(
            http_response.iter_content(8192) if stream else http_response.text,
            200,
            mimetype='application/json'
        )

    else:
        response_data = json.loads(http_response.text)
        if raise_exceptions and isinstance(response_data, dict) and 'exception' in response_data:
            raise ReturnResponse(http_response.text, 200, [('Content-Type', 'application/json')])
        else:
            return response_data
示例#7
0
def proxy(path):

    url = current_app.config['PASSTHROUGH_SERVER'] + request.path

    # Strip out the hop-by-hop headers, AND the host (since that likely points
    # to the cache and not Shotgun).
    headers = [(k, v) for k, v in request.headers.items()
               if k.lower() != 'host']
    remove_hop_by_hop_headers(headers)

    start_time = time.time()
    if request.method == 'POST':
        log.info('Proxying %s %s' % (request.method, request.path))

    remote_response = current_app.http_session.request(
        request.method,
        url,
        data=_StreamReadWrapper(request.stream),
        params=request.args,
        headers=dict(headers),
        stream=True,
    )

    headers = remote_response.headers.items()
    remove_hop_by_hop_headers(headers)

    elapsed_ms = 1000 * (time.time() - start_time)
    log.info('Proxy %s %s returned %s in %.1fms' % (
        request.method,
        request.path,
        remote_response.status_code,
        elapsed_ms,
    ))

    return Response(
        remote_response.iter_content(8192),
        status=remote_response.status_code,
        headers=headers,
        direct_passthrough=True,  # Don't encode it.
    )
示例#8
0
文件: core.py 项目: westernx/sgcache
def proxy(path):

    url = current_app.config['PASSTHROUGH_SERVER'] + request.path

    # Strip out the hop-by-hop headers, AND the host (since that likely points
    # to the cache and not Shotgun).
    headers = [(k, v) for k, v in request.headers.items() if k.lower() != 'host']
    remove_hop_by_hop_headers(headers)

    start_time = time.time()
    if request.method == 'POST':
        log.info('Proxying %s %s' % (request.method, request.path))

    remote_response = current_app.http_session.request(request.method, url,
        data=_StreamReadWrapper(request.stream),
        params=request.args,
        headers=dict(headers),
        stream=True,
    )

    headers = remote_response.headers.items()
    remove_hop_by_hop_headers(headers)

    elapsed_ms = 1000 * (time.time() - start_time)
    log.info('Proxy %s %s returned %s in %.1fms' % (
        request.method,
        request.path,
        remote_response.status_code,
        elapsed_ms,
    ))

    return Response(
        remote_response.iter_content(8192),
        status=remote_response.status_code,
        headers=headers,
        direct_passthrough=True, # Don't encode it.
    )