Python werkzeug.datastructures.Headers() Examples

The following are 30 code examples of werkzeug.datastructures.Headers(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module werkzeug.datastructures , or try the search function .
Example #1
Source File: asgi.py    From quart with MIT License 7 votes vote down vote up
def _create_websocket_from_scope(self, send: Callable) -> Websocket:
        headers = Headers()
        headers["Remote-Addr"] = (self.scope.get("client") or ["<local>"])[0]
        for name, value in self.scope["headers"]:
            headers.add(name.decode("latin1").title(), value.decode("latin1"))

        path = self.scope["path"]
        path = path if path[0] == "/" else urlparse(path).path

        return self.app.websocket_class(
            path,
            self.scope["query_string"],
            self.scope["scheme"],
            headers,
            self.scope.get("root_path", ""),
            self.scope.get("http_version", "1.1"),
            self.scope.get("subprotocols", []),
            self.queue.get,
            partial(self.send_data, send),
            partial(self.accept_connection, send),
        ) 
Example #2
Source File: test_ctx.py    From quart with MIT License 6 votes vote down vote up
def test_request_context_match() -> None:
    app = Quart(__name__)
    url_adapter = Mock()
    rule = QuartRule("/", methods={"GET"}, endpoint="index")
    url_adapter.match.return_value = (rule, {"arg": "value"})
    app.create_url_adapter = lambda *_: url_adapter  # type: ignore
    request = Request(
        "GET",
        "http",
        "/",
        b"",
        Headers([("host", "quart.com")]),
        "",
        "1.1",
        send_push_promise=no_op_push,
    )
    RequestContext(app, request)
    assert request.url_rule == rule
    assert request.view_args == {"arg": "value"} 
Example #3
Source File: rss_proxy_server.py    From rssant with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def rss_proxy_handler(request: WerkzeugRequest) -> WerkzeugResponse:
    try:
        data = json.loads(request.data.decode('utf-8'))
        assert data['token'] == _RSS_PROXY_TOKEN
        assert data.get('method') in (None, 'GET', 'POST')
        url = urlparse(data['url'])
        query = _parse_query(url.query)
        assert url.path == '/not-proxy'
        assert HTTPHeaders(data['headers'])['user-agent']
    except Exception as ex:
        LOG.warning(ex, exc_info=ex)
        msg = traceback.format_exception_only(type(ex), ex)
        return WerkzeugResponse(msg, status=400)
    status = query.get('status')
    error = query.get('error')
    if error:
        if error == 'ERROR':
            headers = {'x-rss-proxy-status': 'ERROR'}
            return WerkzeugResponse(str(status), status=200, headers=headers)
        else:
            return WerkzeugResponse(str(status), status=int(error))
    else:
        status = int(status) if status else 200
        headers = {'x-rss-proxy-status': status}
        return WerkzeugResponse(str(status), status=200, headers=headers) 
Example #4
Source File: test_request.py    From quart with MIT License 6 votes vote down vote up
def test_request_exceeds_max_content_length() -> None:
    max_content_length = 5
    headers = Headers()
    headers["Content-Length"] = str(max_content_length + 1)
    request = Request(
        "POST",
        "http",
        "/",
        b"",
        headers,
        "",
        "1.1",
        max_content_length=max_content_length,
        send_push_promise=no_op_push,
    )
    with pytest.raises(RequestEntityTooLarge):
        await request.get_data() 
Example #5
Source File: asgi.py    From quart with MIT License 6 votes vote down vote up
def _create_request_from_scope(self, send: Callable) -> Request:
        headers = Headers()
        headers["Remote-Addr"] = (self.scope.get("client") or ["<local>"])[0]
        for name, value in self.scope["headers"]:
            headers.add(name.decode("latin1").title(), value.decode("latin1"))
        if self.scope["http_version"] < "1.1":
            headers.setdefault("Host", self.app.config["SERVER_NAME"] or "")

        path = self.scope["path"]
        path = path if path[0] == "/" else urlparse(path).path

        return self.app.request_class(
            self.scope["method"],
            self.scope["scheme"],
            path,
            self.scope["query_string"],
            headers,
            self.scope.get("root_path", ""),
            self.scope["http_version"],
            max_content_length=self.app.config["MAX_CONTENT_LENGTH"],
            body_timeout=self.app.config["BODY_TIMEOUT"],
            send_push_promise=partial(self._send_push_promise, send),
            scope=self.scope,
        ) 
Example #6
Source File: test_base.py    From quart with MIT License 6 votes vote down vote up
def test_digest_authorization() -> None:
    headers = Headers()
    headers["Authorization"] = (
        "Digest "
        'username="identity", '
        'realm="realm@rea.lm", '
        'nonce="abcd1234", '
        'uri="/path", '
        'response="abcd1235", '
        'opaque="abcd1236"'
    )
    request = BaseRequestWebsocket("GET", "http", "/", b"", headers, "", "1.1")
    auth = request.authorization
    assert auth.username == "identity"
    assert auth.realm == "realm@rea.lm"
    assert auth.nonce == "abcd1234"
    assert auth.uri == "/path"
    assert auth.response == "abcd1235"
    assert auth.opaque == "abcd1236" 
Example #7
Source File: test_asgi.py    From quart with MIT License 6 votes vote down vote up
def test_websocket_accept_connection(
    scope: dict, headers: Headers, subprotocol: Optional[str], has_headers: bool
) -> None:
    connection = ASGIWebsocketConnection(Quart(__name__), scope)
    mock_send = CoroutineMock()
    await connection.accept_connection(mock_send, headers, subprotocol)

    if has_headers:
        mock_send.assert_called_with(
            {
                "subprotocol": subprotocol,
                "type": "websocket.accept",
                "headers": _encode_headers(headers),
            }
        )
    else:
        mock_send.assert_called_with({"subprotocol": subprotocol, "type": "websocket.accept"}) 
Example #8
Source File: rest_test.py    From ctfscoreboard with Apache License 2.0 6 votes vote down vote up
def testGetSessionWithApiKey(self):
        """Test that an API Key can be used to make requests."""
        key = '41'*16
        headers = datastructures.Headers()
        headers.add('X-SCOREBOARD-API-KEY', key)
        with self.client as c:
            with self.queryLimit(1):
                with mock.patch.object(
                        models.User, 'get_by_api_key') as getter:
                    getter.return_value = self.admin_client.user
                    resp = c.get(self.PATH, headers=headers)
                    getter.assert_called_once_with(key)
            self.assert200(resp)
            self.assertEqual(flask.g.user.email, self.admin_client.user.email)
            self.assertEqual(flask.g.uid, self.admin_client.user.uid)
            self.assertTrue(flask.g.admin)
        self.assertEqual(
                self.admin_client.user.nick,
                resp.json['user']['nick'])
        self.assertTrue(resp.json['user']['admin']) 
Example #9
Source File: test_app.py    From quart with MIT License 6 votes vote down vote up
def test_app_handle_request_asyncio_cancelled_error() -> None:
    app = Quart(__name__)

    @app.route("/")
    async def index() -> NoReturn:
        raise asyncio.CancelledError()

    request = app.request_class(
        "GET",
        "http",
        "/",
        b"",
        Headers([("host", "quart.com")]),
        "",
        "1.1",
        send_push_promise=no_op_push,
    )
    with pytest.raises(asyncio.CancelledError):
        await app.handle_request(request) 
Example #10
Source File: storage.py    From indico-plugins with MIT License 6 votes vote down vote up
def send_file(self, file_id, content_type, filename, inline=True):
        if self.proxy_downloads == ProxyDownloadsMode.local:
            return send_file(filename, self.open(file_id), content_type, inline=inline)

        try:
            bucket, id_ = self._parse_file_id(file_id)
            content_disp = 'inline' if inline else 'attachment'
            h = Headers()
            h.add('Content-Disposition', content_disp, filename=filename)
            url = self.client.generate_presigned_url('get_object',
                                                     Params={'Bucket': bucket,
                                                             'Key': id_,
                                                             'ResponseContentDisposition': h.get('Content-Disposition'),
                                                             'ResponseContentType': content_type},
                                                     ExpiresIn=120)
            response = redirect(url)
            if self.proxy_downloads == ProxyDownloadsMode.nginx:
                # nginx can proxy the request to S3 to avoid exposing the redirect and
                # bucket URL to the end user (since it is quite ugly and temporary)
                response.headers['X-Accel-Redirect'] = '/.xsf/s3/' + url.replace('://', '/', 1)
            return response
        except Exception as e:
            raise StorageError('Could not send file "{}": {}'.format(file_id, e)), None, sys.exc_info()[2] 
Example #11
Source File: test_ctx.py    From quart with MIT License 6 votes vote down vote up
def test_request_context_matching_error(
    exception_type: Exception, exception_instance: Exception
) -> None:
    app = Quart(__name__)
    url_adapter = Mock()
    url_adapter.match.side_effect = exception_instance
    app.create_url_adapter = lambda *_: url_adapter  # type: ignore
    request = Request(
        "GET",
        "http",
        "/",
        b"",
        Headers([("host", "quart.com")]),
        "",
        "1.1",
        send_push_promise=no_op_push,
    )
    RequestContext(app, request)
    assert isinstance(request.routing_exception, exception_type)  # type: ignore 
Example #12
Source File: test_rbac.py    From appkernel with Apache License 2.0 6 votes vote down vote up
def test_auth_decorated_link_good_token_admin_role(client):
    user1 = default_config()
    user2 = User(name='second user', password='second-pass', roles=['user', 'admin'])
    user2.save()
    headers = Headers()
    headers.set('Authorization', 'Bearer {}'.format(user2.auth_token))
    post_data = json.dumps({'current_password': 'some_pass', 'new_password': 'newpass'})
    rsp = client.post('/users/{}/change_password'.format(user1.id), data=post_data, headers=headers)
    print('\nResponse: {} -> {}'.format(rsp.status, rsp.data.decode()))
    assert rsp.status_code == 200, 'should be ok'
    assert rsp.json.get('result') == 'Password changed'

    # for h in rsp.headers:
    #     print h
    # self.assertTrue('WWW-Authenticate' in rv.headers)
    # self.assertTrue('Basic' in rv.headers['WWW-Authenticate']) 
Example #13
Source File: test_ctx.py    From quart with MIT License 6 votes vote down vote up
def test_overlapping_request_ctx() -> None:
    app = Quart(__name__)

    request = Request(
        "GET",
        "http",
        "/",
        b"",
        Headers([("host", "quart.com")]),
        "",
        "1.1",
        send_push_promise=no_op_push,
    )
    ctx1 = app.request_context(request)
    await ctx1.__aenter__()
    ctx2 = app.request_context(request)
    await ctx2.__aenter__()
    await ctx1.__aexit__(None, None, None)
    assert has_app_context()  # Ensure the app context still exists for ctx2
    await ctx2.__aexit__(None, None, None) 
Example #14
Source File: rest_test.py    From ctfscoreboard with Apache License 2.0 6 votes vote down vote up
def testGetSessionWithBadApiKey(self):
        """Test that an API Key with the wrong value does not work."""
        key = '41'*16
        for key in ('41'*16, '41'*18, '41'*15, '55'*16, ''):
            headers = datastructures.Headers()
            headers.add('X-SCOREBOARD-API-KEY', key)
            with self.client as c:
                with self.queryLimit(1):
                    with mock.patch.object(
                            models.User, 'get_by_api_key') as getter:
                        getter.return_value = None
                        resp = c.get(self.PATH, headers=headers)
                        if len(key) == 32:
                            getter.assert_called_once_with(key)
                        else:
                            getter.assert_not_called()
                self.assert403(resp)
                with self.assertRaises(AttributeError):
                    _ = flask.g.user
                self.assertIsNone(flask.g.uid) 
Example #15
Source File: helper.py    From calibre-web with GNU General Public License v3.0 6 votes vote down vote up
def get_download_link(book_id, book_format, client):
    book_format = book_format.split(".")[0]
    book = calibre_db.get_filtered_book(book_id)
    if book:
        data1 = calibre_db.get_book_format(book.id, book_format.upper())
    else:
        abort(404)
    if data1:
        # collect downloaded books only for registered user and not for anonymous user
        if current_user.is_authenticated:
            ub.update_download(book_id, int(current_user.id))
        file_name = book.title
        if len(book.authors) > 0:
            file_name = book.authors[0].name + '_' + file_name
        file_name = get_valid_filename(file_name)
        headers = Headers()
        headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream")
        headers["Content-Disposition"] = "attachment; filename=%s.%s; filename*=UTF-8''%s.%s" % (
            quote(file_name.encode('utf-8')), book_format, quote(file_name.encode('utf-8')), book_format)
        return do_download_file(book, book_format, client, data1, headers)
    else:
        abort(404) 
Example #16
Source File: test_rbac.py    From appkernel with Apache License 2.0 5 votes vote down vote up
def test_auth_explicit_anonymous(client):
    user = default_config()
    user.description = 'A dummy user'
    user.save()
    headers = Headers()
    rsp = client.get('/users/{}/get_description'.format(user.id), headers=headers)
    print('\nResponse: {} -> {}'.format(rsp.status, rsp.data.decode()))
    assert rsp.status_code == 200, 'should be ok'
    assert rsp.json.get('result') == 'A dummy user' 
Example #17
Source File: test_rbac.py    From appkernel with Apache License 2.0 5 votes vote down vote up
def test_auth_decorated_link_good_token_wrong_authority(client):
    user1 = default_config()
    user2 = User(name='second user', password='second-pass', roles=['user'])
    user2.save()
    headers = Headers()
    headers.set('Authorization', 'Bearer {}'.format(user2.auth_token))
    post_data = json.dumps({'current_password': 'some_pass', 'new_password': 'newpass'})
    rsp = client.post('/users/{}/change_password'.format(user1.id), data=post_data, headers=headers)
    print('\nResponse: {} -> {}'.format(rsp.status, rsp.data.decode()))
    assert rsp.status_code == 403, 'should be ok' 
Example #18
Source File: test_rbac.py    From appkernel with Apache License 2.0 5 votes vote down vote up
def test_default_state_with_enabled_security(client):
    user_service = kernel.register(User, methods=['GET', 'PUT', 'POST', 'PATCH', 'DELETE'])
    user_service.deny_all()
    user = create_basic_user()
    user.update(roles=['user', 'admin'])
    headers = Headers()
    headers.set('Authorization', 'Bearer {}'.format(user.auth_token))
    rsp = client.get('/users/{}'.format(user.id), headers=headers)
    print('\nResponse: {} -> {}'.format(rsp.status, rsp.data.decode()))
    assert rsp.status_code == 403, 'should be accepted'
    assert rsp.json.get('message') == 'Not allowed to access method.' 
Example #19
Source File: fixers.py    From jbox with MIT License 5 votes vote down vote up
def run_fixed(self, environ, start_response):
        def fixing_start_response(status, headers, exc_info=None):
            headers = Headers(headers)
            self.fix_headers(environ, headers, status)
            return start_response(status, headers.to_wsgi_list(), exc_info)
        return self.app(environ, fixing_start_response) 
Example #20
Source File: validators.py    From Metis with Apache License 2.0 5 votes vote down vote up
def type_convert(self, obj):
        if obj is None:
            return None
        if isinstance(obj, (dict, list)) and not isinstance(obj, RequestParameters):
            return obj
        if isinstance(obj, Headers):
            obj = MultiDict(obj.items())
        result = dict()

        convert_funs = {
            'integer': lambda v: self.validate_number(int, v[0]),
            'boolean': lambda v: v[0].lower() not in ['n', 'no', 'false', '', '0'],
            'null': lambda v: None,
            'number': lambda v: self.validate_number(float, v[0]),
            'string': lambda v: v[0]
        }

        def convert_array(type_, v):
            func = convert_funs.get(type_, lambda v: v[0])
            return [func([i]) for i in v]

        for k, values in obj.items():
            prop = self.validator.schema['properties'].get(k, {})
            type_ = prop.get('type')
            fun = convert_funs.get(type_, lambda v: v[0])
            if type_ == 'array':
                item_type = prop.get('items', {}).get('type')
                result[k] = convert_array(item_type, values)
            else:
                result[k] = fun(values)
        return result 
Example #21
Source File: validators.py    From Metis with Apache License 2.0 5 votes vote down vote up
def type_convert(self, obj):
        if obj is None:
            return None
        if isinstance(obj, (dict, list)) and not isinstance(obj, RequestParameters):
            return obj
        if isinstance(obj, Headers):
            obj = MultiDict(obj.items())
        result = dict()

        convert_funs = {
            'integer': lambda v: self.validate_number(int, v[0]),
            'boolean': lambda v: v[0].lower() not in ['n', 'no', 'false', '', '0'],
            'null': lambda v: None,
            'number': lambda v: self.validate_number(float, v[0]),
            'string': lambda v: v[0]
        }

        def convert_array(type_, v):
            func = convert_funs.get(type_, lambda v: v[0])
            return [func([i]) for i in v]

        for k, values in obj.items():
            prop = self.validator.schema['properties'].get(k, {})
            type_ = prop.get('type')
            fun = convert_funs.get(type_, lambda v: v[0])
            if type_ == 'array':
                item_type = prop.get('items', {}).get('type')
                result[k] = convert_array(item_type, values)
            else:
                result[k] = fun(values)
        return result 
Example #22
Source File: response.py    From pscheduler with Apache License 2.0 5 votes vote down vote up
def see_other(url):
    log.debug("Response 303: Redirect to %s", url)
    return Response(url + "\n", status=303,
                    headers=Headers([("Location", url)])) 
Example #23
Source File: fixers.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def run_fixed(self, environ, start_response):
        def fixing_start_response(status, headers, exc_info=None):
            headers = Headers(headers)
            self.fix_headers(environ, headers, status)
            return start_response(status, headers.to_wsgi_list(), exc_info)
        return self.app(environ, fixing_start_response) 
Example #24
Source File: lint.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def check_start_response(self, status, headers, exc_info):
        check_string('status', status)
        status_code = status.split(None, 1)[0]
        if len(status_code) != 3 or not status_code.isdigit():
            warn(WSGIWarning('Status code must be three digits'), stacklevel=3)
        if len(status) < 4 or status[3] != ' ':
            warn(WSGIWarning('Invalid value for status %r.  Valid '
                             'status strings are three digits, a space '
                             'and a status explanation'), stacklevel=3)
        status_code = int(status_code)
        if status_code < 100:
            warn(WSGIWarning('status code < 100 detected'), stacklevel=3)

        if type(headers) is not list:
            warn(WSGIWarning('header list is not a list'), stacklevel=3)
        for item in headers:
            if type(item) is not tuple or len(item) != 2:
                warn(WSGIWarning('Headers must tuple 2-item tuples'),
                     stacklevel=3)
            name, value = item
            if type(name) is not str or type(value) is not str:
                warn(WSGIWarning('header items must be strings'),
                     stacklevel=3)
            if name.lower() == 'status':
                warn(WSGIWarning('The status header is not supported due to '
                                 'conflicts with the CGI spec.'),
                     stacklevel=3)

        if exc_info is not None and not isinstance(exc_info, tuple):
            warn(WSGIWarning('invalid value for exc_info'), stacklevel=3)

        headers = Headers(headers)
        self.check_headers(headers)

        return status_code, headers 
Example #25
Source File: formparser.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def parse_multipart_headers(iterable):
    """Parses multipart headers from an iterable that yields lines (including
    the trailing newline symbol).  The iterable has to be newline terminated.

    The iterable will stop at the line where the headers ended so it can be
    further consumed.

    :param iterable: iterable of strings that are newline terminated
    """
    result = []
    for line in iterable:
        line = to_native(line)
        line, line_terminated = _line_parse(line)
        if not line_terminated:
            raise ValueError('unexpected end of line in multipart header')
        if not line:
            break
        elif line[0] in ' \t' and result:
            key, value = result[-1]
            result[-1] = (key, value + '\n ' + line[1:])
        else:
            parts = line.split(':', 1)
            if len(parts) == 2:
                result.append((parts[0].strip(), parts[1].strip()))

    # we link the list to the headers, no need to create a copy, the
    # list was not shared anyways.
    return Headers(result) 
Example #26
Source File: utils.py    From flask-smorest with MIT License 5 votes vote down vote up
def unpack_tuple_response(rv):
    """Unpack a flask Response tuple"""

    status = headers = None

    # unpack tuple returns
    # Unlike Flask, we check exact type because tuple subclasses may be
    # returned by view functions and paginated/dumped
    if type(rv) is tuple:  # pylint: disable=unidiomatic-typecheck
        len_rv = len(rv)

        # a 3-tuple is unpacked directly
        if len_rv == 3:
            rv, status, headers = rv
        # decide if a 2-tuple has status or headers
        elif len_rv == 2:
            if isinstance(rv[1], (Headers, dict, tuple, list)):
                rv, headers = rv
            else:
                rv, status = rv
        # other sized tuples are not allowed
        else:
            raise TypeError(
                'The view function did not return a valid response tuple.'
                ' The tuple must have the form (body, status, headers),'
                ' (body, status), or (body, headers).'
            )

    return rv, status, headers 
Example #27
Source File: fixers.py    From lambda-packs with MIT License 5 votes vote down vote up
def run_fixed(self, environ, start_response):
        def fixing_start_response(status, headers, exc_info=None):
            headers = Headers(headers)
            self.fix_headers(environ, headers, status)
            return start_response(status, headers.to_wsgi_list(), exc_info)
        return self.app(environ, fixing_start_response) 
Example #28
Source File: lint.py    From lambda-packs with MIT License 5 votes vote down vote up
def check_start_response(self, status, headers, exc_info):
        check_string('status', status)
        status_code = status.split(None, 1)[0]
        if len(status_code) != 3 or not status_code.isdigit():
            warn(WSGIWarning('Status code must be three digits'), stacklevel=3)
        if len(status) < 4 or status[3] != ' ':
            warn(WSGIWarning('Invalid value for status %r.  Valid '
                             'status strings are three digits, a space '
                             'and a status explanation'), stacklevel=3)
        status_code = int(status_code)
        if status_code < 100:
            warn(WSGIWarning('status code < 100 detected'), stacklevel=3)

        if type(headers) is not list:
            warn(WSGIWarning('header list is not a list'), stacklevel=3)
        for item in headers:
            if type(item) is not tuple or len(item) != 2:
                warn(WSGIWarning('Headers must tuple 2-item tuples'),
                     stacklevel=3)
            name, value = item
            if type(name) is not str or type(value) is not str:
                warn(WSGIWarning('header items must be strings'),
                     stacklevel=3)
            if name.lower() == 'status':
                warn(WSGIWarning('The status header is not supported due to '
                                 'conflicts with the CGI spec.'),
                     stacklevel=3)

        if exc_info is not None and not isinstance(exc_info, tuple):
            warn(WSGIWarning('invalid value for exc_info'), stacklevel=3)

        headers = Headers(headers)
        self.check_headers(headers)

        return status_code, headers 
Example #29
Source File: formparser.py    From lambda-packs with MIT License 5 votes vote down vote up
def parse_multipart_headers(iterable):
    """Parses multipart headers from an iterable that yields lines (including
    the trailing newline symbol).  The iterable has to be newline terminated.

    The iterable will stop at the line where the headers ended so it can be
    further consumed.

    :param iterable: iterable of strings that are newline terminated
    """
    result = []
    for line in iterable:
        line = to_native(line)
        line, line_terminated = _line_parse(line)
        if not line_terminated:
            raise ValueError('unexpected end of line in multipart header')
        if not line:
            break
        elif line[0] in ' \t' and result:
            key, value = result[-1]
            result[-1] = (key, value + '\n ' + line[1:])
        else:
            parts = line.split(':', 1)
            if len(parts) == 2:
                result.append((parts[0].strip(), parts[1].strip()))

    # we link the list to the headers, no need to create a copy, the
    # list was not shared anyways.
    return Headers(result) 
Example #30
Source File: fixers.py    From Flask-P2P with MIT License 5 votes vote down vote up
def run_fixed(self, environ, start_response):
        def fixing_start_response(status, headers, exc_info=None):
            headers = Headers(headers)
            self.fix_headers(environ, headers, status)
            return start_response(status, headers.to_wsgi_list(), exc_info)
        return self.app(environ, fixing_start_response)