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

Commit 83360cb

Browse files
committed
http cached headers
1 parent b982278 commit 83360cb

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

server/server_http.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"html/template"
66
"net/http"
77
"strings"
8+
"time"
89

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

51+
uptime := time.Unix(s.baseInfo.Uptime, 0)
52+
cacheSince := uptime.Format(http.TimeFormat)
53+
cacheExpire := uptime.AddDate(0, 1, 0).Format(http.TimeFormat) // 1 month expire date
54+
5055
pathDir := strings.SplitN(r.URL.Path[1:], "/", 2)
5156
switch pathDir[0] {
5257
case "search":
@@ -62,10 +67,14 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
6267
case "download":
6368
s.dlfilesh.ServeHTTP(w, r)
6469
case s.baseInfo.Version:
70+
w.Header().Set("Last-Modified", cacheSince)
71+
w.Header().Set("Expires", cacheExpire)
6572
w.Header().Set("Cache-Control", "max-age:290304000, public")
6673
s.verStatich.ServeHTTP(w, r)
6774
default:
6875
//no match, assume static file
76+
w.Header().Set("Last-Modified", cacheSince)
77+
w.Header().Set("Expires", cacheExpire)
6978
w.Header().Set("Cache-Control", "max-age:290304000, public")
7079
s.statich.ServeHTTP(w, r)
7180
}

0 commit comments

Comments
 (0)