Skip to content

Commit

Permalink
copy path-quoting behavior from requests
Browse files Browse the repository at this point in the history
  • Loading branch information
harvimt committed Sep 6, 2014
1 parent 8560043 commit e24caf5
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@
HTTP_PORT = 80
HTTPS_PORT = 443

#<copied from="requests/utils.py">

# The unreserved URI characters (RFC 3986)
UNRESERVED_SET = frozenset(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ "0123456789-._~")


def unquote_unreserved(uri):
"""Un-escape any percent-escape sequences in a URI that are unreserved
characters. This leaves all reserved, illegal and non-ASCII bytes encoded.
"""
parts = uri.split('%')
for i in range(1, len(parts)):
h = parts[i][0:2]
if len(h) == 2 and h.isalnum():
try:
c = chr(int(h, 16))
except ValueError:
raise InvalidURL("Invalid percent-escape sequence: '%s'" % h)

if c in UNRESERVED_SET:
parts[i] = c + parts[i][2:]
else:
parts[i] = '%' + parts[i]
else:
parts[i] = '%' + parts[i]
return ''.join(parts)
# </copied>


@asyncio.coroutine
def request(method, url, *,
Expand Down Expand Up @@ -259,7 +289,7 @@ def update_version(self, version):
raise ValueError(
'Can not parse http version number: {}'
.format(version)) from None
self.version = version
self.version = version

def update_path(self, params):
"""Build path."""
Expand All @@ -280,8 +310,10 @@ def update_path(self, params):
else:
query = params

self.path = urllib.parse.urlunsplit(
('', '', urllib.parse.quote(path, safe='/%'), query, fragment))

path = urllib.parse.urlunsplit(('', '', path, query, fragment))

self.path = urllib.parse.quote(unquote_unreserved(path), safe="!#$%&'()*+,/:;=?@[]~")

def update_headers(self, headers):
"""Update request headers."""
Expand Down

0 comments on commit e24caf5

Please sign in to comment.