cmd/syncthing: Correctly set, parse and compare modified time HTTP headers (fixes #3165)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3167
This commit is contained in:
Jakob Borg 2016-05-23 12:16:14 +00:00 committed by Antony Male
parent 37816e3818
commit 98734375f2

View File

@ -298,7 +298,7 @@ func (s *apiService) Serve() {
// Serve compiled in assets unless an asset directory was set (for development) // Serve compiled in assets unless an asset directory was set (for development)
assets := &embeddedStatic{ assets := &embeddedStatic{
theme: s.cfg.GUI().Theme, theme: s.cfg.GUI().Theme,
lastModified: time.Now(), lastModified: time.Now().Truncate(time.Second), // must truncate, for the wire precision is 1s
mut: sync.NewRWMutex(), mut: sync.NewRWMutex(),
assetDir: s.assetDir, assetDir: s.assetDir,
assets: auto.Assets(), assets: auto.Assets(),
@ -1235,7 +1235,8 @@ func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
if modifiedSince, err := time.Parse(r.Header.Get("If-Modified-Since"), http.TimeFormat); err == nil && modified.Before(modifiedSince) { modifiedSince, err := http.ParseTime(r.Header.Get("If-Modified-Since"))
if err == nil && !modified.After(modifiedSince) {
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)
return return
} }
@ -1254,7 +1255,7 @@ func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gr.Close() gr.Close()
} }
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs))) w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
w.Header().Set("Last-Modified", modified.Format(http.TimeFormat)) w.Header().Set("Last-Modified", modified.UTC().Format(http.TimeFormat))
w.Header().Set("Cache-Control", "public") w.Header().Set("Cache-Control", "public")
w.Write(bs) w.Write(bs)