1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Prevent caching for dynamic directory

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-11-24 07:38:28 -08:00
parent 521cf5338e
commit 754c84596e

29
echo.go
View File

@ -418,9 +418,10 @@ func (e *Echo) serveFile(dir, file string, c *Context) (err error) {
fi, _ := f.Stat()
if fi.IsDir() {
if checkLastModified(c.response, c.request, fi.ModTime()) {
return
}
/* NOTE:
Not checking the Last-Modified header as it caches the response `304` when
changing differnt directories for the same path.
*/
d := f
// Index file
@ -693,25 +694,3 @@ func (binder) Bind(r *http.Request, i interface{}) (err error) {
}
return
}
// Source: net/http/fs.go
func checkLastModified(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
if modtime.IsZero() || modtime.Equal(unixEpochTime) {
// If the file doesn't have a modtime (IsZero), or the modtime
// is obviously garbage (Unix time == 0), then ignore modtimes
// and don't process the If-Modified-Since header.
return false
}
// The Date-Modified header truncates sub-second precision, so
// use mtime < t+1s instead of mtime <= t to check for unmodified.
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
w.WriteHeader(http.StatusNotModified)
return true
}
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
return false
}