author | Gregory Szorc <gregory.szorc@gmail.com> |
Tue, 07 Aug 2018 15:31:03 -0700 | |
changeset 39006 | 5959ef78d834 |
parent 37605 | 74e1362585c0 |
child 41588 | 765a608c2108 |
permissions | -rw-r--r-- |
37605
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 |
"""This was forked from cpython's wsgiref.headers module to work on bytes. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 |
Header from old file showing copyright is below. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 |
Much of this module is red-handedly pilfered from email.message in the stdlib, |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 |
so portions are Copyright (C) 2001,2002 Python Software Foundation, and were |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 |
written by Barry Warsaw. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
8 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
9 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
10 |
# Regular expression that matches `special' characters in parameters, the |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
11 |
# existence of which force quoting of the parameter value. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
12 |
from __future__ import absolute_import, print_function |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
13 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
14 |
import re |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
15 |
tspecials = re.compile(br'[ \(\)<>@,;:\\"/\[\]\?=]') |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
16 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
17 |
def _formatparam(param, value=None, quote=1): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
18 |
"""Convenience function to format and return a key=value pair. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
19 |
This will quote the value if needed or if quote is true. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
20 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
21 |
if value is not None and len(value) > 0: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
22 |
if quote or tspecials.search(value): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
23 |
value = value.replace('\\', '\\\\').replace('"', r'\"') |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
24 |
return '%s="%s"' % (param, value) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
25 |
else: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
26 |
return '%s=%s' % (param, value) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
27 |
else: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
28 |
return param |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
29 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
30 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
31 |
class Headers(object): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
32 |
"""Manage a collection of HTTP response headers""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
33 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
34 |
def __init__(self, headers=None): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
35 |
headers = headers if headers is not None else [] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
36 |
if type(headers) is not list: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
37 |
raise TypeError("Headers must be a list of name/value tuples") |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
38 |
self._headers = headers |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
39 |
if __debug__: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
40 |
for k, v in headers: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
41 |
self._convert_string_type(k) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
42 |
self._convert_string_type(v) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
43 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
44 |
def _convert_string_type(self, value): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
45 |
"""Convert/check value type.""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
46 |
if type(value) is bytes: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
47 |
return value |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
48 |
raise AssertionError(u"Header names/values must be" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
49 |
u" of type bytes (got %s)" % repr(value)) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
50 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
51 |
def __len__(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
52 |
"""Return the total number of headers, including duplicates.""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
53 |
return len(self._headers) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
54 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
55 |
def __setitem__(self, name, val): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
56 |
"""Set the value of a header.""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
57 |
del self[name] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
58 |
self._headers.append( |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
59 |
(self._convert_string_type(name), self._convert_string_type(val))) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
60 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
61 |
def __delitem__(self, name): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
62 |
"""Delete all occurrences of a header, if present. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
63 |
Does *not* raise an exception if the header is missing. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
64 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
65 |
name = self._convert_string_type(name.lower()) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
66 |
self._headers[:] = [kv for kv in self._headers if kv[0].lower() != name] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
67 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
68 |
def __getitem__(self, name): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
69 |
"""Get the first header value for 'name' |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
70 |
Return None if the header is missing instead of raising an exception. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
71 |
Note that if the header appeared multiple times, the first exactly which |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
72 |
occurrence gets returned is undefined. Use getall() to get all |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
73 |
the values matching a header field name. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
74 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
75 |
return self.get(name) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
76 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
77 |
def __contains__(self, name): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
78 |
"""Return true if the message contains the header.""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
79 |
return self.get(name) is not None |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
80 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
81 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
82 |
def get_all(self, name): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
83 |
"""Return a list of all the values for the named field. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
84 |
These will be sorted in the order they appeared in the original header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
85 |
list or were added to this instance, and may contain duplicates. Any |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
86 |
fields deleted and re-inserted are always appended to the header list. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
87 |
If no fields exist with the given name, returns an empty list. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
88 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
89 |
name = self._convert_string_type(name.lower()) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
90 |
return [kv[1] for kv in self._headers if kv[0].lower()==name] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
91 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
92 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
93 |
def get(self, name, default=None): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
94 |
"""Get the first header value for 'name', or return 'default'""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
95 |
name = self._convert_string_type(name.lower()) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
96 |
for k, v in self._headers: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
97 |
if k.lower()==name: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
98 |
return v |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
99 |
return default |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
100 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
101 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
102 |
def keys(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
103 |
"""Return a list of all the header field names. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
104 |
These will be sorted in the order they appeared in the original header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
105 |
list, or were added to this instance, and may contain duplicates. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
106 |
Any fields deleted and re-inserted are always appended to the header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
107 |
list. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
108 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
109 |
return [k for k, v in self._headers] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
110 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
111 |
def values(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
112 |
"""Return a list of all header values. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
113 |
These will be sorted in the order they appeared in the original header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
114 |
list, or were added to this instance, and may contain duplicates. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
115 |
Any fields deleted and re-inserted are always appended to the header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
116 |
list. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
117 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
118 |
return [v for k, v in self._headers] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
119 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
120 |
def items(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
121 |
"""Get all the header fields and values. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
122 |
These will be sorted in the order they were in the original header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
123 |
list, or were added to this instance, and may contain duplicates. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
124 |
Any fields deleted and re-inserted are always appended to the header |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
125 |
list. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
126 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
127 |
return self._headers[:] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
128 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
129 |
def __repr__(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
130 |
return "%s(%r)" % (self.__class__.__name__, self._headers) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
131 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
132 |
def __str__(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
133 |
"""str() returns the formatted headers, complete with end line, |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
134 |
suitable for direct HTTP transmission.""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
135 |
return '\r\n'.join(["%s: %s" % kv for kv in self._headers]+['','']) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
136 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
137 |
def __bytes__(self): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
138 |
return str(self).encode('iso-8859-1') |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
139 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
140 |
def setdefault(self, name, value): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
141 |
"""Return first matching header value for 'name', or 'value' |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
142 |
If there is no header named 'name', add a new header with name 'name' |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
143 |
and value 'value'.""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
144 |
result = self.get(name) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
145 |
if result is None: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
146 |
self._headers.append((self._convert_string_type(name), |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
147 |
self._convert_string_type(value))) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
148 |
return value |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
149 |
else: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
150 |
return result |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
151 |
|
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
152 |
def add_header(self, _name, _value, **_params): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
153 |
"""Extended header setting. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
154 |
_name is the header field to add. keyword arguments can be used to set |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
155 |
additional parameters for the header field, with underscores converted |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
156 |
to dashes. Normally the parameter will be added as key="value" unless |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
157 |
value is None, in which case only the key will be added. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
158 |
Example: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
159 |
h.add_header('content-disposition', 'attachment', filename='bud.gif') |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
160 |
Note that unlike the corresponding 'email.message' method, this does |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
161 |
*not* handle '(charset, language, value)' tuples: all values must be |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
162 |
strings or None. |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
163 |
""" |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
164 |
parts = [] |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
165 |
if _value is not None: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
166 |
_value = self._convert_string_type(_value) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
167 |
parts.append(_value) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
168 |
for k, v in _params.items(): |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
169 |
k = self._convert_string_type(k) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
170 |
if v is None: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
171 |
parts.append(k.replace('_', '-')) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
172 |
else: |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
173 |
v = self._convert_string_type(v) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
174 |
parts.append(_formatparam(k.replace('_', '-'), v)) |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
175 |
self._headers.append( |
74e1362585c0
wsgiheaders: import a bytes-ified fork of wsgiref.headers from cpython@46f5072
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
176 |
(self._convert_string_type(_name), "; ".join(parts))) |