ยปCore Development>Code coverage>Lib/wsgiref/util.py

Python code coverage for Lib/wsgiref/util.py

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