mercurial/urllibcompat.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 10 Mar 2018 11:03:45 -0800
changeset 36855 2cdf47e14c30
parent 36835 5bc7ff103081
child 40159 5774fc623a18
permissions -rw-r--r--
hgweb: refactor the request draining code The previous code for draining was only invoked in a few places in the wire protocol. Behavior wasn't consist. Furthermore, it was difficult to reason about. With us converting the input stream to a capped reader, it is now safe to always drain the input stream when its size is known because we can never overrun the input and read into the next HTTP request. The only question is "should we?" This commit changes the draining code so every request is examined. Draining now kicks in for a few requests where it wouldn't before. But I think the code is sufficiently restricted so the behavior is safe. Possibly the most dangerous part of this code is the issuing of Connection: close for POST and PUT requests that don't have a Content-Length. I don't think there are any such uses in our WSGI application, so this should be safe. In the near future, I plan to significantly refactor the WSGI response handling. I anticipate this code evolving a bit. So any minor regressions around draining or connection closing behavior might be fixed as a result of that work. All tests pass with this change. That scares me a bit because it means we are lacking low-level tests for the HTTP protocol. Differential Revision: https://phab.mercurial-scm.org/D2769
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34465
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
# Copyright 2017 Google, Inc.
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
from __future__ import absolute_import
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
from . import pycompat
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
34467
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    11
_sysstr = pycompat.sysstr
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    12
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    13
class _pycompatstub(object):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    14
    def __init__(self):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    15
        self._aliases = {}
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    16
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    17
    def _registeraliases(self, origin, items):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    18
        """Add items that will be populated at the first access"""
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    19
        items = map(_sysstr, items)
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    20
        self._aliases.update(
36835
5bc7ff103081 py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents: 36809
diff changeset
    21
            (item.replace(r'_', r'').lower(), (origin, item))
34467
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    22
            for item in items)
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    23
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    24
    def _registeralias(self, origin, attr, name):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    25
        """Alias ``origin``.``attr`` as ``name``"""
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    26
        self._aliases[_sysstr(name)] = (origin, _sysstr(attr))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    27
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    28
    def __getattr__(self, name):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    29
        try:
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    30
            origin, item = self._aliases[name]
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    31
        except KeyError:
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    32
            raise AttributeError(name)
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    33
        self.__dict__[name] = obj = getattr(origin, item)
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    34
        return obj
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    35
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    36
httpserver = _pycompatstub()
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    37
urlreq = _pycompatstub()
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    38
urlerr = _pycompatstub()
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    39
34465
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
if pycompat.ispy3:
34467
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    41
    import urllib.parse
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    42
    urlreq._registeraliases(urllib.parse, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    43
        "splitattr",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    44
        "splitpasswd",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    45
        "splitport",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    46
        "splituser",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    47
        "urlparse",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    48
        "urlunparse",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    49
    ))
36077
a3d42d1865f1 wireprotoserver: define and use parse_qs from urllib
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34467
diff changeset
    50
    urlreq._registeralias(urllib.parse, "parse_qs", "parseqs")
36809
3c15b84ab66c hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36077
diff changeset
    51
    urlreq._registeralias(urllib.parse, "parse_qsl", "parseqsl")
34467
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    52
    urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote")
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    53
    import urllib.request
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    54
    urlreq._registeraliases(urllib.request, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    55
        "AbstractHTTPHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    56
        "BaseHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    57
        "build_opener",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    58
        "FileHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    59
        "FTPHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    60
        "ftpwrapper",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    61
        "HTTPHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    62
        "HTTPSHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    63
        "install_opener",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    64
        "pathname2url",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    65
        "HTTPBasicAuthHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    66
        "HTTPDigestAuthHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    67
        "HTTPPasswordMgrWithDefaultRealm",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    68
        "ProxyHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    69
        "Request",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    70
        "url2pathname",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    71
        "urlopen",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    72
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    73
    import urllib.response
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    74
    urlreq._registeraliases(urllib.response, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    75
        "addclosehook",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    76
        "addinfourl",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    77
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    78
    import urllib.error
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    79
    urlerr._registeraliases(urllib.error, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    80
        "HTTPError",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    81
        "URLError",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    82
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    83
    import http.server
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    84
    httpserver._registeraliases(http.server, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    85
        "HTTPServer",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    86
        "BaseHTTPRequestHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    87
        "SimpleHTTPRequestHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    88
        "CGIHTTPRequestHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    89
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    90
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    91
    # urllib.parse.quote() accepts both str and bytes, decodes bytes
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    92
    # (if necessary), and returns str. This is wonky. We provide a custom
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    93
    # implementation that only accepts bytes and emits bytes.
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    94
    def quote(s, safe=r'/'):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    95
        s = urllib.parse.quote_from_bytes(s, safe=safe)
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    96
        return s.encode('ascii', 'strict')
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    97
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    98
    # urllib.parse.urlencode() returns str. We use this function to make
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
    99
    # sure we return bytes.
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   100
    def urlencode(query, doseq=False):
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   101
            s = urllib.parse.urlencode(query, doseq=doseq)
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   102
            return s.encode('ascii')
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   103
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   104
    urlreq.quote = quote
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   105
    urlreq.urlencode = urlencode
34465
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   106
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   107
    def getfullurl(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   108
        return req.full_url
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   109
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   110
    def gethost(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   111
        return req.host
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   112
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   113
    def getselector(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   114
        return req.selector
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
    def getdata(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
        return req.data
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
    def hasdata(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
        return req.data is not None
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
else:
34467
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   122
    import BaseHTTPServer
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   123
    import CGIHTTPServer
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   124
    import SimpleHTTPServer
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   125
    import urllib2
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   126
    import urllib
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   127
    import urlparse
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   128
    urlreq._registeraliases(urllib, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   129
        "addclosehook",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   130
        "addinfourl",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   131
        "ftpwrapper",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   132
        "pathname2url",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   133
        "quote",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   134
        "splitattr",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   135
        "splitpasswd",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   136
        "splitport",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   137
        "splituser",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   138
        "unquote",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   139
        "url2pathname",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   140
        "urlencode",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   141
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   142
    urlreq._registeraliases(urllib2, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   143
        "AbstractHTTPHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   144
        "BaseHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   145
        "build_opener",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   146
        "FileHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   147
        "FTPHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   148
        "HTTPBasicAuthHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   149
        "HTTPDigestAuthHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   150
        "HTTPHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   151
        "HTTPPasswordMgrWithDefaultRealm",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   152
        "HTTPSHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   153
        "install_opener",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   154
        "ProxyHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   155
        "Request",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   156
        "urlopen",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   157
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   158
    urlreq._registeraliases(urlparse, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   159
        "urlparse",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   160
        "urlunparse",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   161
    ))
36077
a3d42d1865f1 wireprotoserver: define and use parse_qs from urllib
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34467
diff changeset
   162
    urlreq._registeralias(urlparse, "parse_qs", "parseqs")
36809
3c15b84ab66c hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36077
diff changeset
   163
    urlreq._registeralias(urlparse, "parse_qsl", "parseqsl")
34467
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   164
    urlerr._registeraliases(urllib2, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   165
        "HTTPError",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   166
        "URLError",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   167
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   168
    httpserver._registeraliases(BaseHTTPServer, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   169
        "HTTPServer",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   170
        "BaseHTTPRequestHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   171
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   172
    httpserver._registeraliases(SimpleHTTPServer, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   173
        "SimpleHTTPRequestHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   174
    ))
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   175
    httpserver._registeraliases(CGIHTTPServer, (
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   176
        "CGIHTTPRequestHandler",
192f7b126ed2 urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents: 34465
diff changeset
   177
    ))
34465
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   178
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   179
    def gethost(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   180
        return req.get_host()
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   181
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   182
    def getselector(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   183
        return req.get_selector()
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   184
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   185
    def getfullurl(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   186
        return req.get_full_url()
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   187
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   188
    def getdata(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   189
        return req.get_data()
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   190
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   191
    def hasdata(req):
80d4681150b9 urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff changeset
   192
        return req.has_data()