Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow redirects for file downloads #12309

Merged
Merged
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
8 changes: 7 additions & 1 deletion kolibri/utils/file_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,15 @@ def _set_headers(self):
if self._headers_set:
return

response = self.session.head(self.source, timeout=self.timeout)
response = self.session.head(
self.source, timeout=self.timeout, allow_redirects=True
)
response.raise_for_status()

if response.url != self.source:
logger.debug("Redirected from {} to {}".format(self.source, response.url))
self.source = response.url

self.compressed = bool(response.headers.get("content-encoding", ""))

self.content_length_header = "content-length" in response.headers
Expand Down
28 changes: 28 additions & 0 deletions kolibri/utils/tests/test_file_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def mock_get_request(self, url, headers=None, **kwargs):

range_headers = self.get_headers(range_data, start, end)
mock_response = MagicMock()
mock_response.url = url

# Because of the way that requests iterates over the content, we need to
# keep track of whether the content has been exhausted, so that we can
Expand Down Expand Up @@ -166,6 +167,7 @@ def content(other):
def mock_head_request(self, url, **kwargs):
mock_response = MagicMock()
mock_response.headers = self.get_headers(self.content, None, None)
mock_response.url = url
return mock_response

def set_session_mock(self):
Expand Down Expand Up @@ -368,6 +370,32 @@ def test_file_download_500_raise(self):
with self.assertRaises(HTTPError):
fd.run()

def test_file_download_redirect(self):
mock_response = MagicMock()
mock_response.status_code = 302
mock_response.url = "https://example.com/testfile"

def mock_head_request_redirect(url, **kwargs):
mock_response = self.mock_head_request(url, **kwargs)
mock_response.status_code = 302
mock_response.url = "https://example.com/testfile"
return mock_response

self.mock_session.head.side_effect = mock_head_request_redirect

with FileDownload(
self.source,
self.dest,
self.checksum,
session=self.mock_session,
retry_wait=0,
full_ranges=self.full_ranges,
) as fd:
fd.run()

self.source = "https://example.com/testfile"
self._assert_downloaded_content()

def test_file_download_request_exception(self):
mock_session = MagicMock()
mock_session.head.side_effect = RequestException
Expand Down
Loading