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