Skip to content
Myr

Merge pull request #3963 from shmuelamar/master

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

fix unicode decode error on py2 when handling redirect without scheme

authored by Cory Benfield · committed by GitHub · parents 84a6040, a3e597c · Browse files · 3 files+40 added−3 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+40 added−3 removed

  • tests:+38 added−1 removed
  • other:+2 added−2 removed

3 files+40 added−3 removed

modified:requests/sessions.py+1 added-1 removed
120 unmodified lines
121122 # Handle redirection without scheme (see: RFC 1808 Section 4)123 if url.startswith('//'):124 parsed_rurl = urlparse(resp.url)125 url = '%s:%s' % (parsed_rurl.scheme, url)125 url = '%s:%s' % (to_native_string(parsed_rurl.scheme), url)126127 # The scheme should be lower case...128 parsed = urlparse(url)129 url = parsed.geturl()
607 unmodified lines
modified:tests/test_lowlevel.py+38 added-1 removed
1# -*- coding: utf-8 -*-23import os3import pytest4import threading5import requests6from requests.compat import quote, is_py378from tests.testserver.server import Server, consume_socket_content910from .utils import override_environ
192 unmodified lines
203 assert len(fake_proxy.handler_results) == 1204205 # it had actual content (not checking for SOCKS protocol for now)206 assert len(fake_proxy.handler_results[0]) > 0207208209def test_redirect_rfc1808_to_non_ascii_location():210 path = u'š'211 expected_path = quote(path.encode('utf8')).encode('ascii')212 expected_path_py3 = b'%C3%85%C2%A1'213 redirect_request = [] # stores the second request to the server214215 def redirect_resp_handler(sock):216 consume_socket_content(sock, timeout=0.5)217 location = u'//{0}:{1}/{2}'.format(host, port, path)218 sock.send(219 b'HTTP/1.1 301 Moved Permanently\r\n'220 b'Content-Length: 0\r\n'221 b'Location: ' + location.encode('utf8') + b'\r\n'222 b'\r\n'223 )224 redirect_request.append(consume_socket_content(sock, timeout=0.5))225 sock.send(b'HTTP/1.1 200 OK\r\n\r\n')226227 close_server = threading.Event()228 server = Server(redirect_resp_handler, wait_to_close_event=close_server)229230 with server as (host, port):231 url = u'http://{0}:{1}'.format(host, port)232 r = requests.get(url=url, allow_redirects=True)233 assert r.status_code == 200234 assert len(r.history) == 1235 assert r.history[0].status_code == 301236237 # currently Python3 not handling non-ASCII redirects (issue #3888)238 if is_py3:239 assert redirect_request[0].startswith(b'GET /' + expected_path_py3 + b' HTTP/1.1')240 else:241 assert redirect_request[0].startswith(b'GET /' + expected_path + b' HTTP/1.1')242243 close_server.set()
modified:AUTHORS.rst+1 added-1 removed
177 unmodified lines
178- Moinuddin Quadri <moin18@gmail.com> (`@moin18 <https://github.com/moin18>`_)179- Matt Kohl (`@mattkohl <https://github.com/mattkohl>`_)180- Jonathan Vanasco (`@jvanasco <https://github.com/jvanasco>`_)181- David Fontenot (`@davidfontenot <https://github.com/davidfontenot>`_)182182- Shmuel Amar (`@shmuelamar <https://github.com/shmuelamar>`_)