From d66abd9824de4aba63218c56289e7bfe55af039f Mon Sep 17 00:00:00 2001 From: Francesco Canovai Date: Fri, 3 Jan 2025 23:45:34 +0100 Subject: [PATCH] fix: avoid IncompleteRead on completed partial reads (#1827) * fix: avoid IncompleteRead on completed partial reads Fix a bug in which the server would return a 416 reply with an empty body declaring a Content-Length of 1. Closes #1761 Signed-off-by: Francesco Canovai * fakestorage: add a test for invalid range --------- Signed-off-by: Francesco Canovai Co-authored-by: francisco souza <108725+fsouza@users.noreply.github.com> --- fakestorage/object.go | 2 +- fakestorage/object_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/fakestorage/object.go b/fakestorage/object.go index bc23a61665..d5792bec69 100644 --- a/fakestorage/object.go +++ b/fakestorage/object.go @@ -996,7 +996,7 @@ func (s *Server) handleRange(obj StreamingObject, r *http.Request) (ranged bool, // Length: 40, Range: bytes=50- case start >= obj.Size: // This IS a ranged request, but it ISN'T satisfiable. - return true, 0, 0, false + return true, 0, -1, false // Negative range, ignore range and return all content. // Examples: // Length: 40, Range: bytes=30-20 diff --git a/fakestorage/object_test.go b/fakestorage/object_test.go index 6987cfdf7d..a7f144425f 100644 --- a/fakestorage/object_test.go +++ b/fakestorage/object_test.go @@ -610,6 +610,34 @@ func TestServerClientObjectRangeReader(t *testing.T) { }) } +func TestServerClientObjectRangeReaderInvalid(t *testing.T) { + const ( + bucketName = "some-bucket" + objectName = "items/data.txt" + content = "some really nice but long content stored in my object" + contentType = "text/plain; charset=iso-8859" + ) + objs := []Object{ + { + ObjectAttrs: ObjectAttrs{ + BucketName: bucketName, + Name: objectName, + ContentType: contentType, + }, + Content: []byte(content), + }, + } + + runServersTest(t, runServersOptions{objs: objs}, func(t *testing.T, server *Server) { + client := server.Client() + objHandle := client.Bucket(bucketName).Object(objectName) + _, err := objHandle.NewRangeReader(context.TODO(), 500, 10) + if err == nil { + t.Fatal("unexpected error") + } + }) +} + func TestServerClientObjectReaderAfterCreateObject(t *testing.T) { const ( bucketName = "staging-bucket"