Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
http cached headers
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Oct 8, 2021
1 parent b982278 commit 83360cb
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"net/http"
"strings"
"time"

ctstatic "github.com/boypt/simple-torrent/static"
"github.com/jpillora/velox"
Expand Down Expand Up @@ -47,6 +48,10 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
return
}

uptime := time.Unix(s.baseInfo.Uptime, 0)
cacheSince := uptime.Format(http.TimeFormat)
cacheExpire := uptime.AddDate(0, 1, 0).Format(http.TimeFormat) // 1 month expire date

pathDir := strings.SplitN(r.URL.Path[1:], "/", 2)
switch pathDir[0] {
case "search":
Expand All @@ -62,10 +67,14 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
case "download":
s.dlfilesh.ServeHTTP(w, r)
case s.baseInfo.Version:
w.Header().Set("Last-Modified", cacheSince)
w.Header().Set("Expires", cacheExpire)
w.Header().Set("Cache-Control", "max-age:290304000, public")
s.verStatich.ServeHTTP(w, r)
default:
//no match, assume static file
w.Header().Set("Last-Modified", cacheSince)
w.Header().Set("Expires", cacheExpire)
w.Header().Set("Cache-Control", "max-age:290304000, public")
s.statich.ServeHTTP(w, r)
}
Expand Down

0 comments on commit 83360cb

Please sign in to comment.