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