Skip to content

Commit

Permalink
handler.cache: rewrite some time/date headers - fixes #119
Browse files Browse the repository at this point in the history
the rewritten headers when serving from the cache:
Date, Age, Cache-Control (with max-age), Expires

Now non of this would be cached as well as they are always recalculated.
  • Loading branch information
mstoykov committed Oct 27, 2015
1 parent 87391e2 commit d6bd5ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 11 additions & 2 deletions handler/cache/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"os"
"strconv"
"time"

"golang.org/x/net/context"

Expand Down Expand Up @@ -61,8 +62,6 @@ func (h *reqHandler) handle() {
h.obj = obj
//!TODO: advertise that we support ranges - send "Accept-Ranges: bytes"?

//!TODO: rewrite all date and duration headers? date, max-age, expires, etc.

//!TODO: evaluate conditional requests: https://tools.ietf.org/html/rfc7232
//!TODO: Also, handle this from RFC7233:
// "The Range header field is evaluated after evaluating the precondition
Expand Down Expand Up @@ -119,6 +118,7 @@ func (h *reqHandler) knownRanged() {
httputils.CopyHeaders(h.obj.Headers, h.resp.Header())
h.resp.Header().Set("Content-Range", reqRange.ContentRange(h.obj.Size))
h.resp.Header().Set("Content-Length", strconv.FormatUint(reqRange.Length, 10))
h.rewriteTimeBasedHeaders()
h.resp.WriteHeader(http.StatusPartialContent)
if h.req.Method == "HEAD" {
return
Expand All @@ -130,10 +130,19 @@ func (h *reqHandler) knownRanged() {
func (h *reqHandler) knownFull() {
httputils.CopyHeaders(h.obj.Headers, h.resp.Header())
h.resp.Header().Set("Content-Length", strconv.FormatUint(h.obj.Size, 10))
h.rewriteTimeBasedHeaders()
h.resp.WriteHeader(h.obj.Code)
if h.req.Method == "HEAD" {
return
}

h.lazilyRespond(0, h.obj.Size-1)
}

func (h *reqHandler) rewriteTimeBasedHeaders() {
var now = time.Now()
h.resp.Header().Set("Date", now.Format(http.TimeFormat))
h.resp.Header().Set("Expires", time.Unix(h.obj.ExpiresAt, 0).Format(http.TimeFormat))
h.resp.Header().Set("Age", strconv.FormatInt(now.Unix()-h.obj.ResponseTimestamp, 10))
h.resp.Header().Set("Cache-Control", "max-age="+strconv.FormatInt(h.obj.ExpiresAt-now.Unix(), 10))
}
3 changes: 1 addition & 2 deletions handler/cache/handler_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
// Hop-by-hop headers. These are removed when sent to the client.
var hopHeaders = httputils.GetHopByHopHeaders()

//!TODO: add Date and cache-expity headers here? we probably have to manage them on our own
var metadataHeadersToFilter = append(hopHeaders, "Content-Length", "Content-Range")
var metadataHeadersToFilter = append(hopHeaders, "Content-Length", "Content-Range", "Date", "Expires", "Age", "Cache-Control")

// Returns a new HTTP 1.1 request that has no body. It also clears headers like
// accept-encoding and rearranges the requested ranges so they match part
Expand Down

0 comments on commit d6bd5ad

Please sign in to comment.