Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python-ecosys/requests/requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ def request(
elif l.startswith(b"Location:") and not 200 <= status <= 299:
if status in [301, 302, 303, 307, 308]:
redirect = str(l[10:-2], "utf-8")
if redirect.startswith("/"):
if redirect.startswith("//"):
redirect = proto + redirect
elif redirect.startswith("/"):
redirect = proto + "//" + host + ":" + str(port) + redirect
else:
raise NotImplementedError("Redirect %d not yet supported" % status)
Expand Down
19 changes: 19 additions & 0 deletions python-ecosys/requests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,24 @@ def test_redirect_relative():
socket.socket = lambda *a, **k: Socket()


def test_redirect_protocol_relative():
server = iter(
[
b"HTTP/1.1 301 OK\r\nLocation: //example.com/index\r\n\r\n",
SERVER_RESPONSE_200_OK,
]
)
socket.socket = lambda *a, **k: Socket(next(server))

response = requests.request("GET", "http://example.com")

assert response.raw._write_buffer.getvalue() == (
b"GET /index HTTP/1.1\r\nConnection: close\r\nHost: example.com\r\n\r\n"
), format_message(response)

socket.socket = lambda *a, **k: Socket()


test_simple_get()
test_get_auth()
test_get_custom_header()
Expand All @@ -331,3 +349,4 @@ def test_redirect_relative():
test_raw_readinto_content_length()
test_redirect_absolute()
test_redirect_relative()
test_redirect_protocol_relative()
Loading