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