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