6 # GNU General Public License version 2 or any later version. |
6 # GNU General Public License version 2 or any later version. |
7 from __future__ import absolute_import |
7 from __future__ import absolute_import |
8 |
8 |
9 from . import pycompat |
9 from . import pycompat |
10 |
10 |
|
11 _sysstr = pycompat.sysstr |
|
12 |
|
13 class _pycompatstub(object): |
|
14 def __init__(self): |
|
15 self._aliases = {} |
|
16 |
|
17 def _registeraliases(self, origin, items): |
|
18 """Add items that will be populated at the first access""" |
|
19 items = map(_sysstr, items) |
|
20 self._aliases.update( |
|
21 (item.replace(_sysstr('_'), _sysstr('')).lower(), (origin, item)) |
|
22 for item in items) |
|
23 |
|
24 def _registeralias(self, origin, attr, name): |
|
25 """Alias ``origin``.``attr`` as ``name``""" |
|
26 self._aliases[_sysstr(name)] = (origin, _sysstr(attr)) |
|
27 |
|
28 def __getattr__(self, name): |
|
29 try: |
|
30 origin, item = self._aliases[name] |
|
31 except KeyError: |
|
32 raise AttributeError(name) |
|
33 self.__dict__[name] = obj = getattr(origin, item) |
|
34 return obj |
|
35 |
|
36 httpserver = _pycompatstub() |
|
37 urlreq = _pycompatstub() |
|
38 urlerr = _pycompatstub() |
|
39 |
11 if pycompat.ispy3: |
40 if pycompat.ispy3: |
|
41 import urllib.parse |
|
42 urlreq._registeraliases(urllib.parse, ( |
|
43 "splitattr", |
|
44 "splitpasswd", |
|
45 "splitport", |
|
46 "splituser", |
|
47 "urlparse", |
|
48 "urlunparse", |
|
49 )) |
|
50 urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote") |
|
51 import urllib.request |
|
52 urlreq._registeraliases(urllib.request, ( |
|
53 "AbstractHTTPHandler", |
|
54 "BaseHandler", |
|
55 "build_opener", |
|
56 "FileHandler", |
|
57 "FTPHandler", |
|
58 "ftpwrapper", |
|
59 "HTTPHandler", |
|
60 "HTTPSHandler", |
|
61 "install_opener", |
|
62 "pathname2url", |
|
63 "HTTPBasicAuthHandler", |
|
64 "HTTPDigestAuthHandler", |
|
65 "HTTPPasswordMgrWithDefaultRealm", |
|
66 "ProxyHandler", |
|
67 "Request", |
|
68 "url2pathname", |
|
69 "urlopen", |
|
70 )) |
|
71 import urllib.response |
|
72 urlreq._registeraliases(urllib.response, ( |
|
73 "addclosehook", |
|
74 "addinfourl", |
|
75 )) |
|
76 import urllib.error |
|
77 urlerr._registeraliases(urllib.error, ( |
|
78 "HTTPError", |
|
79 "URLError", |
|
80 )) |
|
81 import http.server |
|
82 httpserver._registeraliases(http.server, ( |
|
83 "HTTPServer", |
|
84 "BaseHTTPRequestHandler", |
|
85 "SimpleHTTPRequestHandler", |
|
86 "CGIHTTPRequestHandler", |
|
87 )) |
|
88 |
|
89 # urllib.parse.quote() accepts both str and bytes, decodes bytes |
|
90 # (if necessary), and returns str. This is wonky. We provide a custom |
|
91 # implementation that only accepts bytes and emits bytes. |
|
92 def quote(s, safe=r'/'): |
|
93 s = urllib.parse.quote_from_bytes(s, safe=safe) |
|
94 return s.encode('ascii', 'strict') |
|
95 |
|
96 # urllib.parse.urlencode() returns str. We use this function to make |
|
97 # sure we return bytes. |
|
98 def urlencode(query, doseq=False): |
|
99 s = urllib.parse.urlencode(query, doseq=doseq) |
|
100 return s.encode('ascii') |
|
101 |
|
102 urlreq.quote = quote |
|
103 urlreq.urlencode = urlencode |
12 |
104 |
13 def getfullurl(req): |
105 def getfullurl(req): |
14 return req.full_url |
106 return req.full_url |
15 |
107 |
16 def gethost(req): |
108 def gethost(req): |
23 return req.data |
115 return req.data |
24 |
116 |
25 def hasdata(req): |
117 def hasdata(req): |
26 return req.data is not None |
118 return req.data is not None |
27 else: |
119 else: |
|
120 import BaseHTTPServer |
|
121 import CGIHTTPServer |
|
122 import SimpleHTTPServer |
|
123 import urllib2 |
|
124 import urllib |
|
125 import urlparse |
|
126 urlreq._registeraliases(urllib, ( |
|
127 "addclosehook", |
|
128 "addinfourl", |
|
129 "ftpwrapper", |
|
130 "pathname2url", |
|
131 "quote", |
|
132 "splitattr", |
|
133 "splitpasswd", |
|
134 "splitport", |
|
135 "splituser", |
|
136 "unquote", |
|
137 "url2pathname", |
|
138 "urlencode", |
|
139 )) |
|
140 urlreq._registeraliases(urllib2, ( |
|
141 "AbstractHTTPHandler", |
|
142 "BaseHandler", |
|
143 "build_opener", |
|
144 "FileHandler", |
|
145 "FTPHandler", |
|
146 "HTTPBasicAuthHandler", |
|
147 "HTTPDigestAuthHandler", |
|
148 "HTTPHandler", |
|
149 "HTTPPasswordMgrWithDefaultRealm", |
|
150 "HTTPSHandler", |
|
151 "install_opener", |
|
152 "ProxyHandler", |
|
153 "Request", |
|
154 "urlopen", |
|
155 )) |
|
156 urlreq._registeraliases(urlparse, ( |
|
157 "urlparse", |
|
158 "urlunparse", |
|
159 )) |
|
160 urlerr._registeraliases(urllib2, ( |
|
161 "HTTPError", |
|
162 "URLError", |
|
163 )) |
|
164 httpserver._registeraliases(BaseHTTPServer, ( |
|
165 "HTTPServer", |
|
166 "BaseHTTPRequestHandler", |
|
167 )) |
|
168 httpserver._registeraliases(SimpleHTTPServer, ( |
|
169 "SimpleHTTPRequestHandler", |
|
170 )) |
|
171 httpserver._registeraliases(CGIHTTPServer, ( |
|
172 "CGIHTTPRequestHandler", |
|
173 )) |
28 |
174 |
29 def gethost(req): |
175 def gethost(req): |
30 return req.get_host() |
176 return req.get_host() |
31 |
177 |
32 def getselector(req): |
178 def getselector(req): |