Merge branch 'master' of github.com:requests/requests
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 4e7e270, 86914e2 · Browse files · 5 files·+57 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+57 added−13 removed
- tests:+26 added−5 removed
- other:+31 added−8 removed
Provenance
- Commit
fdcb717- Tree
4f0c7f0- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
4e7e270- Parent 2
86914e2
5 files changed+57 added−13 removed
modified:docs/dev/contributing.rst+1 added-1 removed
39 unmodified lines40 41 **All contributions are welcome**, as long as42 everyone involved is treated with respect.43 44-.. _be cordial or be on your way: http://kennethreitz.org/be-cordial-or-be-on-your-way/44+.. _be cordial or be on your way: https://www.kennethreitz.org/essays/be-cordial-or-be-on-your-way45 46 .. _early-feedback:47 48 Get Early Feedback160 unmodified linesmodified:requests/sessions.py+18 added-6 removed
113 unmodified lines114 location = location.encode('latin1')115 return to_native_string(location, 'utf8')116 return None117 118+ def should_strip_auth(self, old_url, new_url):119+ """Decide whether Authorization header should be removed when redirecting"""120+ old_parsed = urlparse(old_url)121+ new_parsed = urlparse(new_url)122+ if old_parsed.hostname != new_parsed.hostname:123+ return True124+ # Special case: allow http -> https redirect when using the standard125+ # ports. This isn't specified by RFC 7235, but is kept to avoid126+ # breaking backwards compatibility with older versions of requests127+ # that allowed any redirects on the same host.128+ if (old_parsed.scheme == 'http' and old_parsed.port in (80, None)129+ and new_parsed.scheme == 'https' and new_parsed.port in (443, None)):130+ return False131+ # Standard case: root URI must match132+ return old_parsed.port != new_parsed.port or old_parsed.scheme != new_parsed.scheme133+134 def resolve_redirects(self, resp, req, stream=False, timeout=None,135 verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs):136 """Receives a Response. Returns a generator of Responses or Requests."""137 113 unmodified lines251 """252 headers = prepared_request.headers253 url = prepared_request.url254 239- if 'Authorization' in headers:255+ if 'Authorization' in headers and self.should_strip_auth(response.request.url, url):256 # If we get redirected to a new host, we should strip out any257 # authentication headers.242- original_parsed = urlparse(response.request.url)243- redirect_parsed = urlparse(url)258+ del headers['Authorization']259 245- if (original_parsed.hostname != redirect_parsed.hostname):246- del headers['Authorization']247-260 # .netrc might have more auth for us on our new host.261 new_auth = get_netrc_auth(url) if self.trust_env else None262 if new_auth is not None:263 prepared_request.prepare_auth(new_auth)498 unmodified linesmodified:tests/test_requests.py+26 added-5 removed
1571 unmodified lines1572 req = requests.Request('GET', test_url)1573 preq = req.prepare()1574 assert test_url == preq.url1575 1576- @pytest.mark.xfail(raises=ConnectionError)1577- def test_auth_is_stripped_on_redirect_off_host(self, httpbin):1576+ def test_auth_is_stripped_on_http_downgrade(self, httpbin, httpbin_secure, httpbin_ca_bundle):1577 r = requests.get(1579- httpbin('redirect-to'),1580- params={'url': 'http://www.google.co.uk'},1578+ httpbin_secure('redirect-to'),1579+ params={'url': httpbin('get')},1580 auth=('user', 'pass'),1581+ verify=httpbin_ca_bundle1582 )1583 assert r.history[0].request.headers['Authorization']1584- assert not r.request.headers.get('Authorization', '')1584+ assert 'Authorization' not in r.request.headers1585 1586 def test_auth_is_retained_for_redirect_on_host(self, httpbin):1587 r = requests.get(httpbin('redirect/1'), auth=('user', 'pass'))1588 h1 = r.history[0].request.headers['Authorization']1589 h2 = r.request.headers['Authorization']1590 1591 assert h1 == h21592 1593+ def test_should_strip_auth_host_change(self):1594+ s = requests.Session()1595+ assert s.should_strip_auth('http://example.com/foo', 'http://another.example.com/')1596+1597+ def test_should_strip_auth_http_downgrade(self):1598+ s = requests.Session()1599+ assert s.should_strip_auth('https://example.com/foo', 'http://example.com/bar')1600+1601+ def test_should_strip_auth_https_upgrade(self):1602+ s = requests.Session()1603+ assert not s.should_strip_auth('http://example.com/foo', 'https://example.com/bar')1604+ assert not s.should_strip_auth('http://example.com:80/foo', 'https://example.com/bar')1605+ assert not s.should_strip_auth('http://example.com/foo', 'https://example.com:443/bar')1606+ # Non-standard ports should trigger stripping1607+ assert s.should_strip_auth('http://example.com:8080/foo', 'https://example.com/bar')1608+ assert s.should_strip_auth('http://example.com/foo', 'https://example.com:8443/bar')1609+1610+ def test_should_strip_auth_port_change(self):1611+ s = requests.Session()1612+ assert s.should_strip_auth('http://example.com:1234/foo', 'https://example.com:4321/bar')1613+1614 def test_manual_redirect_with_partial_body_read(self, httpbin):1615 s = requests.Session()1616 r1 = s.get(httpbin('redirect/2'), allow_redirects=False, stream=True)1617 assert r1.is_redirect880 unmodified linesmodified:README.rst+4 added
1 Requests: HTTP for Humans2 =========================3 4+**If you're interested in supporting Kenneth Reitz projects financially, visit `this link <https://cash.me/$KennethReitz>`_. It really helps with motivation!**5+6 .. image:: https://img.shields.io/pypi/v/requests.svg7 :target: https://pypi.org/project/requests/8 9 .. image:: https://img.shields.io/pypi/l/requests.svg36 unmodified lines46 See `the similar code, sans Requests <https://gist.github.com/973705>`_.47 48 .. image:: https://raw.githubusercontent.com/requests/requests/master/docs/_static/requests-logo-small.png49 :target: http://docs.python-requests.org/50+ 51 52 53+54 Requests allows you to send *organic, grass-fed* HTTP/1.1 requests, without the55 need for manual labor. There's no need to manually add query strings to your56 URLs, or to form-encode your POST data. Keep-alive and HTTP connection pooling57 are 100% automatic, thanks to `urllib3 <https://github.com/shazow/urllib3>`_.57 unmodified linesmodified:setup.py+8 added-1 removed
52 unmodified lines53 'urllib3>=1.21.1,<1.24',54 'certifi>=2017.4.17'55 56 ]57-test_requirements = ['pytest-httpbin==0.0.7', 'pytest-cov', 'pytest-mock', 'pytest-xdist', 'PySocks>=1.5.6, !=1.5.7', 'pytest>=2.8.0']57+test_requirements = [58+ 'pytest-httpbin==0.0.7',59+ 'pytest-cov',60+ 'pytest-mock',61+ 'pytest-xdist',62+ 'PySocks>=1.5.6, !=1.5.7',63+ 'pytest>=2.8.0'64+]65 66 about = {}67 with open(os.path.join(here, 'requests', '__version__.py'), 'r', 'utf-8') as f:68 exec(f.read(), about)47 unmodified lines