|
|
b9c088 |
diff --git a/requests/sessions.py b/requests/sessions.py
|
|
|
b9c088 |
index a448bd8..d73d700 100644
|
|
|
b9c088 |
--- a/requests/sessions.py
|
|
|
b9c088 |
+++ b/requests/sessions.py
|
|
|
b9c088 |
@@ -19,7 +19,7 @@ from .cookies import (
|
|
|
b9c088 |
from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT
|
|
|
b9c088 |
from .hooks import default_hooks, dispatch_hook
|
|
|
b9c088 |
from ._internal_utils import to_native_string
|
|
|
b9c088 |
-from .utils import to_key_val_list, default_headers
|
|
|
b9c088 |
+from .utils import to_key_val_list, default_headers, DEFAULT_PORTS
|
|
|
b9c088 |
from .exceptions import (
|
|
|
b9c088 |
TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)
|
|
|
b9c088 |
|
|
|
b9c088 |
@@ -128,8 +128,17 @@ class SessionRedirectMixin(object):
|
|
|
b9c088 |
if (old_parsed.scheme == 'http' and old_parsed.port in (80, None)
|
|
|
b9c088 |
and new_parsed.scheme == 'https' and new_parsed.port in (443, None)):
|
|
|
b9c088 |
return False
|
|
|
b9c088 |
+
|
|
|
b9c088 |
+ # Handle default port usage corresponding to scheme.
|
|
|
b9c088 |
+ changed_port = old_parsed.port != new_parsed.port
|
|
|
b9c088 |
+ changed_scheme = old_parsed.scheme != new_parsed.scheme
|
|
|
b9c088 |
+ default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)
|
|
|
b9c088 |
+ if (not changed_scheme and old_parsed.port in default_port
|
|
|
b9c088 |
+ and new_parsed.port in default_port):
|
|
|
b9c088 |
+ return False
|
|
|
b9c088 |
+
|
|
|
b9c088 |
# Standard case: root URI must match
|
|
|
b9c088 |
- return old_parsed.port != new_parsed.port or old_parsed.scheme != new_parsed.scheme
|
|
|
b9c088 |
+ return changed_port or changed_scheme
|
|
|
b9c088 |
|
|
|
b9c088 |
def resolve_redirects(self, resp, req, stream=False, timeout=None,
|
|
|
b9c088 |
verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs):
|
|
|
b9c088 |
diff --git a/requests/utils.py b/requests/utils.py
|
|
|
b9c088 |
index 0ce7fe1..04145c8 100644
|
|
|
b9c088 |
--- a/requests/utils.py
|
|
|
b9c088 |
+++ b/requests/utils.py
|
|
|
b9c088 |
@@ -38,6 +38,8 @@ NETRC_FILES = ('.netrc', '_netrc')
|
|
|
b9c088 |
|
|
|
b9c088 |
DEFAULT_CA_BUNDLE_PATH = certs.where()
|
|
|
b9c088 |
|
|
|
b9c088 |
+DEFAULT_PORTS = {'http': 80, 'https': 443}
|
|
|
b9c088 |
+
|
|
|
b9c088 |
|
|
|
b9c088 |
if sys.platform == 'win32':
|
|
|
b9c088 |
# provide a proxy_bypass version on Windows without DNS lookups
|
|
|
b9c088 |
diff --git a/tests/test_requests.py b/tests/test_requests.py
|
|
|
b9c088 |
index f46561e..f99fdaf 100644
|
|
|
b9c088 |
--- a/tests/test_requests.py
|
|
|
b9c088 |
+++ b/tests/test_requests.py
|
|
|
b9c088 |
@@ -1611,6 +1611,17 @@ class TestRequests:
|
|
|
b9c088 |
s = requests.Session()
|
|
|
b9c088 |
assert s.should_strip_auth('http://example.com:1234/foo', 'https://example.com:4321/bar')
|
|
|
b9c088 |
|
|
|
b9c088 |
+ @pytest.mark.parametrize(
|
|
|
b9c088 |
+ 'old_uri, new_uri', (
|
|
|
b9c088 |
+ ('https://example.com:443/foo', 'https://example.com/bar'),
|
|
|
b9c088 |
+ ('http://example.com:80/foo', 'http://example.com/bar'),
|
|
|
b9c088 |
+ ('https://example.com/foo', 'https://example.com:443/bar'),
|
|
|
b9c088 |
+ ('http://example.com/foo', 'http://example.com:80/bar')
|
|
|
b9c088 |
+ ))
|
|
|
b9c088 |
+ def test_should_strip_auth_default_port(self, old_uri, new_uri):
|
|
|
b9c088 |
+ s = requests.Session()
|
|
|
b9c088 |
+ assert not s.should_strip_auth(old_uri, new_uri)
|
|
|
b9c088 |
+
|
|
|
b9c088 |
def test_manual_redirect_with_partial_body_read(self, httpbin):
|
|
|
b9c088 |
s = requests.Session()
|
|
|
b9c088 |
r1 = s.get(httpbin('redirect/2'), allow_redirects=False, stream=True)
|