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:
19
context.go
19
context.go
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,23 +245,23 @@ 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()
|
||||||
assert.Equal(t, http.StatusOK, rc.Status())
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
// Cached
|
// Cached
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = NewContext(rq, rc, e)
|
c = NewContext(rq, rc, e)
|
||||||
fi, err := f.Stat()
|
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
|
||||||
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
assert.Equal(t, http.StatusNotModified, rc.Status())
|
||||||
if assert.NoError(t, c.ServeContent(f)) {
|
}
|
||||||
assert.Equal(t, http.StatusNotModified, rc.Status())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user