1
0
mirror of https://github.com/labstack/echo.git synced 2025-02-03 13:11:39 +02:00

Last-Modified header for static content

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-12 10:18:40 -08:00
parent a7697fe480
commit 5b5df5bf44
3 changed files with 8 additions and 6 deletions

View File

@ -295,8 +295,8 @@ func (c *context) Attachment(file string) (err error) {
return
}
_, name := filepath.Split(file)
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
c.response.Header().Set(ContentType, detectContentType(file))
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
c.response.WriteHeader(http.StatusOK)
_, err = io.Copy(c.response, f)
return
@ -339,8 +339,8 @@ func (c *context) Object() *context {
}
func ServeContent(req engine.Request, res engine.Response, f http.File, fi os.FileInfo) error {
// TODO: http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
res.Header().Set(ContentType, detectContentType(fi.Name()))
res.Header().Set(LastModified, fi.ModTime().UTC().Format(http.TimeFormat))
res.WriteHeader(http.StatusOK)
_, err := io.Copy(res, f)
return err

View File

@ -136,6 +136,7 @@ const (
ContentEncoding = "Content-Encoding"
ContentLength = "Content-Length"
ContentType = "Content-Type"
LastModified = "Last-Modified"
Location = "Location"
Upgrade = "Upgrade"
Vary = "Vary"
@ -163,6 +164,8 @@ var (
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
ErrNotFound = NewHTTPError(http.StatusNotFound)
ErrUnauthorized = NewHTTPError(http.StatusUnauthorized)
ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed)
ErrRendererNotRegistered = errors.New("renderer not registered")
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
@ -171,11 +174,11 @@ var (
//----------------
notFoundHandler = HandlerFunc(func(c Context) error {
return NewHTTPError(http.StatusNotFound)
return ErrNotFound
})
methodNotAllowedHandler = HandlerFunc(func(c Context) error {
return NewHTTPError(http.StatusMethodNotAllowed)
return ErrMethodNotAllowed
})
)

View File

@ -2,7 +2,6 @@ package middleware
import (
"encoding/base64"
"net/http"
"github.com/labstack/echo"
)
@ -43,7 +42,7 @@ func BasicAuth(fn BasicAuthFunc, options ...BasicAuthOptions) echo.MiddlewareFun
}
}
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
return echo.NewHTTPError(http.StatusUnauthorized)
return echo.ErrUnauthorized
})
}
}