mirror of
https://github.com/labstack/echo.git
synced 2025-07-15 01:34:53 +02:00
Prevent caching for dynamic directory
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
29
echo.go
29
echo.go
@ -418,9 +418,10 @@ func (e *Echo) serveFile(dir, file string, c *Context) (err error) {
|
|||||||
|
|
||||||
fi, _ := f.Stat()
|
fi, _ := f.Stat()
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
if checkLastModified(c.response, c.request, fi.ModTime()) {
|
/* NOTE:
|
||||||
return
|
Not checking the Last-Modified header as it caches the response `304` when
|
||||||
}
|
changing differnt directories for the same path.
|
||||||
|
*/
|
||||||
d := f
|
d := f
|
||||||
|
|
||||||
// Index file
|
// Index file
|
||||||
@ -693,25 +694,3 @@ func (binder) Bind(r *http.Request, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
return
|
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
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user