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