From 31e19678665be5d0c8473734e5004251060e0199 Mon Sep 17 00:00:00 2001 From: John L Chen Date: Mon, 16 May 2022 21:20:13 +0800 Subject: [PATCH] fix: Empty last chunk case in resumable upload that is not a stream For an upload that inherits the MediaUpload class and is not a stream, if the size of the upload is exact multiples of the chunksize, the last chunk would be empty and the generated Content-Range header will result in "Bad Request". Details: "Failed to parse Content-Range header." --- googleapiclient/http.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/googleapiclient/http.py b/googleapiclient/http.py index 187f6f5dac8..3c0969ea1bf 100644 --- a/googleapiclient/http.py +++ b/googleapiclient/http.py @@ -1062,10 +1062,14 @@ def next_chunk(self, http=None, num_retries=0): "Content-Length": str(chunk_end - self.resumable_progress + 1), } - # An empty file results in chunk_end = -1 and size = 0 - # sending "bytes 0--1/0" results in an invalid request - # Only add header "Content-Range" if chunk_end != -1 + if chunk_end - self.resumable_progress + 1 == 0 and chunk_end != -1: + # This is the last chunk, it's empty but the file is not empty. + # Send a Content-Range that ends this file. + headers["Content-Range"] = "bytes */%s" % (size,) if chunk_end != -1: + # An empty file results in chunk_end = -1 and size = 0 + # sending "bytes 0--1/0" results in an invalid request + # Only add header "Content-Range" if chunk_end != -1 headers["Content-Range"] = "bytes %d-%d/%s" % ( self.resumable_progress, chunk_end,