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

Avoid uploading empty files as chunked transfers #15036

Closed
Closed
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
16 changes: 15 additions & 1 deletion conans/client/rest/file_uploader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time
import os
import contextlib
from copy import copy

from conan.api.output import ConanOutput
Expand Down Expand Up @@ -80,8 +82,20 @@ def upload(self, url, abs_path, auth=None, dedup=False, retry=None, retry_wait=N
self._output.info("Waiting %d seconds to retry..." % retry_wait)
time.sleep(retry_wait)

def _get_file_handler_or_empty_buffer(self, abs_path):
"""Return a context manager for abs_path or an empty buffer.
If abs_path points to a non empty file, open abs_path and return the file handler.
If abs_path points to an empty file, return a context handler for an empty
string.
This avoids an issue with several web servers that cannot handle
"Transfer-Encoding: chunked" for empty files."""
if os.stat(abs_path).st_size == 0:
return contextlib.nullcontext("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this is breaking our, check CI: https://ci.conan.io/blue/organizations/jenkins/ConanTestSuitev2/detail/PR-15036/1/pipeline/41. Please note that things must work with Python 3.6 too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fix that, if this is a good way to fix the issue. I see @SSE4 is asking some good questions.

else:
return open(abs_path, mode='rb')

def _upload_file(self, url, abs_path, headers, auth):
with open(abs_path, mode='rb') as file_handler:
with self._get_file_handler_or_empty_buffer(abs_path) as file_handler:
try:
response = self._requester.put(url, data=file_handler, verify=self._verify_ssl,
headers=headers, auth=auth)
Expand Down