-
Notifications
You must be signed in to change notification settings - Fork 13
/
_http.py
606 lines (531 loc) · 15.8 KB
/
_http.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# -*-encoding=utf-8-*-
import os.path
import base64
import string
from uuid import uuid4
try:
import ssl
has_ssl = True
except:
has_ssl = False
convert_table = [0 for x in range(256)]
# url reversed characters
reversed_table = {
"\x21": "%21", # !
"\x23": "%23", ##
"\x24": "%24", # $
"\x26": "%26", # &
"\x27": "%27", # '
"\x28": "%28", # (
"\x29": "%29", # )
"\x2A": "%2A", # *
"\x2B": "%2B", # +
"\x2C": "%2C", # ,
"\x2F": "%2F", # /
"\x3A": "%3A", #:
"\x3B": "%3B", # ;
"\x3D": "%3D", # =
"\x3F": "%3F", # ?
"\x40": "%40", # @
"\x5B": "%5B", # [
"\x5D": "%5D" # ]
}
# 使用0x0 - xff数组优化查找
for k, v in reversed_table.items():
convert_table[ord(k)] = v
# url common characters
common_chars_table = {
"\x20": "%20", # space
"\x22": "%22", # "
"\x25": "%25", # %
# "\x2D": "%2D", #-
# 0x2E: "%2E", #.
"\x3C": "%3C", # <
"\x3E": "%3E", # >
"\x5C": "%5C", # \
"\x5E": "%5E", # ^
# 0x5F: "%5F", #_
"\x60": "%60", # `
"\x7B": "%7B", # {
"\x7C": "%7C", # |
"\x7D": "%7D", # }
"\x7E": "%7E" # ~
}
# 使用0x0 - 0xff数组优化查找
for k, v in common_chars_table.items():
convert_table[ord(k)] = v
# 是否是字符
letters = [0 for x in range(256)]
for x in string.letters:
letters[ord(x)] = 1
hex_digits_set = set(string.hexdigits)
default_header = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
# "Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh,zh-cn;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
}
download_header = {
"Accept": "*/*",
"Connection": "Keep-Alive"
}
image_header = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "Keep-Alive",
}
html_header = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh,zh-cn;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection": "keep-alive",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
}
json_header = {
"Accept": "application/json,text/javascript,*/*;q=0.01",
"Accept-Language": "zh,zh-cn;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"
}
# common mimetypes
common_types = {
"pdf": "application/pdf",
"zip": "application/zip",
"gz": "application/x-gzip",
"doc": "application/msword",
"ogg": "application/ogg",
"default": "application/octet-stream",
"json": "application/json",
"xml": "application/xml",
"js": "application/x-javascript",
"7z": "application/x-7z-compressed",
"deb": "application/x-deb",
"tar": "application/x-tar",
"swf": "application/x-shockwave-flash",
"torrent": "application/x-bittorrent",
"bmp": "image/bmp",
"gif": "image/gif",
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"svg": "image/svg+xml",
"tiff": "image/tiff",
"mp3": "audio/mpeg",
"wav": "audio/x-wav",
"css": "text/css",
"text": "text/plain",
"html": "text/html",
"vcard": "text/vcard",
"md": "text/x-markdown",
"mov": "video/quicktime",
"mp4": "video/mp4",
"mkv": "video/x-matroska",
"wmv": "video/x-ms-wmv",
"flv": "video/x-flv",
"mpg": "video/mpeg",
"mpeg": "video/mpeg"
}
# http consts
resp_codes = {
100: "100 Continue",
101: "101 Switching Protocols",
102: "102 Processing",
200: "200 OK",
201: "201 Created",
202: "202 Accepted",
203: "203 Non-Authoritative Information",
204: "204 No Content",
205: "205 Reset Content",
206: "206 Partial Content",
300: "300 Multiple Choices",
301: "301 Moved Permanently",
302: "302 Found",
303: "303 See Other",
304: "304 Not Modified",
305: "305 Use Proxy",
306: "306 Switch Proxy",
307: "307 Temporary Redirect",
400: "400 Bad Request",
401: "401 Unauthorized",
402: "402 Payment Required",
403: "403 Forbidden",
404: "404 Not Found",
405: "405 Method Not Allowed",
406: "406 Not Acceptable",
407: "407 Proxy Authentication Required",
408: "408 Request Timeout",
409: "409 Conflict",
410: "410 Gone",
411: "411 Length Required",
412: "412 Precondition Failed",
413: "413 Request Entity Too Large",
414: "414 Request-URI Too Long",
415: "415 Unsupportd Media Type",
416: "416 Requested Range Not Satisfiable",
417: "417 Expectation Failed",
418: "418 I'm a teapot",
421: "421 There are too many remotes from your Internet Address",
422: "422 Unprocessable Entity",
423: "423 Locked",
424: "424 Failed Dependency",
425: "425 Unordered Collection",
426: "426 Upgrade Required",
449: "449 Retry With",
500: "500 Internal Server Error",
501: "501 Not implemented",
502: "502 Bad Gateway",
503: "503 Service Unavailable",
504: "504 Gateway Timeout",
505: "505 HTTP Version Not Supported",
506: "506 Variant Also Negotiates",
507: "507 Insufficient Storage",
509: "509 Bandwidth Limit Exceeded",
510: "Not Extended"
}
default_timeout = 30
HTTP_VERSION = "HTTP/1.1"
HEADER_END = "\x0d\x0a\x0d\x0a"
HEADER_END2 = "\n\n"
METHOD_GET = "GET"
METHOD_POST = "POST"
METHOD_DELETE = "DELETE"
METHOD_PUT = "PUT"
METHOD_OPTIONS = "OPTIONS"
METHOD_TRACE = "TRACE"
METHOD_HEAD = "HEAD"
def set_boundary(uid):
g = globals()
g["BOUNDARY"] = uid
g["BOUNDARY_STRING"] = "--%s\r\n" % BOUNDARY
g["BOUNDARY_END"] = "--%s--" % BOUNDARY
g["FORM_FILE"] = 'Content-Disposition: form-data; name="%s"; filename="%s"\r\nContent-Type: %s\r\n\r\n'
g["FORM_STRING"] = 'Content-Disposition: form-data; name="%s"\r\n\r\n%s\r\n'
g["FORM_SIMPLE_TYPE"] = "application/x-www-form-urlencoded"
g["FORM_COMPLEX_TYPE"] = "multipart/form-data; boundary=%s" % BOUNDARY
set_boundary(uuid4().hex)
def basic_auth_msg(user, password):
if user and password:
return "Basic %s" % base64.b64encode("%s:%s" % (user, password))
if user:
return "Basic %s" % base64.b64encode(user)
def proxy_auth_msg(proxy):
proxyd = urlparse(proxy)
if proxyd["schema"] == "http":
return basic_auth_msg(proxyd.get("user"), proxyd.get("password"))
def generate_query(query):
ql = []
for k, v in query.items():
ql.append("%s=%s" % (quote_plus(k), quote_plus(v)))
return "&".join(ql)
def parse_query(query):
qd = {}
for q in query.split("&"):
i = q.find("=")
if i > -1:
qd[unquote_plus(q[:i])] = unquote_plus(q[i + 1:])
else:
qd[unquote_plus(q)] = None
return qd
def generate_simple_post(payload):
pl = []
for k, v in payload.items():
pl.append("%s=%s" % (quote_plus(k), quote_plus(v)))
return "&".join(pl)
def generate_complex_post(payload):
cl = []
for k, v in payload.items():
if isinstance(v, str):
cl.append(BOUNDARY_STRING)
cl.append(FORM_STRING % (k, v))
if isinstance(v, file):
filename = os.path.basename(v.name)
if not filename:
filename = "unknown"
cl.append(BOUNDARY_STRING)
cl.append(FORM_FILE % (k, filename,
auto_content_type(filename)))
cl.append(v.read())
cl.append("\r\n")
cl.append(BOUNDARY_END)
return cl
def generate_post(header, payload):
if isinstance(payload, str):
return payload
elif isinstance(payload, unicode):
return payload.encode("utf-8")
elif isinstance(payload, dict):
has_file = False
# use multipart/form-data or not
for k, v in payload.items():
if isinstance(v, unicode):
payload[k] = v.encode("utf-8")
elif isinstance(v, file):
has_file = True
elif isinstance(v, str):
continue
else:
raise Exception("payload value: str or unicode or fileobject")
if has_file:
header["Content-Type"] = FORM_COMPLEX_TYPE
return "".join(generate_complex_post(payload))
else:
header["Content-Type"] = FORM_SIMPLE_TYPE
cl = generate_simple_post(payload)
return "".join(cl)
else:
raise ValueError("unknown payload type: %s" % type(payload))
def quote(url):
result = []
for char in url.decode("utf-8"):
x = ord(char)
if x < 256:
if convert_table[x]:
result.append(convert_table[x])
else:
result.append(char)
else:
result.extend(["%" + i.encode("hex").upper() for i in char.encode("utf-8")])
return "".join(result)
# fix bug
def unquote(url):
ret = []
i = 0
ulen = len(url)
if not ulen:
return url
while i < ulen:
char = url[i]
if char == "%":
ret.append(url[i + 1:i + 3].decode("hex"))
i = i + 3
else:
ret.append(char)
i += 1
return "".join(ret)
def quote_plus(url):
if ' ' in url:
# dirty hack
convert_table[0x20] = '+'
ret = quote(url)
convert_table[0x20] = '%20'
return ret
return quote(url)
def unquote_plus(url):
url = url.replace("+", " ")
return unquote(url)
def auto_content_type(name):
dot_offset = name.rfind(".")
if dot_offset < 0:
return common_types["default"]
else:
return common_types.get(name[dot_offset + 1:], common_types["default"])
def generate_url(d):
ret = []
if "schema" in d:
ret.append(d["schema"] + "://")
if "user" in d:
ret.append(d["user"])
if "password" in d:
result.append(":" + d["password"])
ret.append("@")
if "host" in d:
ret.append(d["host"])
if "port" in d:
ret.append(":" + str(d["port"]))
if "path" in d:
if not d["path"].startswith("/"):
ret.append("/")
ret.append(d["path"])
if "query" in d:
ret.append("?" + d["query"])
if "params" in d:
if isinstance(d["params"], list):
ret.append(";" + ";".join(d["params"]))
else:
ret.append(";")
ret.append(d["params"])
if "fragment" in d:
ret.append("#")
ret.append(d["fragment"])
return "".join(ret)
def urlparse(url):
d = {}
order = (("fragment", url.rfind("#")),
("params", url.rfind(";")),
("query", url.rfind("?")))
order = sorted(order, key=lambda x: x[1], reverse=True)
pv = len(url)
for n, i in order:
if i > 0:
if n == "query" and url[i - 1] == "?":
continue
d[n] = url[i + 1:pv]
pv = i
else:
break
ne = url[:pv]
hps = ne.split("://")
host = hps[0]
if len(hps) > 1:
d["schema"] = hps[0]
host = hps[1]
# http://v2ex.com/static/img/[email protected]'
us = host.find("@")
p = host.find("/")
if 0 < us < p:
user = host[:us]
host = host[us + 1:]
ac = user.split(":")
if len(ac) > 0:
user = ac[0]
d["password"] = ac[1]
d["user"] = user
p = host.find("/")
if p > 0:
d["path"] = host[p + 1:]
host = host[:p]
ph = host.split(":")
port = None
if len(ph) > 1:
host = ph[0]
d["port"] = ph[1]
d["host"] = host
if "path" not in d:
d["path"] = "/"
return d
def generate_cookie(cookie):
ret = []
has_unicode = False
for k, v in cookie.items():
if isinstance(k, unicode) or isinstance(v, unicode):
has_unicode = True
ret.append("%s=%s; " % (k, v))
if has_unicode:
return "".join(ret)[:-2].encode("utf-8")
else:
return "".join(ret)[:-2]
def parse_cookie(cookie):
cd = {}
for cookie in cookie.split(";"):
kv = cookie.split("=")
cd[kv[0].strip()] = kv[1].strip()
return cd
def generate_setcookie(cl):
ret = []
for cd in cl:
items_list = []
for k, v in cd.items():
if k == "cookie":
item_list.append('%s; ' % v)
else:
item_list.append('%s=%s; ' % (k, v))
ret.append("".join(items_list)[:-2])
ret.append("\r\n")
return "".join(ret)[:-2]
def parse_setcookie(data):
cl = {}
for line in data.split("\r\n"):
lines = line.split(";")
cookie = {}
vl = lines[0]
idx = vl.find("=")
name = vl[:idx]
value = vl[idx + 1:]
cookie["value"] = value
for part in lines[1:]:
kv = part.split("=")
# path=/ or httponly
key = kv[0].strip()
if len(kv) == 2:
cookie[key] = kv[1]
else:
cookie[key] = True
cl[name] = cookie
return cl
def get_cookie(cookies):
c = {}
for key, value in cookies.items():
c[key] = value["value"]
return c
def parse_simple_post(data):
post_dict = {}
for i in data.split("&"):
k, v = i.replace("+", " ").split("=")
post_dict[unquote_plus(k)] = unquote_plus(v)
return post_dict
def generate_request_header(header, method, path):
sl = "%s %s %s\r\n" % (method, path, HTTP_VERSION)
b = [sl]
for k, v in header.items():
b.append("%s: %s\r\n" % (k, v))
return "".join(b)
def generate_response_header(header, status):
sl = "%s %s\r\n" % (HTTP_VERSION, resp_codes["status"])
b = [sl]
for k, v in header.items():
b.append("%s: %s\r\n" % (k, v))
return "".join(b)
def parse_request_header(text):
parts = text.split("\r\n")
status_parts = parts[0].split(" ")
status = {
"method": status_parts[0],
"path": status_parts[1],
"protocol": status_parts[-1]
}
header = {}
last = None
for line in parts[1:]:
kv = [x.strip() for x in line.split(":")]
# maybe : in value
if len(kv) > 2:
kv[1] = ":".join(kv[1:])
# maybe multiple lines
if len(kv) == 1:
header[last[0]] += "\r\n" + "".join(kv)
continue
last = kv
header[kv[0]] = kv[1]
cookie = header.get("Cookie", {})
if cookie:
del header["Cookie"]
cookie = parse_cookie(cookie)
return status, cookie, header
def parse_response_header(text):
parts = text.split("\r\n")
status_parts = parts[0].split(" ")
status = {
"protocol": status_parts[0],
"status": int(status_parts[1]),
"message": " ".join(status_parts[2:])
}
header = {}
last = None
for line in parts[1:]:
kv = [x.strip() for x in line.split(":")]
# maybe : in value
if len(kv) > 2:
kv[1] = ":".join(kv[1:])
# maybe multiple lines
if len(kv) == 1:
header[last[0]] += "\r\n" + kv[0]
continue
last = kv
key = kv[0]
if key in header and "set-cookie" in key.lower():
header[key] += "\r\n" + kv[1]
else:
header[kv[0]] = kv[1]
cookie = {}
cookie1 = header.get("Set-Cookie")
if cookie1:
cookie.update(parse_setcookie(cookie1))
cookie2 = header.get("Set-Cookie2")
if cookie2:
cookie.update(parse_setcookie(cookie2))
return status, cookie, header