summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2020-04-23 18:11:55 -0400
committerÉric Araujo <merwok@netwok.org>2020-05-04 17:43:39 -0400
commit254efda1456a707afd36ec8ee505dc1bb76f8a77 (patch)
tree8558e494b4325b1a324c49977c4b9341dedeffc2 /src
parent7dd166b158594dec3e222bf041cdfd806f633f88 (diff)
downloadpyramid-254efda1456a707afd36ec8ee505dc1bb76f8a77.tar.gz
pyramid-254efda1456a707afd36ec8ee505dc1bb76f8a77.tar.bz2
pyramid-254efda1456a707afd36ec8ee505dc1bb76f8a77.zip
support multiple values for header predicate
Diffstat (limited to 'src')
-rw-r--r--src/pyramid/config/routes.py3
-rw-r--r--src/pyramid/config/views.py3
-rw-r--r--src/pyramid/predicates.py52
3 files changed, 35 insertions, 23 deletions
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: