Merge pull request #4851 from requests/default_port_handling
Before adoption: this commit came in with the history the project adopted, so no landing records it.
proper handling for default ports in auth stripping
authored by Ian Stapleton Cordasco · committed by GitHub · parents 7c812e9, ea9436a · Browse files · 3 files·+24 added−2 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+24 added−2 removed
- tests:+11 added−0 removed
- other:+13 added−2 removed
Provenance
- Commit
eaab47f- Tree
1673095- Author
Ian Stapleton Cordasco <graffatcolmingov@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
7c812e9- Parent 2
ea9436a
3 files changed+24 added−2 removed
modified:requests/sessions.py+11 added-2 removed
17 unmodified lines18 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)19 from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT20 from .hooks import default_hooks, dispatch_hook21 from ._internal_utils import to_native_string22-from .utils import to_key_val_list, default_headers22+from .utils import to_key_val_list, default_headers, DEFAULT_PORTS23 from .exceptions import (24 TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)25 26 from .structures import CaseInsensitiveDict100 unmodified lines127 # 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+132+ # Handle default port usage corresponding to scheme.133+ changed_port = old_parsed.port != new_parsed.port134+ changed_scheme = old_parsed.scheme != new_parsed.scheme135+ default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)136+ if (not changed_scheme and old_parsed.port in default_port137+ and new_parsed.port in default_port):138+ return False139+140 # Standard case: root URI must match132- return old_parsed.port != new_parsed.port or old_parsed.scheme != new_parsed.scheme141+ return changed_port or changed_scheme142 143 def resolve_redirects(self, resp, req, stream=False, timeout=None,144 verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs):145 """Receives a Response. Returns a generator of Responses or Requests."""625 unmodified linesmodified:requests/utils.py+2 added
36 unmodified lines37 NETRC_FILES = ('.netrc', '_netrc')38 39 DEFAULT_CA_BUNDLE_PATH = certs.where()40 41+DEFAULT_PORTS = {'http': 80, 'https': 443}42 43+44 if sys.platform == 'win32':45 # provide a proxy_bypass version on Windows without DNS lookups46 47 def proxy_bypass_registry(host):930 unmodified linesmodified:tests/test_requests.py+11 added
1609 unmodified lines1610 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+ @pytest.mark.parametrize(1615+ 'old_uri, new_uri', (1616+ ('https://example.com:443/foo', 'https://example.com/bar'),1617+ ('http://example.com:80/foo', 'http://example.com/bar'),1618+ ('https://example.com/foo', 'https://example.com:443/bar'),1619+ ('http://example.com/foo', 'http://example.com:80/bar')1620+ ))1621+ def test_should_strip_auth_default_port(self, old_uri, new_uri):1622+ s = requests.Session()1623+ assert not s.should_strip_auth(old_uri, new_uri)1624+1625 def test_manual_redirect_with_partial_body_read(self, httpbin):1626 s = requests.Session()1627 r1 = s.get(httpbin('redirect/2'), allow_redirects=False, stream=True)1628 assert r1.is_redirect890 unmodified lines