Tres Seaver
2017-12-20 a83610e170325e157726a4649923dff8ae303d12
commit | author | age
9df42f 1 try:
TS 2     STRING_TYPES = (str, unicode)
a37aff 3 except NameError: #pragma NO COVER Python >= 3.0
9df42f 4     STRING_TYPES = (str,)
TS 5
fdc787 6 try:
TS 7     u = unicode
8 except NameError: #pragma NO COVER Python >= 3.0
9     u = str
00c816 10     b = bytes
bf5b5a 11 else: #pragma NO COVER Python < 3.0
TS 12     b = str
fdc787 13
a37aff 14 import base64
TS 15 if 'decodebytes' in base64.__dict__: #pragma NO COVER Python >= 3.0
16     decodebytes = base64.decodebytes
17     encodebytes = base64.encodebytes
513d65 18     def decodestring(value):
5bdad5 19         return base64.decodebytes(bytes(value, 'ascii')).decode('ascii')
513d65 20     def encodestring(value):
5bdad5 21         return base64.encodebytes(bytes(value, 'ascii')).decode('ascii')
bf5b5a 22 else: #pragma NO COVER Python < 3.0
a37aff 23     decodebytes = base64.decodestring
TS 24     encodebytes = base64.encodestring
513d65 25     decodestring = base64.decodestring
TS 26     encodestring = base64.encodestring
a90306 27
adef05 28 try:
9c1e6f 29     from urllib.parse import parse_qs
TS 30 except ImportError: #pragma NO COVER Python < 3.0
31     from cgi import parse_qs
32     from cgi import parse_qsl
33 else: #pragma NO COVER Python >= 3.0
34     from urllib.parse import parse_qsl
35
36 try:
6866db 37     import ConfigParser
adef05 38 except ImportError: #pragma NO COVER Python >= 3.0
6866db 39     from configparser import ConfigParser
adef05 40     from configparser import ParsingError
bf5b5a 41 else: #pragma NO COVER Python < 3.0
6866db 42     from ConfigParser import SafeConfigParser as ConfigParser
a37aff 43     from ConfigParser import ParsingError
adef05 44
TS 45 try:
46     from Cookie import SimpleCookie
47 except ImportError: #pragma NO COVER Python >= 3.0
48     from http.cookies import SimpleCookie
49     from http.cookies import CookieError
bf5b5a 50 else: #pragma NO COVER Python < 3.0
a37aff 51     from Cookie import CookieError
adef05 52
TS 53 try:
54     from itertools import izip_longest
55 except ImportError: #pragma NO COVER Python >= 3.0
56     from itertools import zip_longest as izip_longest
57
58 try:
59     from StringIO import StringIO
60 except ImportError: #pragma NO COVER Python >= 3.0
61     from io import StringIO
62
63 try:
53987b 64     from urllib import urlencode
adef05 65 except ImportError: #pragma NO COVER Python >= 3.0
53987b 66     from urllib.parse import urlencode
adef05 67     from urllib.parse import quote as url_quote
TS 68     from urllib.parse import unquote as url_unquote
bf5b5a 69 else: #pragma NO COVER Python < 3.0
a37aff 70     from urllib import quote as url_quote
TS 71     from urllib import unquote as url_unquote
adef05 72
53987b 73 try:
TS 74     from urlparse import urlparse
75 except ImportError: #pragma NO COVER Python >= 3.0
76     from urllib.parse import urlparse
77     from urllib.parse import urlunparse
bf5b5a 78 else: #pragma NO COVER Python < 3.0
a37aff 79     from urlparse import urlunparse
53987b 80
adef05 81 import wsgiref.util
TS 82 import wsgiref.headers
83
02a504 84 def REQUEST_METHOD(environ):
AO 85     return environ['REQUEST_METHOD']
86
87 def CONTENT_TYPE(environ):
a5135a 88     return environ.get('CONTENT_TYPE', '')
02a504 89
AO 90 def USER_AGENT(environ):
91     return environ.get('HTTP_USER_AGENT')
92
93 def AUTHORIZATION(environ):
94     return environ.get('HTTP_AUTHORIZATION', '')
95
96 def get_cookies(environ):
97     header = environ.get('HTTP_COOKIE', '')
a37aff 98     if 'paste.cookies' in environ:
02a504 99         cookies, check_header = environ['paste.cookies']
AO 100         if check_header == header:
101             return cookies
102     cookies = SimpleCookie()
103     try:
104         cookies.load(header)
a37aff 105     except CookieError: #pragma NO COVER (can't see how to provoke this)
02a504 106         pass
AO 107     environ['paste.cookies'] = (cookies, header)
108     return cookies
109
110 def construct_url(environ):
111     return wsgiref.util.request_uri(environ)
112
113 def header_value(environ, key):
114     headers = wsgiref.headers.Headers(environ)
115     values = headers.get(key)
116     if not values:
117         return ""
bf5b5a 118     if isinstance(values, list): #pragma NO COVER can't be true under Py3k.
02a504 119         return ",".join(values)
AO 120     else:
121         return values
cc04ed 122
TS 123 def must_decode(value):
124     if type(value) is b:
125         try:
126             return value.decode('utf-8')
127         except UnicodeDecodeError:
128             return value.decode('latin1')
129     return value
4cc78d 130
TS 131 def must_encode(value):
132     if type(value) is u:
133         return value.encode('utf-8')
134     return value