forked from SeattleTestbed/seattlelib_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urllib.r2py
208 lines (144 loc) · 4.3 KB
/
urllib.r2py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def urllib_quote(string, safe="/"):
"""
<Purpose>
Encode a string such that it can be used safely in a URL or XML
document.
<Arguments>
string:
The string to urlencode.
safe (optional):
Specifies additional characters that should not be quoted --
defaults to "/".
<Exceptions>
TypeError if the safe parameter isn't an enumerable.
<Side Effects>
None.
<Returns>
Urlencoded version of the passed string.
"""
resultstr = ""
# We go through each character in the string; if it's not in [0-9a-zA-Z]
# we wrap it.
safeset = set(safe)
for char in string:
asciicode = ord(char)
if (asciicode >= ord("0") and asciicode <= ord("9")) or \
(asciicode >= ord("A") and asciicode <= ord("Z")) or \
(asciicode >= ord("a") and asciicode <= ord("z")) or \
asciicode == ord("_") or asciicode == ord(".") or \
asciicode == ord("-") or char in safeset:
resultstr += char
else:
resultstr += "%%%02X" % asciicode
return resultstr
def urllib_quote_plus(string, safe=""):
"""
<Purpose>
Encode a string to go in the query fragment of a URL.
<Arguments>
string:
The string to urlencode.
safe (optional):
Specifies additional characters that should not be quoted --
defaults to the empty string.
<Exceptions>
TypeError if the safe parameter isn't a string.
<Side Effects>
None.
<Returns>
Urlencoded version of the passed string.
"""
return urllib_quote(string, safe + " ").replace(" ", "+")
def urllib_unquote(string):
"""
<Purpose>
Unquote a urlencoded string.
<Arguments>
string:
The string to unquote.
<Exceptions>
ValueError thrown if the last wrapped octet isn't a valid wrapped octet
(i.e. if the string ends in "%" or "%x" rather than "%xx". Also throws
ValueError if the nibbles aren't valid hex digits.
<Side Effects>
None.
<Returns>
The decoded string.
"""
resultstr = ""
# We go through the string from end to beginning, looking for wrapped
# octets. When one is found we add it (unwrapped) and the following
# string to the resultant string, and shorten the original string.
while True:
lastpercentlocation = string.rfind("%")
if lastpercentlocation < 0:
break
wrappedoctetstr = string[lastpercentlocation+1:lastpercentlocation+3]
if len(wrappedoctetstr) != 2:
raise ValueError("Quoted string is poorly formed")
resultstr = \
chr(int(wrappedoctetstr, 16)) + \
string[lastpercentlocation+3:] + \
resultstr
string = string[:lastpercentlocation]
resultstr = string + resultstr
return resultstr
def urllib_unquote_plus(string):
"""
<Purpose>
Unquote the urlencoded query fragment of a URL.
<Arguments>
string:
The string to unquote.
<Exceptions>
ValueError thrown if the last wrapped octet isn't a valid wrapped octet
(i.e. if the string ends in "%" or "%x" rather than "%xx". Also throws
ValueError if the nibbles aren't valid hex digits.
<Side Effects>
None.
<Returns>
The decoded string.
"""
return urllib_unquote(string.replace("+", " "))
def urllib_quote_parameters(dictionary):
"""
<Purpose>
Encode a dictionary of (key, value) pairs into an HTTP query string or
POST body (same form).
<Arguments>
dictionary:
The dictionary to quote.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The quoted dictionary.
"""
quoted_keyvals = []
for key, val in dictionary.items():
quoted_keyvals.append("%s=%s" % (urllib_quote(key), urllib_quote(val)))
return "&".join(quoted_keyvals)
def urllib_unquote_parameters(string):
"""
<Purpose>
Decode a urlencoded query string or POST body.
<Arguments>
string:
The string to decode.
<Exceptions>
ValueError if the string is poorly formed.
<Side Effects>
None.
<Returns>
A dictionary mapping keys to values.
"""
keyvalpairs = string.split("&")
res = {}
for quotedkeyval in keyvalpairs:
# Throw ValueError if there is more or less than one '='.
quotedkey, quotedval = quotedkeyval.split("=")
key = urllib_unquote_plus(quotedkey)
val = urllib_unquote_plus(quotedval)
res[key] = val
return res