author | Kevin Bullock <kbullock+mercurial@ringworld.org> |
Tue, 06 Mar 2018 13:17:07 -0600 | |
branch | stable |
changeset 36757 | 8bba684efde7 |
parent 34467 | 192f7b126ed2 |
child 36077 | a3d42d1865f1 |
permissions | -rw-r--r-- |
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( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
21 |
(item.replace(_sysstr('_'), _sysstr('')).lower(), (origin, item)) |
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 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
50 |
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
|
51 |
import urllib.request |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
52 |
urlreq._registeraliases(urllib.request, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
53 |
"AbstractHTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
54 |
"BaseHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
55 |
"build_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
56 |
"FileHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
57 |
"FTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
58 |
"ftpwrapper", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
59 |
"HTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
60 |
"HTTPSHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
61 |
"install_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
62 |
"pathname2url", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
63 |
"HTTPBasicAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
64 |
"HTTPDigestAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
65 |
"HTTPPasswordMgrWithDefaultRealm", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
66 |
"ProxyHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
67 |
"Request", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
68 |
"url2pathname", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
69 |
"urlopen", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
70 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
71 |
import urllib.response |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
72 |
urlreq._registeraliases(urllib.response, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
73 |
"addclosehook", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
74 |
"addinfourl", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
75 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
76 |
import urllib.error |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
77 |
urlerr._registeraliases(urllib.error, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
78 |
"HTTPError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
79 |
"URLError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
80 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
81 |
import http.server |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
82 |
httpserver._registeraliases(http.server, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
83 |
"HTTPServer", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
84 |
"BaseHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
85 |
"SimpleHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
86 |
"CGIHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
87 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
88 |
|
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
89 |
# 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
|
90 |
# (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
|
91 |
# 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
|
92 |
def quote(s, safe=r'/'): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
93 |
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
|
94 |
return s.encode('ascii', 'strict') |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
95 |
|
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
96 |
# 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
|
97 |
# sure we return bytes. |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
98 |
def urlencode(query, doseq=False): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
99 |
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
|
100 |
return s.encode('ascii') |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
101 |
|
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
102 |
urlreq.quote = quote |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
103 |
urlreq.urlencode = urlencode |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
104 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
105 |
def getfullurl(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
106 |
return req.full_url |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
107 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
108 |
def gethost(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
109 |
return req.host |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
110 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
111 |
def getselector(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
112 |
return req.selector |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
113 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
114 |
def getdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
115 |
return req.data |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
116 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
117 |
def hasdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
118 |
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
|
119 |
else: |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
120 |
import BaseHTTPServer |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
121 |
import CGIHTTPServer |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
122 |
import SimpleHTTPServer |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
123 |
import urllib2 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
124 |
import urllib |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
125 |
import urlparse |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
126 |
urlreq._registeraliases(urllib, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
127 |
"addclosehook", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
128 |
"addinfourl", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
129 |
"ftpwrapper", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
130 |
"pathname2url", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
131 |
"quote", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
132 |
"splitattr", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
133 |
"splitpasswd", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
134 |
"splitport", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
135 |
"splituser", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
136 |
"unquote", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
137 |
"url2pathname", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
138 |
"urlencode", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
139 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
140 |
urlreq._registeraliases(urllib2, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
141 |
"AbstractHTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
142 |
"BaseHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
143 |
"build_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
144 |
"FileHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
145 |
"FTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
146 |
"HTTPBasicAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
147 |
"HTTPDigestAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
148 |
"HTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
149 |
"HTTPPasswordMgrWithDefaultRealm", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
150 |
"HTTPSHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
151 |
"install_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
152 |
"ProxyHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
153 |
"Request", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
154 |
"urlopen", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
155 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
156 |
urlreq._registeraliases(urlparse, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
157 |
"urlparse", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
158 |
"urlunparse", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
159 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
160 |
urlerr._registeraliases(urllib2, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
161 |
"HTTPError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
162 |
"URLError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
163 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
164 |
httpserver._registeraliases(BaseHTTPServer, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
165 |
"HTTPServer", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
166 |
"BaseHTTPRequestHandler", |
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(SimpleHTTPServer, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
169 |
"SimpleHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
170 |
)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
171 |
httpserver._registeraliases(CGIHTTPServer, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
172 |
"CGIHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
173 |
)) |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
174 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
175 |
def gethost(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
176 |
return req.get_host() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
177 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
178 |
def getselector(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
179 |
return req.get_selector() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
180 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
181 |
def getfullurl(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
182 |
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
|
183 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
184 |
def getdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
185 |
return req.get_data() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
186 |
|
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
187 |
def hasdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
188 |
return req.has_data() |