1 | from urllib2 import BaseHandler |
---|
2 | |
---|
3 | from _request import Request |
---|
4 | from _response import upgrade_response |
---|
5 | from _util import deprecation |
---|
6 | |
---|
7 | |
---|
8 | class HTTPRequestUpgradeProcessor(BaseHandler): |
---|
9 | # upgrade urllib2.Request to this module's Request |
---|
10 | # yuck! |
---|
11 | handler_order = 0 # before anything else |
---|
12 | |
---|
13 | def http_request(self, request): |
---|
14 | if not hasattr(request, "add_unredirected_header"): |
---|
15 | newrequest = Request(request._Request__original, request.data, |
---|
16 | request.headers) |
---|
17 | try: newrequest.origin_req_host = request.origin_req_host |
---|
18 | except AttributeError: pass |
---|
19 | try: newrequest.unverifiable = request.unverifiable |
---|
20 | except AttributeError: pass |
---|
21 | try: newrequest.visit = request.visit |
---|
22 | except AttributeError: pass |
---|
23 | request = newrequest |
---|
24 | return request |
---|
25 | |
---|
26 | https_request = http_request |
---|
27 | |
---|
28 | |
---|
29 | class ResponseUpgradeProcessor(BaseHandler): |
---|
30 | # upgrade responses to be .close()able without becoming unusable |
---|
31 | handler_order = 0 # before anything else |
---|
32 | |
---|
33 | def __init__(self): |
---|
34 | deprecation( |
---|
35 | "See http://wwwsearch.sourceforge.net/mechanize/doc.html#seekable") |
---|
36 | |
---|
37 | def any_response(self, request, response): |
---|
38 | if not hasattr(response, 'closeable_response'): |
---|
39 | response = upgrade_response(response) |
---|
40 | return response |
---|