Skip to content

Commit

Permalink
storage: don't set the trivial "0-" Range header.
Browse files Browse the repository at this point in the history
As per the RFC, the server has to response with 416 (Requested range not
satisfiable) if there is not at least one matching byte. Specifying a
range for the trivial case therefore prevents us reading zero-length
objects.

Fixes #242.

Change-Id: Ic054411e03cf7e0a2c91b1de0879653e48bb451d
Reviewed-on: https://code-review.googlesource.com/4562
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
okdave committed Apr 20, 2016
1 parent 8e17245 commit 17acd8a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions storage/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,35 @@ func TestWriterContentType(t *testing.T) {
}
}

func TestZeroSizedObject(t *testing.T) {
ctx := context.Background()
client, bucket := testConfig(ctx, t)
defer client.Close()

obj := client.Bucket(bucket).Object("zero" + suffix)

// Check writing it works as expected.
w := obj.NewWriter(ctx)
if err := w.Close(); err != nil {
t.Fatalf("Writer.Close: %v", err)
}
defer obj.Delete(ctx)

// Check we can read it too.
r, err := obj.NewReader(ctx)
if err != nil {
t.Fatalf("NewReader: %v", err)
}
defer r.Close()
body, err := ioutil.ReadAll(r)
if err != nil {
t.Fatalf("ioutil.ReadAll: %v", err)
}
if len(body) != 0 {
t.Errorf("Body is %v, want empty []byte{}", body)
}
}

// cleanup deletes any objects in the default bucket which were created
// during this test run (those with the designated suffix), and any
// objects whose suffix indicates they were created over an hour ago.
Expand Down
2 changes: 1 addition & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func (o *ObjectHandle) NewRangeReader(ctx context.Context, offset, length int64)
if err := applyConds("NewReader", o.conds, objectsGetCall{req}); err != nil {
return nil, err
}
if length < 0 {
if length < 0 && offset > 0 {
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", offset))
} else if length > 0 {
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
Expand Down

0 comments on commit 17acd8a

Please sign in to comment.