From 254efda1456a707afd36ec8ee505dc1bb76f8a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Thu, 23 Apr 2020 18:11:55 -0400 Subject: support multiple values for header predicate --- src/pyramid/config/routes.py | 3 ++- src/pyramid/config/views.py | 3 ++- src/pyramid/predicates.py | 52 ++++++++++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/pyramid/config/routes.py b/src/pyramid/config/routes.py index 7c78fbfa7..5daa1dc00 100644 --- a/src/pyramid/config/routes.py +++ b/src/pyramid/config/routes.py @@ -220,7 +220,8 @@ class RoutesConfiguratorMixin: header This argument represents an HTTP header name or a header - name/value pair. If the argument contains a ``:`` (colon), + name/value pair, or a sequence of them. + If the argument contains a ``:`` (colon), it will be considered a name/value pair (e.g. ``User-Agent:Mozilla/.*`` or ``Host:localhost``). If the value contains a colon, the value portion should be a diff --git a/src/pyramid/config/views.py b/src/pyramid/config/views.py index 466c31f94..9053160fa 100644 --- a/src/pyramid/config/views.py +++ b/src/pyramid/config/views.py @@ -671,7 +671,8 @@ class ViewsConfiguratorMixin: header This value represents an HTTP header name or a header - name/value pair. If the value contains a ``:`` (colon), it + name/value pair, or a sequence of them. + If the value contains a ``:`` (colon), it will be considered a name/value pair (e.g. ``User-Agent:Mozilla/.*`` or ``Host:localhost``). The value portion should be a regular expression. If the value diff --git a/src/pyramid/predicates.py b/src/pyramid/predicates.py index f51ea3b21..0c74ed6d5 100644 --- a/src/pyramid/predicates.py +++ b/src/pyramid/predicates.py @@ -98,33 +98,43 @@ class RequestParamPredicate: class HeaderPredicate: def __init__(self, val, config): - name = val - v = None - if ':' in name: - name, val_str = name.split(':', 1) - try: - v = re.compile(val_str) - except re.error as why: - raise ConfigurationError(why.args[0]) - if v is None: - self._text = 'header %s' % (name,) - else: - self._text = 'header %s=%s' % (name, val_str) - self.name = name - self.val = v + values = [] + + val = as_sorted_tuple(val) + for name in val: + v, val_str = None, None + if ':' in name: + name, val_str = name.split(':', 1) + try: + v = re.compile(val_str) + except re.error as why: + raise ConfigurationError(why.args[0]) + + values.append((name, v, val_str)) + + self.val = values def text(self): - return self._text + return 'header %s' % ', '.join( + '%s=%s' % (name, val_str) if val_str else name + for name, _, val_str in self.val + ) phash = text def __call__(self, context, request): - if self.val is None: - return self.name in request.headers - val = request.headers.get(self.name) - if val is None: - return False - return self.val.match(val) is not None + for name, val, _ in self.val: + if val is None: + if name not in request.headers: + return False + else: + value = request.headers.get(name) + if value is None: + return False + if val.match(value) is None: + return False + + return True class AcceptPredicate: -- cgit v1.2.3 From c65d2c03174d5a2d66a7a85974b938070ad01a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Tue, 5 May 2020 20:23:49 -0400 Subject: reword doc --- src/pyramid/config/routes.py | 30 ++++++++++++++++-------------- src/pyramid/config/views.py | 33 ++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/pyramid/config/routes.py b/src/pyramid/config/routes.py index 5daa1dc00..7208f5adb 100644 --- a/src/pyramid/config/routes.py +++ b/src/pyramid/config/routes.py @@ -219,24 +219,26 @@ class RoutesConfiguratorMixin: header - This argument represents an HTTP header name or a header - name/value pair, or a sequence of them. - If the argument contains a ``:`` (colon), - it will be considered a name/value pair - (e.g. ``User-Agent:Mozilla/.*`` or ``Host:localhost``). If - the value contains a colon, the value portion should be a - regular expression. If the value does not contain a colon, - the entire value will be considered to be the header name - (e.g. ``If-Modified-Since``). If the value evaluates to a - header name only without a value, the header specified by + This value can be a string or an iterable of strings for HTTP + header names. Any string that does not contain a ``:`` + (colon) will be considered to be the header name (e.g. + ``If-Modified-Since``). In this case, the header specified by the name must be present in the request for this predicate - to be true. If the value evaluates to a header name/value - pair, the header specified by the name must be present in + to be true. + + If a string contains a colon, it will be considered a + name/value pair (e.g. ``User-Agent:Mozilla/.*`` or + ``Host:localhost``), where the value part is a regular + expression. The header specified by the name must be present in the request *and* the regular expression specified as the - value must match the header value. Whether or not the value - represents a header name or a header name/value pair, the + value must match the header value. + + Whether or not the strings + represent a header name or a header name/value pair, the case of the header name is not significant. If this predicate returns ``False``, route matching continues. + When the value is an iterable of strings, they must all + return ``True`` for this predicate to return ``True``. accept diff --git a/src/pyramid/config/views.py b/src/pyramid/config/views.py index 9053160fa..cf1b0a800 100644 --- a/src/pyramid/config/views.py +++ b/src/pyramid/config/views.py @@ -670,23 +670,26 @@ class ViewsConfiguratorMixin: header - This value represents an HTTP header name or a header - name/value pair, or a sequence of them. - If the value contains a ``:`` (colon), it - will be considered a name/value pair - (e.g. ``User-Agent:Mozilla/.*`` or ``Host:localhost``). The - value portion should be a regular expression. If the value - does not contain a colon, the entire value will be - considered to be the header name - (e.g. ``If-Modified-Since``). If the value evaluates to a - header name only without a value, the header specified by + This value can be a string or an iterable of strings for HTTP + header names. Any string that does not contain a ``:`` + (colon) will be considered to be the header name (e.g. + ``If-Modified-Since``). In this case, the header specified by the name must be present in the request for this predicate - to be true. If the value evaluates to a header name/value - pair, the header specified by the name must be present in + to be true. + + If a string contains a colon, it will be considered a + name/value pair (e.g. ``User-Agent:Mozilla/.*`` or + ``Host:localhost``), where the value part is a regular + expression. The header specified by the name must be present in the request *and* the regular expression specified as the - value must match the header value. Whether or not the value - represents a header name or a header name/value pair, the - case of the header name is not significant. + value must match the header value. + + Whether or not the strings + represent a header name or a header name/value pair, the + case of the header name is not significant. If this + predicate returns ``False``, route matching continues. + When the value is an iterable of strings, they must all + return ``True`` for this predicate to return ``True``. path_info -- cgit v1.2.3 From c60c98b5169023f8e7f19eb841a0c72230fa6c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Wed, 6 May 2020 09:57:21 -0400 Subject: more rewording --- src/pyramid/config/routes.py | 36 +++++++++++++++++------------------- src/pyramid/config/views.py | 36 +++++++++++++++++------------------- 2 files changed, 34 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/pyramid/config/routes.py b/src/pyramid/config/routes.py index 7208f5adb..41c7108df 100644 --- a/src/pyramid/config/routes.py +++ b/src/pyramid/config/routes.py @@ -220,25 +220,23 @@ class RoutesConfiguratorMixin: header This value can be a string or an iterable of strings for HTTP - header names. Any string that does not contain a ``:`` - (colon) will be considered to be the header name (e.g. - ``If-Modified-Since``). In this case, the header specified by - the name must be present in the request for this predicate - to be true. - - If a string contains a colon, it will be considered a - name/value pair (e.g. ``User-Agent:Mozilla/.*`` or - ``Host:localhost``), where the value part is a regular - expression. The header specified by the name must be present in - the request *and* the regular expression specified as the - value must match the header value. - - Whether or not the strings - represent a header name or a header name/value pair, the - case of the header name is not significant. If this - predicate returns ``False``, route matching continues. - When the value is an iterable of strings, they must all - return ``True`` for this predicate to return ``True``. + header names. The header names are determined as follow: + + - If a string does not contain a ``:`` (colon), it will be + considered to be the header name (e.g. ``If-Modified-Since``). + In this case, the header specified by the name must be present + in the request for this string to match. Case is not significant. + + - If a string contains a colon, it will be considered a + name/value pair (e.g. ``User-Agent:Mozilla/.*`` or + ``Host:localhost``), where the value part is a regular + expression. The header specified by the name must be present + in the request *and* the regular expression specified as the + value must match the header value. Case is not significant for + the header name, but it is for the value. + + All strings must be matched for this predicate to return ``True``. + If this predicate returns ``False``, route matching continues. accept diff --git a/src/pyramid/config/views.py b/src/pyramid/config/views.py index cf1b0a800..ebef7840d 100644 --- a/src/pyramid/config/views.py +++ b/src/pyramid/config/views.py @@ -671,25 +671,23 @@ class ViewsConfiguratorMixin: header This value can be a string or an iterable of strings for HTTP - header names. Any string that does not contain a ``:`` - (colon) will be considered to be the header name (e.g. - ``If-Modified-Since``). In this case, the header specified by - the name must be present in the request for this predicate - to be true. - - If a string contains a colon, it will be considered a - name/value pair (e.g. ``User-Agent:Mozilla/.*`` or - ``Host:localhost``), where the value part is a regular - expression. The header specified by the name must be present in - the request *and* the regular expression specified as the - value must match the header value. - - Whether or not the strings - represent a header name or a header name/value pair, the - case of the header name is not significant. If this - predicate returns ``False``, route matching continues. - When the value is an iterable of strings, they must all - return ``True`` for this predicate to return ``True``. + header names. The header names are determined as follow: + + - If a string does not contain a ``:`` (colon), it will be + considered to be the header name (e.g. ``If-Modified-Since``). + In this case, the header specified by the name must be present + in the request for this string to match. Case is not significant. + + - If a string contains a colon, it will be considered a + name/value pair (e.g. ``User-Agent:Mozilla/.*`` or + ``Host:localhost``), where the value part is a regular + expression. The header specified by the name must be present + in the request *and* the regular expression specified as the + value must match the header value. Case is not significant for + the header name, but it is for the value. + + All strings must be matched for this predicate to return ``True``. + If this predicate returns ``False``, view matching continues. path_info -- cgit v1.2.3 From f5a8cd2840dc6b0fea4fd7642950b5c731445776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Araujo?= Date: Thu, 7 May 2020 23:52:19 -0400 Subject: better wording --- src/pyramid/config/routes.py | 16 ++++++++-------- src/pyramid/config/views.py | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/pyramid/config/routes.py b/src/pyramid/config/routes.py index 4896ce0ea..a12e18fa8 100644 --- a/src/pyramid/config/routes.py +++ b/src/pyramid/config/routes.py @@ -211,29 +211,29 @@ class RoutesConfiguratorMixin: dictionary (an HTTP ``GET`` or ``POST`` variable) that has a name which matches the supplied value. If the value supplied as the argument has a ``=`` sign in it, - e.g. ``request_param="foo=123"``, then the key - (``foo``) must both exist in the ``request.params`` dictionary, and + e.g. ``request_param="foo=123"``, then both the key + (``foo``) must exist in the ``request.params`` dictionary, and the value must match the right hand side of the expression (``123``) for the route to "match" the current request. If this predicate returns ``False``, route matching continues. header - This value can be a string or an iterable of strings for HTTP - header names. The header names are determined as follow: + This argument can be a string or an iterable of strings for HTTP + headers. The matching is determined as follow: - If a string does not contain a ``:`` (colon), it will be - considered to be the header name (e.g. ``If-Modified-Since``). + considered to be the header name (example ``If-Modified-Since``). In this case, the header specified by the name must be present in the request for this string to match. Case is not significant. - If a string contains a colon, it will be considered a - name/value pair (e.g. ``User-Agent:Mozilla/.*`` or + name/value pair (for example ``User-Agent:Mozilla/.*`` or ``Host:localhost``), where the value part is a regular expression. The header specified by the name must be present in the request *and* the regular expression specified as the - value must match the header value. Case is not significant for - the header name, but it is for the value. + value part must match the value of the request header. Case is + not significant for the header name, but it is for the value. All strings must be matched for this predicate to return ``True``. If this predicate returns ``False``, route matching continues. diff --git a/src/pyramid/config/views.py b/src/pyramid/config/views.py index 170f8a028..a064ebd05 100644 --- a/src/pyramid/config/views.py +++ b/src/pyramid/config/views.py @@ -670,21 +670,21 @@ class ViewsConfiguratorMixin: header - This value can be a string or an iterable of strings for HTTP - header names. The header names are determined as follow: + This argument can be a string or an iterable of strings for HTTP + headers. The matching is determined as follow: - If a string does not contain a ``:`` (colon), it will be - considered to be the header name (e.g. ``If-Modified-Since``). + considered to be a header name (example ``If-Modified-Since``). In this case, the header specified by the name must be present in the request for this string to match. Case is not significant. - If a string contains a colon, it will be considered a - name/value pair (e.g. ``User-Agent:Mozilla/.*`` or + name/value pair (for example ``User-Agent:Mozilla/.*`` or ``Host:localhost``), where the value part is a regular expression. The header specified by the name must be present in the request *and* the regular expression specified as the - value must match the header value. Case is not significant for - the header name, but it is for the value. + value part must match the value of the request header. Case is + not significant for the header name, but it is for the value. All strings must be matched for this predicate to return ``True``. If this predicate returns ``False``, view matching continues. -- cgit v1.2.3