Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../wsgiref
File: util.py
"""Miscellaneous WSGI-related Utilities"""
[0] Fix | Delete
[1] Fix | Delete
import posixpath
[2] Fix | Delete
[3] Fix | Delete
__all__ = [
[4] Fix | Delete
'FileWrapper', 'guess_scheme', 'application_uri', 'request_uri',
[5] Fix | Delete
'shift_path_info', 'setup_testing_defaults',
[6] Fix | Delete
]
[7] Fix | Delete
[8] Fix | Delete
[9] Fix | Delete
class FileWrapper:
[10] Fix | Delete
"""Wrapper to convert file-like objects to iterables"""
[11] Fix | Delete
[12] Fix | Delete
def __init__(self, filelike, blksize=8192):
[13] Fix | Delete
self.filelike = filelike
[14] Fix | Delete
self.blksize = blksize
[15] Fix | Delete
if hasattr(filelike,'close'):
[16] Fix | Delete
self.close = filelike.close
[17] Fix | Delete
[18] Fix | Delete
def __getitem__(self,key):
[19] Fix | Delete
data = self.filelike.read(self.blksize)
[20] Fix | Delete
if data:
[21] Fix | Delete
return data
[22] Fix | Delete
raise IndexError
[23] Fix | Delete
[24] Fix | Delete
def __iter__(self):
[25] Fix | Delete
return self
[26] Fix | Delete
[27] Fix | Delete
def next(self):
[28] Fix | Delete
data = self.filelike.read(self.blksize)
[29] Fix | Delete
if data:
[30] Fix | Delete
return data
[31] Fix | Delete
raise StopIteration
[32] Fix | Delete
[33] Fix | Delete
def guess_scheme(environ):
[34] Fix | Delete
"""Return a guess for whether 'wsgi.url_scheme' should be 'http' or 'https'
[35] Fix | Delete
"""
[36] Fix | Delete
if environ.get("HTTPS") in ('yes','on','1'):
[37] Fix | Delete
return 'https'
[38] Fix | Delete
else:
[39] Fix | Delete
return 'http'
[40] Fix | Delete
[41] Fix | Delete
def application_uri(environ):
[42] Fix | Delete
"""Return the application's base URI (no PATH_INFO or QUERY_STRING)"""
[43] Fix | Delete
url = environ['wsgi.url_scheme']+'://'
[44] Fix | Delete
from urllib import quote
[45] Fix | Delete
[46] Fix | Delete
if environ.get('HTTP_HOST'):
[47] Fix | Delete
url += environ['HTTP_HOST']
[48] Fix | Delete
else:
[49] Fix | Delete
url += environ['SERVER_NAME']
[50] Fix | Delete
[51] Fix | Delete
if environ['wsgi.url_scheme'] == 'https':
[52] Fix | Delete
if environ['SERVER_PORT'] != '443':
[53] Fix | Delete
url += ':' + environ['SERVER_PORT']
[54] Fix | Delete
else:
[55] Fix | Delete
if environ['SERVER_PORT'] != '80':
[56] Fix | Delete
url += ':' + environ['SERVER_PORT']
[57] Fix | Delete
[58] Fix | Delete
url += quote(environ.get('SCRIPT_NAME') or '/')
[59] Fix | Delete
return url
[60] Fix | Delete
[61] Fix | Delete
def request_uri(environ, include_query=1):
[62] Fix | Delete
"""Return the full request URI, optionally including the query string"""
[63] Fix | Delete
url = application_uri(environ)
[64] Fix | Delete
from urllib import quote
[65] Fix | Delete
path_info = quote(environ.get('PATH_INFO',''),safe='/;=,')
[66] Fix | Delete
if not environ.get('SCRIPT_NAME'):
[67] Fix | Delete
url += path_info[1:]
[68] Fix | Delete
else:
[69] Fix | Delete
url += path_info
[70] Fix | Delete
if include_query and environ.get('QUERY_STRING'):
[71] Fix | Delete
url += '?' + environ['QUERY_STRING']
[72] Fix | Delete
return url
[73] Fix | Delete
[74] Fix | Delete
def shift_path_info(environ):
[75] Fix | Delete
"""Shift a name from PATH_INFO to SCRIPT_NAME, returning it
[76] Fix | Delete
[77] Fix | Delete
If there are no remaining path segments in PATH_INFO, return None.
[78] Fix | Delete
Note: 'environ' is modified in-place; use a copy if you need to keep
[79] Fix | Delete
the original PATH_INFO or SCRIPT_NAME.
[80] Fix | Delete
[81] Fix | Delete
Note: when PATH_INFO is just a '/', this returns '' and appends a trailing
[82] Fix | Delete
'/' to SCRIPT_NAME, even though empty path segments are normally ignored,
[83] Fix | Delete
and SCRIPT_NAME doesn't normally end in a '/'. This is intentional
[84] Fix | Delete
behavior, to ensure that an application can tell the difference between
[85] Fix | Delete
'/x' and '/x/' when traversing to objects.
[86] Fix | Delete
"""
[87] Fix | Delete
path_info = environ.get('PATH_INFO','')
[88] Fix | Delete
if not path_info:
[89] Fix | Delete
return None
[90] Fix | Delete
[91] Fix | Delete
path_parts = path_info.split('/')
[92] Fix | Delete
path_parts[1:-1] = [p for p in path_parts[1:-1] if p and p != '.']
[93] Fix | Delete
name = path_parts[1]
[94] Fix | Delete
del path_parts[1]
[95] Fix | Delete
[96] Fix | Delete
script_name = environ.get('SCRIPT_NAME','')
[97] Fix | Delete
script_name = posixpath.normpath(script_name+'/'+name)
[98] Fix | Delete
if script_name.endswith('/'):
[99] Fix | Delete
script_name = script_name[:-1]
[100] Fix | Delete
if not name and not script_name.endswith('/'):
[101] Fix | Delete
script_name += '/'
[102] Fix | Delete
[103] Fix | Delete
environ['SCRIPT_NAME'] = script_name
[104] Fix | Delete
environ['PATH_INFO'] = '/'.join(path_parts)
[105] Fix | Delete
[106] Fix | Delete
# Special case: '/.' on PATH_INFO doesn't get stripped,
[107] Fix | Delete
# because we don't strip the last element of PATH_INFO
[108] Fix | Delete
# if there's only one path part left. Instead of fixing this
[109] Fix | Delete
# above, we fix it here so that PATH_INFO gets normalized to
[110] Fix | Delete
# an empty string in the environ.
[111] Fix | Delete
if name=='.':
[112] Fix | Delete
name = None
[113] Fix | Delete
return name
[114] Fix | Delete
[115] Fix | Delete
def setup_testing_defaults(environ):
[116] Fix | Delete
"""Update 'environ' with trivial defaults for testing purposes
[117] Fix | Delete
[118] Fix | Delete
This adds various parameters required for WSGI, including HTTP_HOST,
[119] Fix | Delete
SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO,
[120] Fix | Delete
and all of the wsgi.* variables. It only supplies default values,
[121] Fix | Delete
and does not replace any existing settings for these variables.
[122] Fix | Delete
[123] Fix | Delete
This routine is intended to make it easier for unit tests of WSGI
[124] Fix | Delete
servers and applications to set up dummy environments. It should *not*
[125] Fix | Delete
be used by actual WSGI servers or applications, since the data is fake!
[126] Fix | Delete
"""
[127] Fix | Delete
[128] Fix | Delete
environ.setdefault('SERVER_NAME','127.0.0.1')
[129] Fix | Delete
environ.setdefault('SERVER_PROTOCOL','HTTP/1.0')
[130] Fix | Delete
[131] Fix | Delete
environ.setdefault('HTTP_HOST',environ['SERVER_NAME'])
[132] Fix | Delete
environ.setdefault('REQUEST_METHOD','GET')
[133] Fix | Delete
[134] Fix | Delete
if 'SCRIPT_NAME' not in environ and 'PATH_INFO' not in environ:
[135] Fix | Delete
environ.setdefault('SCRIPT_NAME','')
[136] Fix | Delete
environ.setdefault('PATH_INFO','/')
[137] Fix | Delete
[138] Fix | Delete
environ.setdefault('wsgi.version', (1,0))
[139] Fix | Delete
environ.setdefault('wsgi.run_once', 0)
[140] Fix | Delete
environ.setdefault('wsgi.multithread', 0)
[141] Fix | Delete
environ.setdefault('wsgi.multiprocess', 0)
[142] Fix | Delete
[143] Fix | Delete
from StringIO import StringIO
[144] Fix | Delete
environ.setdefault('wsgi.input', StringIO(""))
[145] Fix | Delete
environ.setdefault('wsgi.errors', StringIO())
[146] Fix | Delete
environ.setdefault('wsgi.url_scheme',guess_scheme(environ))
[147] Fix | Delete
[148] Fix | Delete
if environ['wsgi.url_scheme']=='http':
[149] Fix | Delete
environ.setdefault('SERVER_PORT', '80')
[150] Fix | Delete
elif environ['wsgi.url_scheme']=='https':
[151] Fix | Delete
environ.setdefault('SERVER_PORT', '443')
[152] Fix | Delete
[153] Fix | Delete
[154] Fix | Delete
[155] Fix | Delete
_hoppish = {
[156] Fix | Delete
'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
[157] Fix | Delete
'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
[158] Fix | Delete
'upgrade':1
[159] Fix | Delete
}.__contains__
[160] Fix | Delete
[161] Fix | Delete
def is_hop_by_hop(header_name):
[162] Fix | Delete
"""Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
[163] Fix | Delete
return _hoppish(header_name.lower())
[164] Fix | Delete
[165] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function