summaryrefslogtreecommitdiffstats
path: root/WebKitTools/Scripts/webkitpy/thirdparty/autoinstalled/mechanize/_upgrade.py
blob: df59c010219ae7c59dd23b459b098c6e674562a4 (plain)
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
from urllib2 import BaseHandler

from _request import Request
from _response import upgrade_response
from _util import deprecation


class HTTPRequestUpgradeProcessor(BaseHandler):
    # upgrade urllib2.Request to this module's Request
    # yuck!
    handler_order = 0  # before anything else

    def http_request(self, request):
        if not hasattr(request, "add_unredirected_header"):
            newrequest = Request(request.get_full_url(), request.data,
                                 request.headers)
            try: newrequest.origin_req_host = request.origin_req_host
            except AttributeError: pass
            try: newrequest.unverifiable = request.unverifiable
            except AttributeError: pass
            try: newrequest.visit = request.visit
            except AttributeError: pass
            request = newrequest
        return request

    https_request = http_request


class ResponseUpgradeProcessor(BaseHandler):
    # upgrade responses to be .close()able without becoming unusable
    handler_order = 0  # before anything else

    def __init__(self):
        deprecation(
            "See http://wwwsearch.sourceforge.net/mechanize/doc.html#seekable")

    def any_response(self, request, response):
        if not hasattr(response, 'closeable_response'):
            response = upgrade_response(response)
        return response