Exemple #1
0
def parse_set_header(value, on_update=None):
    """Parse a set-like header and return a
    :class:`~werkzeug.datastructures.HeaderSet` object:

    >>> hs = parse_set_header('token, "quoted value"')

    The return value is an object that treats the items case-insensitively
    and keeps the order of the items:

    >>> 'TOKEN' in hs
    True
    >>> hs.index('quoted value')
    1
    >>> hs
    HeaderSet(['token', 'quoted value'])

    To create a header from the :class:`HeaderSet` again, use the
    :func:`dump_header` function.

    :param value: a set header to be parsed.
    :param on_update: an optional callable that is called every time a
                      value on the :class:`~werkzeug.datastructures.HeaderSet`
                      object is changed.
    :return: a :class:`~werkzeug.datastructures.HeaderSet`
    """
    if not value:
        return HeaderSet(None, on_update)
    return HeaderSet(parse_list_header(value), on_update)
    def test_metadata_datastructure(self):
        def check_metadata(m):
            self.assertEquals(m.salt, 'qux')
            self.assertIn('foo', m.vary)
            self.assertIn('bar', m.vary)

        m = Metadata(HeaderSet(('foo', 'bar')), 'qux')
        check_metadata(m)
        check_metadata(loads(dumps(m)))
        m2 = Metadata(HeaderSet(('foo', 'bar')), 'qux')
        self.assertEquals(m, m2)
        m3 = Metadata(HeaderSet(('foo', 'bar')), 'notqux')
        self.assertNotEquals(m2, m3)
def parse_set_header(value, on_update=None):
    """Parse a set-like header and return a :class:`HeaderSet` object.  The
    return value is an object that treats the items case-insensitively and
    keeps the order of the items.

    :param value: a set header to be parsed.
    :param on_update: an optional callable that is called every time a
                      value on the :class:`HeaderSet` object is changed.
    :return: a :class:`HeaderSet`
    """
    if not value:
        return HeaderSet(None, on_update)
    return HeaderSet(parse_list_header(value), on_update)
Exemple #4
0
def _sanitise_header_set(value: Optional[Union[str, Iterable[str]]],
                         config_key: str) -> HeaderSet:
    if value is None:
        value = _get_config_or_default(config_key)
    elif isinstance(value, str):
        value = [value]
    return HeaderSet(value)
Exemple #5
0
def _apply_cors(
    request_origin: Optional[str],
    request_headers: Optional[HeaderSet],
    request_method: Optional[str],
    method: str,
    response: Response,
    *,
    allow_credentials: bool,
    allow_headers: HeaderSet,
    allow_methods: HeaderSet,
    allow_origin: Set[OriginType],
    expose_headers: HeaderSet,
    max_age: Optional[int],
) -> Response:
    # Logic follows https://www.w3.org/TR/cors/
    if "*" in allow_origin and allow_credentials:
        raise ValueError(
            "Cannot allow credentials with wildcard allowed origins")
    if getattr(response, "_QUART_CORS_APPLIED", False):
        return response
    origin = _get_origin_if_valid(request_origin, allow_origin)
    if origin is not None:
        response.access_control_allow_origin = origin
        response.access_control_allow_credentials = allow_credentials
        response.access_control_expose_headers = expose_headers
        if method == "OPTIONS" and request_method in allow_methods or "*" in allow_methods:
            if request_headers is None:
                request_headers = HeaderSet()

            if "*" in allow_headers:
                response.access_control_allow_headers = request_headers

            else:
                response.access_control_allow_headers = HeaderSet(
                    set(allow_headers).intersection(set(request_headers)))
            response.access_control_allow_methods = allow_methods
            if max_age is not None:
                response.access_control_max_age = max_age
        if "*" not in origin:
            response.vary.add("Origin")
    setattr(response, "_QUART_CORS_APPLIED", True)
    return response
    def test_preemptive_recaching_predicate(self):
        m = Metadata(HeaderSet(('foo', 'bar')), 'qux')

        def mkretr(**kwargs):
            return Retrieval(self.c, Config(**kwargs))

        with a.test_request_context('/'):
            self.assertFalse(
                mkretr(
                    preemptive_recache_seconds=10).should_recache_preemptively(
                        10, m))
            self.assertFalse(
                mkretr(preemptive_recache_callback=lambda x: 0).
                should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(11, m))
            self.assertTrue(self.r.should_recache_preemptively(10, m))
            self.assertFalse(self.r.should_recache_preemptively(10, m))
            self.c.clear()
            self.assertTrue(self.r.should_recache_preemptively(10, m))
Exemple #7
0
 def vary(self, value: HeaderSet) -> None:
     self._set_or_pop_header("Vary", value.to_header())
Exemple #8
0
 def content_language(self, value: HeaderSet) -> None:
     self._set_or_pop_header("Content-Language", value.to_header())
Exemple #9
0
 def allow(self, value: HeaderSet) -> None:
     self._set_or_pop_header("Allow", value.to_header())
Exemple #10
0
def test_header_set():
    """Test the header set"""
    hs = HeaderSet()
    hs.add("foo")
    hs.add("bar")
    assert "Bar" in hs
    assert hs.find("foo") == 0
    assert hs.find("BAR") == 1
    assert hs.find("baz") < 0
    hs.discard("missing")
    hs.discard("foo")
    assert hs.find("foo") < 0
    assert hs.find("bar") == 0
    assert_raises(IndexError, hs.index, "missing")
    assert hs.index("bar") == 0
    assert hs
    hs.clear()
    assert not hs
Exemple #11
0
def parse_set_header(value, on_update = None):
    if not value:
        return HeaderSet(None, on_update)
    return HeaderSet(parse_list_header(value), on_update)
Exemple #12
0
def test_header_set():
    """Test the header set"""
    hs = HeaderSet()
    hs.add('foo')
    hs.add('bar')
    assert 'Bar' in hs
    assert hs.find('foo') == 0
    assert hs.find('BAR') == 1
    assert hs.find('baz') < 0
    hs.discard('missing')
    hs.discard('foo')
    assert hs.find('foo') < 0
    assert hs.find('bar') == 0
    assert_raises(IndexError, hs.index, 'missing')
    assert hs.index('bar') == 0
    assert hs
    hs.clear()
    assert not hs
def test_header_set():
    """Test the header set"""
    hs = HeaderSet()
    hs.add('foo')
    hs.add('bar')
    assert 'Bar' in hs
    assert hs.find('foo') == 0
    assert hs.find('BAR') == 1
    assert hs.find('baz') < 0
    hs.discard('missing')
    hs.discard('foo')
    assert hs.find('foo') < 0
    assert hs.find('bar') == 0
    assert_raises(IndexError, hs.index, 'missing')
    assert hs.index('bar') == 0
    assert hs
    hs.clear()
    assert not hs