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

DM-38599: Fix contents length checking in server-encoded HTTP responses #51

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions python/lsst/resources/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,30 +992,32 @@
f"Unable to download resource {self}; status: {resp.status_code} {resp.reason}"
)

content_length = 0
expected_length = int(resp.headers.get("Content-Length", "-1"))
tmpdir, buffering = _get_temp_dir()

with tempfile.NamedTemporaryFile(
suffix=self.getExtension(), buffering=buffering, dir=tmpdir, delete=False
) as tmpFile:
expected_length = int(resp.headers.get("Content-Length", "-1"))
with time_this(
log,
msg="GET %s [length=%d] to local file %s [chunk_size=%d]",
args=(self, expected_length, tmpFile.name, buffering),
mem_usage=self._config.collect_memory_usage,
mem_unit=u.mebibyte,
):
content_length = 0
for chunk in resp.iter_content(chunk_size=buffering):
tmpFile.write(chunk)
content_length += len(chunk)

# Check that the expected and actual content lengths match
if expected_length >= 0 and expected_length != content_length:
raise ValueError(
f"Size of downloaded file does not match value in Content-Length header for {self}: "
f"expecting {expected_length} and got {content_length} bytes"
)
# Check that the expected and actual content lengths match. Perform
# this check only when the contents of the file was not encoded by
# the server.
if "Content-Encoding" not in resp.headers:
if expected_length >= 0 and expected_length != content_length:
raise ValueError(

Check warning on line 1017 in python/lsst/resources/http.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/resources/http.py#L1017

Added line #L1017 was not covered by tests
f"Size of downloaded file does not match value in Content-Length header for {self}: "
f"expecting {expected_length} and got {content_length} bytes"
)

return tmpFile.name, True

Expand Down Expand Up @@ -1519,7 +1521,7 @@
return responses
else:
# Could not parse the body
raise ValueError(f"Unable to parse response for PROPFIND request: {response}")
raise ValueError(f"Unable to parse response for PROPFIND request: {body}")

Check warning on line 1524 in python/lsst/resources/http.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/resources/http.py#L1524

Added line #L1524 was not covered by tests


class DavProperty:
Expand Down