diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-07-24 19:06:49 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-07-24 19:38:44 -0700 |
commit | a682074e9ed5e94683389f67cc192e6547d6310e (patch) | |
tree | 4c607f3e9ff94a2b6f0b67d7ef86629e8396218f /netlib/http/url.py | |
parent | c1bd4dabc8a03b17ea55b5e7b4ab444f59b95e2d (diff) | |
download | mitmproxy-a682074e9ed5e94683389f67cc192e6547d6310e.tar.gz mitmproxy-a682074e9ed5e94683389f67cc192e6547d6310e.tar.bz2 mitmproxy-a682074e9ed5e94683389f67cc192e6547d6310e.zip |
improve query/path_components getter/setter
Diffstat (limited to 'netlib/http/url.py')
-rw-r--r-- | netlib/http/url.py | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/netlib/http/url.py b/netlib/http/url.py index 1c8c007a..076854b9 100644 --- a/netlib/http/url.py +++ b/netlib/http/url.py @@ -82,19 +82,51 @@ def unparse(scheme, host, port, path=""): def encode(s): - # type: (six.text_type, bytes) -> str + # type: Sequence[Tuple[str,str]] -> str """ Takes a list of (key, value) tuples and returns a urlencoded string. """ - s = [tuple(i) for i in s] - return urllib.parse.urlencode(s, False) + if six.PY2: + return urllib.parse.urlencode(s, False) + else: + return urllib.parse.urlencode(s, False, errors="surrogateescape") def decode(s): """ - Takes a urlencoded string and returns a list of (key, value) tuples. + Takes a urlencoded string and returns a list of surrogate-escaped (key, value) tuples. + """ + if six.PY2: + return urllib.parse.parse_qsl(s, keep_blank_values=True) + else: + return urllib.parse.parse_qsl(s, keep_blank_values=True, errors='surrogateescape') + + +def quote(b, safe="/"): + """ + Returns: + An ascii-encodable str. + """ + # type: (str) -> str + if six.PY2: + return urllib.parse.quote(b, safe=safe) + else: + return urllib.parse.quote(b, safe=safe, errors="surrogateescape") + + +def unquote(s): """ - return urllib.parse.parse_qsl(s, keep_blank_values=True) + Args: + s: A surrogate-escaped str + Returns: + A surrogate-escaped str + """ + # type: (str) -> str + + if six.PY2: + return urllib.parse.unquote(s) + else: + return urllib.parse.unquote(s, errors="surrogateescape") def hostport(scheme, host, port): |