diff options
| author | Michael Merickel <michael@merickel.org> | 2020-05-08 00:22:55 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-08 00:22:55 -0500 |
| commit | 683bc742f7b08db563f3385796809c09babf004b (patch) | |
| tree | 9f3ced848d13003209173369a6f0f4431473d8d2 /src | |
| parent | 1722cbb7bf40ce9d3793faf38f63e9f00d577613 (diff) | |
| parent | f5a8cd2840dc6b0fea4fd7642950b5c731445776 (diff) | |
| download | pyramid-683bc742f7b08db563f3385796809c09babf004b.tar.gz pyramid-683bc742f7b08db563f3385796809c09babf004b.tar.bz2 pyramid-683bc742f7b08db563f3385796809c09babf004b.zip | |
Merge pull request #3576 from merwok/feature/multi-predicate-header
Allow multiple values for header predicate
Diffstat (limited to 'src')
| -rw-r--r-- | src/pyramid/config/routes.py | 39 | ||||
| -rw-r--r-- | src/pyramid/config/views.py | 34 | ||||
| -rw-r--r-- | src/pyramid/predicates.py | 52 |
3 files changed, 69 insertions, 56 deletions
diff --git a/src/pyramid/config/routes.py b/src/pyramid/config/routes.py index 9452a05ab..a12e18fa8 100644 --- a/src/pyramid/config/routes.py +++ b/src/pyramid/config/routes.py @@ -211,31 +211,32 @@ 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 argument represents an HTTP header name or a header - name/value pair. 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 - 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 - 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. If this - predicate returns ``False``, route matching continues. + 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 (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 (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 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. accept diff --git a/src/pyramid/config/views.py b/src/pyramid/config/views.py index 6faa29d20..a064ebd05 100644 --- a/src/pyramid/config/views.py +++ b/src/pyramid/config/views.py @@ -670,22 +670,24 @@ class ViewsConfiguratorMixin: header - This value represents an HTTP header name or a header - name/value pair. 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 - 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 - 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. + 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 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 (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 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. path_info diff --git a/src/pyramid/predicates.py b/src/pyramid/predicates.py index 8b443e79b..576bbbce6 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: |
