9 |
9 |
10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO |
10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO |
11 import __builtin__ |
11 import __builtin__ |
12 from i18n import _ |
12 from i18n import _ |
13 import keepalive, util |
13 import keepalive, util |
14 |
|
15 def _urlunparse(scheme, netloc, path, params, query, fragment, url): |
|
16 '''Handle cases where urlunparse(urlparse(x://)) doesn't preserve the "//"''' |
|
17 result = urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) |
|
18 if (scheme and |
|
19 result.startswith(scheme + ':') and |
|
20 not result.startswith(scheme + '://') and |
|
21 url.startswith(scheme + '://') |
|
22 ): |
|
23 result = scheme + '://' + result[len(scheme + ':'):] |
|
24 return result |
|
25 |
14 |
26 class url(object): |
15 class url(object): |
27 """Reliable URL parser. |
16 """Reliable URL parser. |
28 |
17 |
29 This parses URLs and provides attributes for the following |
18 This parses URLs and provides attributes for the following |
213 self.user, self.passwd or '')) |
202 self.user, self.passwd or '')) |
214 |
203 |
215 def has_scheme(path): |
204 def has_scheme(path): |
216 return bool(url(path).scheme) |
205 return bool(url(path).scheme) |
217 |
206 |
218 def hidepassword(url): |
207 def hidepassword(u): |
219 '''hide user credential in a url string''' |
208 '''hide user credential in a url string''' |
220 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
209 u = url(u) |
221 netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc) |
210 if u.passwd: |
222 return _urlunparse(scheme, netloc, path, params, query, fragment, url) |
211 u.passwd = '***' |
223 |
212 return str(u) |
224 def removeauth(url): |
213 |
|
214 def removeauth(u): |
225 '''remove all authentication information from a url string''' |
215 '''remove all authentication information from a url string''' |
226 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
216 u = url(u) |
227 netloc = netloc[netloc.find('@')+1:] |
217 u.user = u.passwd = None |
228 return _urlunparse(scheme, netloc, path, params, query, fragment, url) |
218 return str(u) |
229 |
219 |
230 def netlocsplit(netloc): |
220 def netlocsplit(netloc): |
231 '''split [user[:passwd]@]host[:port] into 4-tuple.''' |
221 '''split [user[:passwd]@]host[:port] into 4-tuple.''' |
232 |
222 |
233 a = netloc.find('@') |
223 a = netloc.find('@') |