61
60
return split(url, exclude_trailing_slash=exclude_trailing_slash)[0]
64
# Private copies of quote and unquote, copied from Python's
65
# urllib module because urllib unconditionally imports socket, which imports
68
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
69
'abcdefghijklmnopqrstuvwxyz'
72
for i, c in zip(xrange(256), str(bytearray(xrange(256)))):
73
_safe_map[c] = c if (i < 128 and c in always_safe) else '%{0:02X}'.format(i)
77
def quote(s, safe='/'):
78
"""quote('abc def') -> 'abc%20def'
80
Each part of a URL, e.g. the path info, the query, etc., has a
81
different set of reserved characters that must be quoted.
83
RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists
84
the following reserved characters.
86
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
89
Each of these characters is reserved in some component of a URL,
90
but not necessarily in all of them.
92
By default, the quote function is intended for quoting the path
93
section of a URL. Thus, it will not encode '/'. This character
94
is reserved, but in typical usage the quote function is being
95
called on a path where the existing slash characters are used as
101
raise TypeError('None object cannot be quoted')
103
cachekey = (safe, always_safe)
105
(quoter, safe) = _safe_quoters[cachekey]
107
safe_map = _safe_map.copy()
108
safe_map.update([(c, c) for c in safe])
109
quoter = safe_map.__getitem__
110
safe = always_safe + safe
111
_safe_quoters[cachekey] = (quoter, safe)
112
if not s.rstrip(safe):
114
return ''.join(map(quoter, s))
117
_hexdig = '0123456789ABCDEFabcdef'
118
_hextochr = dict((a + b, chr(int(a + b, 16)))
119
for a in _hexdig for b in _hexdig)
122
"""unquote('abc%20def') -> 'abc def'."""
130
s += _hextochr[item[:2]] + item[2:]
133
except UnicodeDecodeError:
134
s += unichr(int(item[:2], 16)) + item[2:]
138
63
def escape(relpath):
139
64
"""Escape relpath to be a valid url."""
140
65
if isinstance(relpath, unicode):
141
66
relpath = relpath.encode('utf-8')
142
67
# After quoting and encoding, the path should be perfectly
143
68
# safe as a plain ASCII string, str() just enforces this
144
return str(quote(relpath, safe='/~'))
69
return str(urllib.quote(relpath, safe='/~'))
147
72
def file_relpath(base, path):
641
566
This returns a Unicode path from a URL
643
568
# jam 20060427 URLs are supposed to be ASCII only strings
644
# If they are passed in as unicode, unquote
569
# If they are passed in as unicode, urllib.unquote
645
570
# will return a UNICODE string, which actually contains
646
571
# utf-8 bytes. So we have to ensure that they are
647
572
# plain ASCII strings, or the final .decode will
817
742
port, quoted_path):
818
743
self.scheme = scheme
819
744
self.quoted_host = quoted_host
820
self.host = unquote(self.quoted_host)
745
self.host = urllib.unquote(self.quoted_host)
821
746
self.quoted_user = quoted_user
822
747
if self.quoted_user is not None:
823
self.user = unquote(self.quoted_user)
748
self.user = urllib.unquote(self.quoted_user)
826
751
self.quoted_password = quoted_password
827
752
if self.quoted_password is not None:
828
self.password = unquote(self.quoted_password)
753
self.password = urllib.unquote(self.quoted_password)
830
755
self.password = None
832
757
self.quoted_path = _url_hex_escapes_re.sub(_unescape_safe_chars, quoted_path)
833
self.path = unquote(self.quoted_path)
758
self.path = urllib.unquote(self.quoted_path)
835
760
def __eq__(self, other):
836
761
return (isinstance(other, self.__class__) and