Handle unseekable data

We may be given a file-like
that cannot seek.
For example, older 'ZipFileExt'
did not have tell/seek.

For such cases, fallback to memory
hungary behavior of using a BytesIO to
provide a trackable
upload.

Change-Id: I401e2fcc7369f31d0d413909854c8d9224b02023
This commit is contained in:
Jarrod Johnson 2023-07-27 10:02:00 -04:00
parent 85f5c7699b
commit 7b62d693c7
1 changed files with 13 additions and 5 deletions

View File

@ -317,11 +317,19 @@ class SecureHTTPConnection(httplib.HTTPConnection, object):
ulhdrs['Content-Length'] = len(uploadforms[filename]) ulhdrs['Content-Length'] = len(uploadforms[filename])
self.ulsize = len(uploadforms[filename]) self.ulsize = len(uploadforms[filename])
else: else:
curroff = data.tell() canseek = True
data.seek(0, 2) try:
self.ulsize = data.tell() curroff = data.tell()
data.seek(curroff, 0) except io.UnsupportedOperation:
self._upbuffer = data canseek = False
databytes = data.read()
self.ulsize = len(databytes)
self._upbuffer = io.BytesIO(databytes)
if canseek:
data.seek(0, 2)
self.ulsize = data.tell()
data.seek(curroff, 0)
self._upbuffer = data
ulhdrs['Content-Type'] = b'application/octet-stream' ulhdrs['Content-Type'] = b'application/octet-stream'
ulhdrs['Content-Length'] = self.ulsize ulhdrs['Content-Length'] = self.ulsize
webclient = self.dupe() webclient = self.dupe()