Skip to content
Myr

Merge branch 'py3-uri-encoding' of https://github.com/mgiuca/requests into develop

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 5a2a78f, 1ffce4f · Browse files · 3 files+74 added−9 removed
This commit

State

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

Diff stats

3 files+74 added−9 removed

  • other:+74 added−9 removed

3 files+74 added−9 removed

modified:requests/models.py+1 added-4 removed
322 unmodified lines
323 if is_py2:324 if isinstance(path, str):325 path = path.encode('utf-8')326327 path = requote_path(path)327 path = requote_path(path)328329 url = (urlunparse([ scheme, netloc, path, params, query, fragment ]))330331 if self._enc_params:
19 unmodified lines
351 path = p.path352 if not path:353 path = '/'354355 if is_py3:356 path = quote(path.encode('utf-8'))357355 url.append(path)356357 query = p.query358 if query:
435 unmodified lines
modified:requests/utils.py+27 added-3 removed
394 unmodified lines
395 rv = buf + dec.flush()396 if rv:397 yield rv398399# The unreserved URI characters (RFC 3986)400UNRESERVED_SET = frozenset(401 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"402 + "0123456789-._~")403404def unquote_unreserved(uri):405 """Un-escape any percent-escape sequences in a URI that are unreserved406 characters.407 This leaves all reserved, illegal and non-ASCII bytes encoded.408 """409 parts = uri.split('%')410 for i in range(1, len(parts)):411 h = parts[i][0:2]412 if len(h) == 2:413 c = chr(int(h, 16))414 if c in UNRESERVED_SET:415 parts[i] = c + parts[i][2:]416 else:417 parts[i] = '%' + parts[i]418 else:419 parts[i] = '%' + parts[i]420 return ''.join(parts)421422def requote_path(path):423 """Re-quote the given URL path component.424425 This function passes the given path through an unquote/quote cycle to426 ensure that it is fully and consistently quoted.427 """406 parts = path.split(b"/")407 parts = (quote(unquote(part), safe=b"") for part in parts)408 return b"/".join(parts)428 # Unquote only the unreserved characters429 # Then quote only illegal characters (do not quote reserved, unreserved,430 # or '%')431 return quote(unquote_unreserved(path), safe="!#$%&'()*+,/:;=?@[]~")432 return "/".join(parts)
modified:test_requests.py+46 added-2 removed
72 unmodified lines
73 self.assertRaises(ValueError, get, 'hiwpefhipowhefopw')747576 def test_path_is_not_double_encoded(self):77 request = requests.Request("http://0.0.0.0/get/~test")77 request = requests.Request("http://0.0.0.0/get/test case")7879 assert request.path_url == "/get/%7Etest"79 assert request.path_url == "/get/test%20case"8081 def test_HTTP_200_OK_GET(self):82 r = get(httpbin('/get'))83 self.assertEqual(r.status_code, 200)
32 unmodified lines
116117 requests.get(url=httpbin('get'), headers=heads)118119120 def test_session_with_escaped_url(self):121 # Test a URL that contains percent-escaped characters122 # This URL should not be modified (double-escaped)123 # Tests:124 # - Quoted illegal characters ("%20" (' '), "%3C" ('<'), "%3E" ('>'))125 # - Quoted reserved characters ("%25" ('%'), "%23" ('#'), "%2F" ('/'))126 # - Quoted non-ASCII characters ("%C3%98", "%C3%A5")127 path_fully_escaped = '%3Ca%25b%23c%2Fd%3E/%C3%98%20%C3%A5'128 url = httpbin('get/' + path_fully_escaped)129 response = get(url)130 self.assertEqual(response.url, httpbin('get/' + path_fully_escaped))131132 # Test that illegal characters in a path get properly percent-escaped133 # Tests:134 # - Bare illegal characters (space, '<')135 # - Bare non-ASCII characters ('\u00d8')136 path = u'<a%25b%23c%2Fd%3E/\u00d8 %C3%A5'137 url = httpbin('get/' + path)138 response = get(url)139 self.assertEqual(response.url, httpbin('get/' + path_fully_escaped))140141 # Test that reserved characters in a path do not get percent-escaped142 # Tests:143 # - All reserved characters (RFC 3986), except '?', '#', '[' and ']',144 # which are not allowed in the path, and ';' which delimits145 # parameters.146 # All such characters must be allowed bare in path, and must not be147 # encoded.148 # - Special unreserved characters (RFC 3986), which should not be149 # encoded (even though it wouldn't hurt).150 path_reserved = '!$&\'()*+,/:=@-._~'151 url = httpbin('get/' + path_reserved)152 response = get(url)153 self.assertEqual(response.url, httpbin('get/' + path_reserved))154155 # Test that percent-encoded unreserved characters in a path get156 # normalised to their un-encoded forms.157 path_unreserved = 'ABCDwxyz1234-._~'158 path_unreserved_escaped = '%41%42%43%44%77%78%79%7A%31%32%33%34%2D%2E%5F%7E'159 url = httpbin('get/' + path_unreserved_escaped)160 response = get(url)161 self.assertEqual(response.url, httpbin('get/' + path_unreserved))162163164 def test_user_agent_transfers(self):165 """Issue XX"""166167 heads = {
613 unmodified lines