1 | """ |
---|
2 | A subclass of the mechanize browser patched to fix various bits. |
---|
3 | """ |
---|
4 | |
---|
5 | # wwwsearch imports |
---|
6 | from _mechanize_dist import Browser as MechanizeBrowser |
---|
7 | |
---|
8 | import wsgi_intercept |
---|
9 | from utils import FixedHTTPBasicAuthHandler, FunctioningHTTPRefreshProcessor |
---|
10 | |
---|
11 | def build_http_handler(): |
---|
12 | from _mechanize_dist._urllib2 import HTTPHandler |
---|
13 | |
---|
14 | class MyHTTPHandler(HTTPHandler): |
---|
15 | def http_open(self, req): |
---|
16 | return self.do_open(wsgi_intercept.WSGI_HTTPConnection, req) |
---|
17 | |
---|
18 | return MyHTTPHandler |
---|
19 | |
---|
20 | class PatchedMechanizeBrowser(MechanizeBrowser): |
---|
21 | """ |
---|
22 | A patched version of the mechanize browser class. Currently |
---|
23 | installs the WSGI intercept handler & fixes a problem with |
---|
24 | mechanize/urllib2 Basic Authentication. |
---|
25 | """ |
---|
26 | def __init__(self, *args, **kwargs): |
---|
27 | # install WSGI intercept handler. |
---|
28 | self.handler_classes['http'] = build_http_handler() |
---|
29 | |
---|
30 | # fix basic auth. |
---|
31 | self.handler_classes['_basicauth'] = FixedHTTPBasicAuthHandler |
---|
32 | |
---|
33 | # make refresh work even for somewhat mangled refresh directives. |
---|
34 | self.handler_classes['_refresh'] = FunctioningHTTPRefreshProcessor |
---|
35 | |
---|
36 | MechanizeBrowser.__init__(self, *args, **kwargs) |
---|