Skip to content

Commit

Permalink
storage: Add Reader.CacheControl()
Browse files Browse the repository at this point in the history
In order to proxy Google Cloud Storage efficiently, it's necessary
to have access to the Cache-Control header without making a separate
request.

Fixes #714

Change-Id: If1757c76909c78811fbe5333093c353120bb4ae9
Reviewed-on: https://code-review.googlesource.com/15330
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
  • Loading branch information
ConradIrwin authored and jba committed Aug 3, 2017
1 parent aedca7a commit 4451689
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions storage/integration_test.go
Expand Up @@ -364,6 +364,9 @@ func TestObjects(t *testing.T) {
if got, want := rc.ContentType(), "text/plain"; got != want {
t.Errorf("ContentType (%q) = %q; want %q", obj, got, want)
}
if got, want := rc.CacheControl(), "public, max-age=60"; got != want {
t.Errorf("CacheControl (%q) = %q; want %q", obj, got, want)
}
rc.Close()

// Check early close.
Expand Down Expand Up @@ -1448,6 +1451,7 @@ func TestIntegration_RequesterPays(t *testing.T) {
func writeObject(ctx context.Context, obj *ObjectHandle, contentType string, contents []byte) error {
w := obj.NewWriter(ctx)
w.ContentType = contentType
w.CacheControl = "public, max-age=60"
if contents != nil {
if _, err := w.Write(contents); err != nil {
_ = w.Close()
Expand Down
6 changes: 6 additions & 0 deletions storage/reader.go
Expand Up @@ -28,6 +28,7 @@ type Reader struct {
body io.ReadCloser
remain, size int64
contentType string
cacheControl string
checkCRC bool // should we check the CRC?
wantCRC uint32 // the CRC32c value the server sent in the header
gotCRC uint32 // running crc
Expand Down Expand Up @@ -72,3 +73,8 @@ func (r *Reader) Remain() int64 {
func (r *Reader) ContentType() string {
return r.contentType
}

// CacheControl returns the cache control of the object.
func (r *Reader) CacheControl() string {
return r.cacheControl
}
13 changes: 7 additions & 6 deletions storage/storage.go
Expand Up @@ -567,12 +567,13 @@ func (o *ObjectHandle) NewRangeReader(ctx context.Context, offset, length int64)
crc, checkCRC = parseCRC32c(res)
}
return &Reader{
body: body,
size: size,
remain: remain,
contentType: res.Header.Get("Content-Type"),
wantCRC: crc,
checkCRC: checkCRC,
body: body,
size: size,
remain: remain,
contentType: res.Header.Get("Content-Type"),
cacheControl: res.Header.Get("Cache-Control"),
wantCRC: crc,
checkCRC: checkCRC,
}, nil
}

Expand Down

0 comments on commit 4451689

Please sign in to comment.