Skip to content
Myr

Rewrite unquote_unreserved based on urllib.unquote

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is almost entirely taken from the unquote implementation in urllib, slightly modified for the case in hand. It now deals with invalid %encodings rather than exploding, and the code is somewhat simpler.

authored by Jonty Wareing · committed by Jonty Wareing · parent e28c1c9 · Browse files · 2 files+12 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+12 added−14 removed

  • other:+12 added−14 removed

2 files+12 added−14 removed

modified:requests/utils.py+10 added-13 removed
439 unmodified lines
440 return gen441442443# The unreserved URI characters (RFC 3986)444UNRESERVED_SET = frozenset(444_unreserved_set = frozenset(445 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"446 + "0123456789-._~")447448_unreserved_hextochr = dict((c.encode('hex'), c) for c in _unreserved_set)449450def unquote_unreserved(uri):451 """Un-escape any percent-escape sequences in a URI that are unreserved452 characters.453 This leaves all reserved, illegal and non-ASCII bytes encoded.454 """454 parts = uri.split('%')455 for i in range(1, len(parts)):456 h = parts[i][0:2]457 if len(h) == 2:458 c = chr(int(h, 16))459 if c in UNRESERVED_SET:460 parts[i] = c + parts[i][2:]461 else:462 parts[i] = '%' + parts[i]463 else:464 parts[i] = '%' + parts[i]465 return ''.join(parts)455 res = uri.split('%')456 s = res[0]457 for item in res[1:]:458 try:459 s += _unreserved_hextochr[item[:2].lower()] + item[2:]460 except (KeyError, UnicodeDecodeError):461 s += '%' + item462 return s463464465def requote_uri(uri):466 """Re-quote the given URI.
8 unmodified lines
modified:AUTHORS.rst+2 added-1 removed
91 unmodified lines
92- Jonathan Drosdeck93- Jiri Machalek94- Steve Pulec95- Michael Kelly96- Michael Newman <newmaniese@gmail.com>96- Michael Newman <newmaniese@gmail.com>97- Jonty Wareing <jonty@jonty.co.uk>