/usr/share/pyshared/twisted/web/http_headers.py is in python-twisted-web 12.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | # -*- test-case-name: twisted.web.test.test_http_headers
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
An API for storing HTTP header names and values.
"""
from UserDict import DictMixin
def _dashCapitalize(name):
"""
Return a string which is capitalized using '-' as a word separator.
@param name: The name of the header to capitalize.
@type name: str
@return: The given header capitalized using '-' as a word separator.
@rtype: str
"""
return '-'.join([word.capitalize() for word in name.split('-')])
class _DictHeaders(DictMixin):
"""
A C{dict}-like wrapper around L{Headers} to provide backwards compatibility
for L{Request.received_headers} and L{Request.headers} which used to be
plain C{dict} instances.
@type _headers: L{Headers}
@ivar _headers: The real header storage object.
"""
def __init__(self, headers):
self._headers = headers
def __getitem__(self, key):
"""
Return the last value for header of C{key}.
"""
if self._headers.hasHeader(key):
return self._headers.getRawHeaders(key)[-1]
raise KeyError(key)
def __setitem__(self, key, value):
"""
Set the given header.
"""
self._headers.setRawHeaders(key, [value])
def __delitem__(self, key):
"""
Delete the given header.
"""
if self._headers.hasHeader(key):
self._headers.removeHeader(key)
else:
raise KeyError(key)
def keys(self):
"""
Return a list of all header names.
"""
return [k.lower() for k, v in self._headers.getAllRawHeaders()]
def copy(self):
"""
Return a C{dict} mapping each header name to the last corresponding
header value.
"""
return dict(self.items())
# Python 2.3 DictMixin.setdefault is defined so as not to have a default
# for the value parameter. This is necessary to make this setdefault look
# like dict.setdefault on Python 2.3. -exarkun
def setdefault(self, name, value=None):
"""
Retrieve the last value for the given header name. If there are no
values present for that header, set the value to C{value} and return
that instead. Note that C{None} is the default for C{value} for
backwards compatibility, but header values may only be of type C{str}.
"""
return DictMixin.setdefault(self, name, value)
# The remaining methods are only for efficiency. The same behavior
# should remain even if they are removed. For details, see
# <http://docs.python.org/lib/module-UserDict.html>.
# -exarkun
def __contains__(self, name):
"""
Return C{True} if the named header is present, C{False} otherwise.
"""
return self._headers.getRawHeaders(name) is not None
def __iter__(self):
"""
Return an iterator of the lowercase name of each header present.
"""
for k, v in self._headers.getAllRawHeaders():
yield k.lower()
def iteritems(self):
"""
Return an iterable of two-tuples of each lower-case header name and the
last value for that header.
"""
for k, v in self._headers.getAllRawHeaders():
yield k.lower(), v[-1]
class Headers(object):
"""
This class stores the HTTP headers as both a parsed representation
and the raw string representation. It converts between the two on
demand.
@cvar _caseMappings: A C{dict} that maps lowercase header names
to their canonicalized representation.
@ivar _rawHeaders: A C{dict} mapping header names as C{str} to C{lists} of
header values as C{str}.
"""
_caseMappings = {
'content-md5': 'Content-MD5',
'dnt': 'DNT',
'etag': 'ETag',
'p3p': 'P3P',
'te': 'TE',
'www-authenticate': 'WWW-Authenticate',
'x-xss-protection': 'X-XSS-Protection'}
def __init__(self, rawHeaders=None):
self._rawHeaders = {}
if rawHeaders is not None:
for name, values in rawHeaders.iteritems():
self.setRawHeaders(name, values[:])
def __repr__(self):
"""
Return a string fully describing the headers set on this object.
"""
return '%s(%r)' % (self.__class__.__name__, self._rawHeaders,)
def __cmp__(self, other):
"""
Define L{Headers} instances as being equal to each other if they have
the same raw headers.
"""
if isinstance(other, Headers):
return cmp(self._rawHeaders, other._rawHeaders)
return NotImplemented
def copy(self):
"""
Return a copy of itself with the same headers set.
"""
return self.__class__(self._rawHeaders)
def hasHeader(self, name):
"""
Check for the existence of a given header.
@type name: C{str}
@param name: The name of the HTTP header to check for.
@rtype: C{bool}
@return: C{True} if the header exists, otherwise C{False}.
"""
return name.lower() in self._rawHeaders
def removeHeader(self, name):
"""
Remove the named header from this header object.
@type name: C{str}
@param name: The name of the HTTP header to remove.
@return: C{None}
"""
self._rawHeaders.pop(name.lower(), None)
def setRawHeaders(self, name, values):
"""
Sets the raw representation of the given header.
@type name: C{str}
@param name: The name of the HTTP header to set the values for.
@type values: C{list}
@param values: A list of strings each one being a header value of
the given name.
@return: C{None}
"""
if not isinstance(values, list):
raise TypeError("Header entry %r should be list but found "
"instance of %r instead" % (name, type(values)))
self._rawHeaders[name.lower()] = values
def addRawHeader(self, name, value):
"""
Add a new raw value for the given header.
@type name: C{str}
@param name: The name of the header for which to set the value.
@type value: C{str}
@param value: The value to set for the named header.
"""
values = self.getRawHeaders(name)
if values is None:
self.setRawHeaders(name, [value])
else:
values.append(value)
def getRawHeaders(self, name, default=None):
"""
Returns a list of headers matching the given name as the raw string
given.
@type name: C{str}
@param name: The name of the HTTP header to get the values of.
@param default: The value to return if no header with the given C{name}
exists.
@rtype: C{list}
@return: A C{list} of values for the given header.
"""
return self._rawHeaders.get(name.lower(), default)
def getAllRawHeaders(self):
"""
Return an iterator of key, value pairs of all headers contained in this
object, as strings. The keys are capitalized in canonical
capitalization.
"""
for k, v in self._rawHeaders.iteritems():
yield self._canonicalNameCaps(k), v
def _canonicalNameCaps(self, name):
"""
Return the canonical name for the given header.
@type name: C{str}
@param name: The all-lowercase header name to capitalize in its
canonical form.
@rtype: C{str}
@return: The canonical name of the header.
"""
return self._caseMappings.get(name, _dashCapitalize(name))
__all__ = ['Headers']
|