Skip to content

Commit

Permalink
feat: set booster-http cache control headers (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored Aug 24, 2022
1 parent 50bc2e6 commit bb88432
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (

var ErrNotFound = errors.New("not found")

// For data served by the endpoints in the HTTP server that never changes
// (eg pieces identified by a piece CID) send a cache header with a constant,
// non-zero last modified time.
var lastModified = time.UnixMilli(1)

const carSuffix = ".car"

type HttpServer struct {
Expand Down Expand Up @@ -196,6 +201,13 @@ func (s *HttpServer) handleByPayloadCid(w http.ResponseWriter, r *http.Request)
return
}

// Set an Etag based on the piece cid
etag := pieceCid.String()
if isCar {
etag += carSuffix
}
w.Header().Set("Etag", etag)

serveContent(w, r, content, getContentType(isCar))
}

Expand Down Expand Up @@ -237,6 +249,13 @@ func (s *HttpServer) handleByPieceCid(w http.ResponseWriter, r *http.Request) {
return
}

// Set an Etag based on the piece cid
etag := pieceCid.String()
if isCar {
etag += carSuffix
}
w.Header().Set("Etag", etag)

serveContent(w, r, content, getContentType(isCar))
}

Expand Down Expand Up @@ -268,9 +287,11 @@ func serveContent(w http.ResponseWriter, r *http.Request, content io.ReadSeeker,
}}

// Send the content
// Note that the last modified time is a constant value because the data
// in a piece identified by a cid will never change.
start := time.Now()
alogAt(start, "%s\tGET %s", color.New(color.FgGreen).Sprintf("%d", http.StatusOK), r.URL)
http.ServeContent(writeErrWatcher, r, "", time.Time{}, content)
http.ServeContent(writeErrWatcher, r, "", lastModified, content)

// Check if there was an error during the transfer
end := time.Now()
Expand Down

0 comments on commit bb88432

Please sign in to comment.