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