Skip to content

Commit

Permalink
Add caching headers on 404 responses (#775)
Browse files Browse the repository at this point in the history
* Add caching headers on 404 responses
  • Loading branch information
willscott committed Nov 24, 2020
1 parent 22a4797 commit d6a09ba
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ func (h *handler) PublicRand(w http.ResponseWriter, r *http.Request) {
roundExpectedTime = time.Unix(chain.TimeOfRound(info.Period, info.GenesisTime, roundN), 0)

if roundExpectedTime.After(time.Now().Add(info.Period)) {
timeToExpected := int(time.Until(roundExpectedTime).Seconds())
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Cache-Control", fmt.Sprintf("public, must-revalidate, max-age=%d", timeToExpected))
h.log.Warn("http_server", "request in the future", "client", r.RemoteAddr, "req", url.PathEscape(r.URL.Path))
return
}
Expand All @@ -267,6 +269,7 @@ func (h *handler) PublicRand(w http.ResponseWriter, r *http.Request) {
}
if data == nil {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Cache-Control", "must-revalidate, no-cache, max-age=0")
h.log.Warn("http_server", "request in the future", "client", r.RemoteAddr, "req", url.PathEscape(r.URL.Path))
return
}
Expand Down

0 comments on commit d6a09ba

Please sign in to comment.