Skip to content
Myr

Fix for #1362.

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

`PreparedRequest.prepare_url` incorrectly applied IDNA encoding to the URLs entire `netloc`. It should only be encoding the hostname portion of the URL. IDNA encoding was limiting the user info, host, and port segments to be a maximum of 63 characters which causes problems for all by the most trivial user + password combinations. - Replaced usage of `urlparse` in `PreparedRequest.prepare_url` with `urllib3` equivalent. - Modified IDNA encoding section so that it only encodes the host portion of the URL.

authored by Dave Shawley · committed by Dave Shawley · parent eacb91a · Browse files · 3 files+25 added−7 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+25 added−7 removed

  • other:+25 added−7 removed

3 files+25 added−7 removed

modified:requests/models.py+15 added-7 removed
16 unmodified lines
1718from .auth import HTTPBasicAuth19from .cookies import cookiejar_from_dict, get_cookie_header20from .packages.urllib3.filepost import encode_multipart_formdata21from .packages.urllib3.util import parse_url22from .exceptions import HTTPError, RequestException, MissingSchema, InvalidURL23from .utils import (24 guess_filename, get_auth_from_url, requote_uri,25 stream_decode_response_unicode, to_key_val_list, parse_header_links,
258 unmodified lines
284 except UnicodeDecodeError:285 pass286287 # Support for unicode domain names and paths.287 scheme, netloc, path, _params, query, fragment = urlparse(url)288 scheme, auth, host, port, path, query, fragment = parse_url(url)289290 if not scheme:291 raise MissingSchema("Invalid URL %r: No schema supplied" % url)292292 if not netloc:293 raise InvalidURL("Invalid URL %t: No netloc supplied" % url)293 if not host:294 raise InvalidURL("Invalid URL %t: No host supplied" % url)295296 # Only want to apply IDNA to the hostname297 try:296 netloc = netloc.encode('idna').decode('utf-8')298 host = host.encode('idna').decode('utf-8')299 except UnicodeError:300 raise InvalidURL('URL has an invalid label.')301302 # Carefully reconstruct the network location303 netloc = auth or ''304 if netloc:305 netloc += '@'306 netloc += host307 if port:308 netloc += ':' + str(port)309310 # Bare domains aren't valid URLs.311 if not path:312 path = '/'313
3 unmodified lines
317 if isinstance(netloc, str):318 netloc = netloc.encode('utf-8')319 if isinstance(path, str):320 path = path.encode('utf-8')311 if isinstance(_params, str):312 _params = _params.encode('utf-8')321 if isinstance(query, str):322 query = query.encode('utf-8')323 if isinstance(fragment, str):324 fragment = fragment.encode('utf-8')
4 unmodified lines
329 query = '%s&%s' % (query, enc_params)330 else:331 query = enc_params332325 url = requote_uri(urlunparse([scheme, netloc, path, _params, query, fragment]))333 url = requote_uri(urlunparse([scheme, netloc, path, None, query, fragment]))334 self.url = url335336 def prepare_headers(self, headers):337 """Prepares the given HTTP headers."""
349 unmodified lines
modified:AUTHORS.rst+1 added
125 unmodified lines
126- Bryce Boe <bbzbryce@gmail.com> @bboe127- Colin Dunklau <colin.dunklau@gmail.com> @cdunklau128- Hugo Osvaldo Barrera <hugo@osvaldobarrera.com.ar> @hobarrera129- Łukasz Langa <lukasz@langa.pl> @llanga130- Dave Shawley <daveshawley@gmail.com>
modified:test_requests.py+9 added
519 unmodified lines
520 s2.mount('https://', HTTPAdapter())521 self.assertTrue('http://' in s2.adapters)522 self.assertTrue('https://' in s2.adapters)523524 def test_long_authinfo_in_url(self):525 url = 'http://{0}:{1}@{2}:9000/path?query#frag'.format(526 'E8A3BE87-9E3F-4620-8858-95478E385B5B',527 'EA770032-DA4D-4D84-8CE9-29C6D910BF1E',528 'exactly-------------sixty-----------three------------characters',529 )530 r = requests.Request('GET', url).prepare()531 self.assertEqual(r.url, url)532533534class TestCaseInsensitiveDict(unittest.TestCase):535536 def test_mapping_init(self):537 cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
142 unmodified lines