62
61
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:]
65
138
def escape(relpath):
66
139
"""Escape relpath to be a valid url."""
67
140
if isinstance(relpath, unicode):
68
141
relpath = relpath.encode('utf-8')
69
142
# After quoting and encoding, the path should be perfectly
70
143
# safe as a plain ASCII string, str() just enforces this
71
return str(urllib.quote(relpath, safe='/~'))
144
return str(quote(relpath, safe='/~'))
74
147
def file_relpath(base, path):
568
641
This returns a Unicode path from a URL
570
643
# jam 20060427 URLs are supposed to be ASCII only strings
571
# If they are passed in as unicode, urllib.unquote
644
# If they are passed in as unicode, unquote
572
645
# will return a UNICODE string, which actually contains
573
646
# utf-8 bytes. So we have to ensure that they are
574
647
# plain ASCII strings, or the final .decode will
744
817
port, quoted_path):
745
818
self.scheme = scheme
746
819
self.quoted_host = quoted_host
747
self.host = urllib.unquote(self.quoted_host)
820
self.host = unquote(self.quoted_host)
748
821
self.quoted_user = quoted_user
749
822
if self.quoted_user is not None:
750
self.user = urllib.unquote(self.quoted_user)
823
self.user = unquote(self.quoted_user)
753
826
self.quoted_password = quoted_password
754
827
if self.quoted_password is not None:
755
self.password = urllib.unquote(self.quoted_password)
828
self.password = unquote(self.quoted_password)
757
830
self.password = None
759
832
self.quoted_path = _url_hex_escapes_re.sub(_unescape_safe_chars, quoted_path)
760
self.path = urllib.unquote(self.quoted_path)
833
self.path = unquote(self.quoted_path)
762
835
def __eq__(self, other):
763
836
return (isinstance(other, self.__class__) and