1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-13 01:30:31 +02:00

Context#StaticContent signature changed

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2016-03-27 21:03:35 -07:00
parent a2d757eddc
commit 28ec39daaa
3 changed files with 23 additions and 25 deletions

View File

@ -136,7 +136,7 @@ type (
// ServeContent sends static content from `io.Reader` and handles caching // ServeContent sends static content from `io.Reader` and handles caching
// via `If-Modified-Since` request header. It automatically sets `Content-Type` // via `If-Modified-Since` request header. It automatically sets `Content-Type`
// and `Last-Modified` response headers. // and `Last-Modified` response headers.
ServeContent(http.File) error ServeContent(io.Reader, string, time.Time) error
// Object returns the `context` instance. // Object returns the `context` instance.
Object() *context Object() *context
@ -380,8 +380,9 @@ func (c *context) File(file string) error {
if err != nil { if err != nil {
return ErrNotFound return ErrNotFound
} }
fi, _ = f.Stat()
} }
return c.ServeContent(f) return c.ServeContent(f, fi.Name(), fi.ModTime())
} }
func (c *context) Attachment(r io.Reader, name string) (err error) { func (c *context) Attachment(r io.Reader, name string) (err error) {
@ -422,24 +423,20 @@ func (c *context) Object() *context {
return c return c
} }
func (c *context) ServeContent(f http.File) error { func (c *context) ServeContent(r io.Reader, name string, modtime time.Time) error {
rq := c.Request() rq := c.Request()
rs := c.Response() rs := c.Response()
fi, err := f.Stat()
if err != nil {
return err
}
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(IfModifiedSince)); err == nil && fi.ModTime().Before(t.Add(1*time.Second)) { if t, err := time.Parse(http.TimeFormat, rq.Header().Get(IfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
rs.Header().Del(ContentType) rs.Header().Del(ContentType)
rs.Header().Del(ContentLength) rs.Header().Del(ContentLength)
return c.NoContent(http.StatusNotModified) return c.NoContent(http.StatusNotModified)
} }
rs.Header().Set(ContentType, detectContentType(fi.Name())) rs.Header().Set(ContentType, detectContentType(name))
rs.Header().Set(LastModified, fi.ModTime().UTC().Format(http.TimeFormat)) rs.Header().Set(LastModified, modtime.UTC().Format(http.TimeFormat))
rs.WriteHeader(http.StatusOK) rs.WriteHeader(http.StatusOK)
_, err = io.Copy(rs, f) _, err := io.Copy(rs, r)
return err return err
} }

View File

@ -245,25 +245,25 @@ func TestContextServeContent(t *testing.T) {
rc := test.NewResponseRecorder() rc := test.NewResponseRecorder()
c := NewContext(rq, rc, e) c := NewContext(rq, rc, e)
// Not cached
fs := http.Dir("_fixture/images") fs := http.Dir("_fixture/images")
f, err := fs.Open("walle.png") f, err := fs.Open("walle.png")
if assert.NoError(t, err) { if assert.NoError(t, err) {
if assert.NoError(t, c.ServeContent(f)) { fi, err := f.Stat()
if assert.NoError(t, err) {
// Not cached
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusOK, rc.Status()) assert.Equal(t, http.StatusOK, rc.Status())
} }
}
// Cached // Cached
rc = test.NewResponseRecorder() rc = test.NewResponseRecorder()
c = NewContext(rq, rc, e) c = NewContext(rq, rc, e)
fi, err := f.Stat()
if assert.NoError(t, err) {
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat)) rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
if assert.NoError(t, c.ServeContent(f)) { if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
assert.Equal(t, http.StatusNotModified, rc.Status()) assert.Equal(t, http.StatusNotModified, rc.Status())
} }
} }
}
} }
func testBindOk(t *testing.T, c Context, ct string) { func testBindOk(t *testing.T, c Context, ct string) {

View File

@ -97,8 +97,9 @@ func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
} }
return next.Handle(c) return next.Handle(c)
} }
fi, _ = f.Stat() // Index file stat
} }
return c.ServeContent(f) return c.ServeContent(f, fi.Name(), fi.ModTime())
}) })
} }
} }