mirror of
https://github.com/labstack/echo.git
synced 2025-06-04 23:37:45 +02:00
Proper header and MIME constants
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
adad28012c
commit
8b5772cf65
30
context.go
30
context.go
@ -289,21 +289,21 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
|
|||||||
if err = c.echo.renderer.Render(buf, name, data, c); err != nil {
|
if err = c.echo.renderer.Render(buf, name, data, c); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
_, err = c.response.Write(buf.Bytes())
|
_, err = c.response.Write(buf.Bytes())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) HTML(code int, html string) (err error) {
|
func (c *context) HTML(code int, html string) (err error) {
|
||||||
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
_, err = c.response.Write([]byte(html))
|
_, err = c.response.Write([]byte(html))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) String(code int, s string) (err error) {
|
func (c *context) String(code int, s string) (err error) {
|
||||||
c.response.Header().Set(ContentType, TextPlainCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
_, err = c.response.Write([]byte(s))
|
_, err = c.response.Write([]byte(s))
|
||||||
return
|
return
|
||||||
@ -321,7 +321,7 @@ func (c *context) JSON(code int, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
||||||
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
_, err = c.response.Write(b)
|
_, err = c.response.Write(b)
|
||||||
return
|
return
|
||||||
@ -332,7 +332,7 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
|
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
|
||||||
return
|
return
|
||||||
@ -356,7 +356,7 @@ func (c *context) XML(code int, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
||||||
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
||||||
return
|
return
|
||||||
@ -385,8 +385,8 @@ func (c *context) File(file string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
||||||
c.response.Header().Set(ContentType, ContentTypeByExtension(name))
|
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
||||||
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
|
||||||
c.response.WriteHeader(http.StatusOK)
|
c.response.WriteHeader(http.StatusOK)
|
||||||
_, err = io.Copy(c.response, r)
|
_, err = io.Copy(c.response, r)
|
||||||
return
|
return
|
||||||
@ -401,7 +401,7 @@ func (c *context) Redirect(code int, url string) error {
|
|||||||
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
||||||
return ErrInvalidRedirectCode
|
return ErrInvalidRedirectCode
|
||||||
}
|
}
|
||||||
c.response.Header().Set(Location, url)
|
c.response.Header().Set(HeaderLocation, url)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -426,14 +426,14 @@ func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.
|
|||||||
rq := c.Request()
|
rq := c.Request()
|
||||||
rs := c.Response()
|
rs := c.Response()
|
||||||
|
|
||||||
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(IfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
||||||
rs.Header().Del(ContentType)
|
rs.Header().Del(HeaderContentType)
|
||||||
rs.Header().Del(ContentLength)
|
rs.Header().Del(HeaderContentLength)
|
||||||
return c.NoContent(http.StatusNotModified)
|
return c.NoContent(http.StatusNotModified)
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.Header().Set(ContentType, ContentTypeByExtension(name))
|
rs.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
||||||
rs.Header().Set(LastModified, modtime.UTC().Format(http.TimeFormat))
|
rs.Header().Set(HeaderLastModified, modtime.UTC().Format(http.TimeFormat))
|
||||||
rs.WriteHeader(http.StatusOK)
|
rs.WriteHeader(http.StatusOK)
|
||||||
_, err := io.Copy(rs, content)
|
_, err := io.Copy(rs, content)
|
||||||
return err
|
return err
|
||||||
@ -444,7 +444,7 @@ func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.
|
|||||||
// found.
|
// found.
|
||||||
func ContentTypeByExtension(name string) (t string) {
|
func ContentTypeByExtension(name string) (t string) {
|
||||||
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
||||||
t = OctetStream
|
t = MIMEOctetStream
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -65,15 +65,15 @@ func TestContext(t *testing.T) {
|
|||||||
//------
|
//------
|
||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
testBindOk(t, c, ApplicationJSON)
|
testBindOk(t, c, MIMEApplicationJSON)
|
||||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
||||||
testBindError(t, c, ApplicationJSON)
|
testBindError(t, c, MIMEApplicationJSON)
|
||||||
|
|
||||||
// XML
|
// XML
|
||||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||||
testBindOk(t, c, ApplicationXML)
|
testBindOk(t, c, MIMEApplicationXML)
|
||||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
||||||
testBindError(t, c, ApplicationXML)
|
testBindError(t, c, MIMEApplicationXML)
|
||||||
|
|
||||||
// Unsupported
|
// Unsupported
|
||||||
testBindError(t, c, "")
|
testBindError(t, c, "")
|
||||||
@ -102,7 +102,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
|
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
assert.Equal(t, userJSON, rec.Body.String())
|
assert.Equal(t, userJSON, rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
|
err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, ApplicationJavaScriptCharsetUTF8, rec.Header().Get(ContentType))
|
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
|
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.XML(http.StatusOK, user{"1", "Joe"})
|
err = c.XML(http.StatusOK, user{"1", "Joe"})
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
|
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.String(http.StatusOK, "Hello, World!")
|
err = c.String(http.StatusOK, "Hello, World!")
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, TextPlainCharsetUTF8, rec.Header().Get(ContentType))
|
assert.Equal(t, MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
assert.Equal(t, "Hello, World!", rec.Body.String())
|
assert.Equal(t, "Hello, World!", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, TextHTMLCharsetUTF8, rec.Header().Get(ContentType))
|
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||||
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,7 +167,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.Attachment(file, "walle.png")
|
err = c.Attachment(file, "walle.png")
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(ContentDisposition))
|
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
|
||||||
assert.Equal(t, 219885, rec.Body.Len())
|
assert.Equal(t, 219885, rec.Body.Len())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -182,7 +182,7 @@ func TestContext(t *testing.T) {
|
|||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = NewContext(rq, rec, e)
|
c = NewContext(rq, rec, e)
|
||||||
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
||||||
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(Location))
|
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
||||||
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
||||||
|
|
||||||
// Error
|
// Error
|
||||||
@ -226,7 +226,7 @@ func TestContextFormValue(t *testing.T) {
|
|||||||
f.Set("email", "joe@labstack.com")
|
f.Set("email", "joe@labstack.com")
|
||||||
|
|
||||||
rq := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
rq := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
||||||
rq.Header().Add(ContentType, ApplicationForm)
|
rq.Header().Add(HeaderContentType, MIMEApplicationForm)
|
||||||
|
|
||||||
c := NewContext(rq, nil, New())
|
c := NewContext(rq, nil, New())
|
||||||
assert.Equal(t, "joe", c.FormValue("name"))
|
assert.Equal(t, "joe", c.FormValue("name"))
|
||||||
@ -258,7 +258,7 @@ func TestContextServeContent(t *testing.T) {
|
|||||||
// Cached
|
// Cached
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = NewContext(rq, rc, e)
|
c = NewContext(rq, rc, e)
|
||||||
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
rq.Header().Set(HeaderIfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
||||||
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
|
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())
|
||||||
}
|
}
|
||||||
@ -267,7 +267,7 @@ func TestContextServeContent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testBindOk(t *testing.T, c Context, ct string) {
|
func testBindOk(t *testing.T, c Context, ct string) {
|
||||||
c.Request().Header().Set(ContentType, ct)
|
c.Request().Header().Set(HeaderContentType, ct)
|
||||||
u := new(user)
|
u := new(user)
|
||||||
err := c.Bind(u)
|
err := c.Bind(u)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
@ -277,12 +277,12 @@ func testBindOk(t *testing.T, c Context, ct string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testBindError(t *testing.T, c Context, ct string) {
|
func testBindError(t *testing.T, c Context, ct string) {
|
||||||
c.Request().Header().Set(ContentType, ct)
|
c.Request().Header().Set(HeaderContentType, ct)
|
||||||
u := new(user)
|
u := new(user)
|
||||||
err := c.Bind(u)
|
err := c.Bind(u)
|
||||||
|
|
||||||
switch ct {
|
switch ct {
|
||||||
case ApplicationJSON, ApplicationXML:
|
case MIMEApplicationJSON, MIMEApplicationXML:
|
||||||
if assert.IsType(t, new(HTTPError), err) {
|
if assert.IsType(t, new(HTTPError), err) {
|
||||||
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code)
|
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code)
|
||||||
}
|
}
|
||||||
|
69
echo.go
69
echo.go
@ -127,46 +127,45 @@ const (
|
|||||||
TRACE = "TRACE"
|
TRACE = "TRACE"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Media types
|
// MIME types
|
||||||
const (
|
const (
|
||||||
ApplicationJSON = "application/json"
|
MIMEApplicationJSON = "application/json"
|
||||||
ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8
|
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
|
||||||
ApplicationJavaScript = "application/javascript"
|
MIMEApplicationJavaScript = "application/javascript"
|
||||||
ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8
|
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
|
||||||
ApplicationXML = "application/xml"
|
MIMEApplicationXML = "application/xml"
|
||||||
ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8
|
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
|
||||||
ApplicationForm = "application/x-www-form-urlencoded"
|
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
||||||
ApplicationProtobuf = "application/protobuf"
|
MIMEApplicationProtobuf = "application/protobuf"
|
||||||
ApplicationMsgpack = "application/msgpack"
|
MIMEApplicationMsgpack = "application/msgpack"
|
||||||
TextHTML = "text/html"
|
MIMETextHTML = "text/html"
|
||||||
TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8
|
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
|
||||||
TextPlain = "text/plain"
|
MIMETextPlain = "text/plain"
|
||||||
TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8
|
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
|
||||||
MultipartForm = "multipart/form-data"
|
MIMEMultipartForm = "multipart/form-data"
|
||||||
OctetStream = "application/octet-stream"
|
MIMEOctetStream = "application/octet-stream"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Charset
|
|
||||||
const (
|
const (
|
||||||
CharsetUTF8 = "charset=utf-8"
|
charsetUTF8 = "charset=utf-8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Headers
|
// Headers
|
||||||
const (
|
const (
|
||||||
AcceptEncoding = "Accept-Encoding"
|
HeaderAcceptEncoding = "Accept-Encoding"
|
||||||
Authorization = "Authorization"
|
HeaderAuthorization = "Authorization"
|
||||||
ContentDisposition = "Content-Disposition"
|
HeaderContentDisposition = "Content-Disposition"
|
||||||
ContentEncoding = "Content-Encoding"
|
HeaderContentEncoding = "Content-Encoding"
|
||||||
ContentLength = "Content-Length"
|
HeaderContentLength = "Content-Length"
|
||||||
ContentType = "Content-Type"
|
HeaderContentType = "Content-Type"
|
||||||
IfModifiedSince = "If-Modified-Since"
|
HeaderIfModifiedSince = "If-Modified-Since"
|
||||||
LastModified = "Last-Modified"
|
HeaderLastModified = "Last-Modified"
|
||||||
Location = "Location"
|
HeaderLocation = "Location"
|
||||||
Upgrade = "Upgrade"
|
HeaderUpgrade = "Upgrade"
|
||||||
Vary = "Vary"
|
HeaderVary = "Vary"
|
||||||
WWWAuthenticate = "WWW-Authenticate"
|
HeaderWWWAuthenticate = "WWW-Authenticate"
|
||||||
XForwardedFor = "X-Forwarded-For"
|
HeaderXForwardedFor = "X-Forwarded-For"
|
||||||
XRealIP = "X-Real-IP"
|
HeaderXRealIP = "X-Real-IP"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -514,13 +513,13 @@ func (e *HTTPError) Error() string {
|
|||||||
|
|
||||||
func (b *binder) Bind(i interface{}, c Context) (err error) {
|
func (b *binder) Bind(i interface{}, c Context) (err error) {
|
||||||
rq := c.Request()
|
rq := c.Request()
|
||||||
ct := rq.Header().Get(ContentType)
|
ct := rq.Header().Get(HeaderContentType)
|
||||||
err = ErrUnsupportedMediaType
|
err = ErrUnsupportedMediaType
|
||||||
if strings.HasPrefix(ct, ApplicationJSON) {
|
if strings.HasPrefix(ct, MIMEApplicationJSON) {
|
||||||
if err = json.NewDecoder(rq.Body()).Decode(i); err != nil {
|
if err = json.NewDecoder(rq.Body()).Decode(i); err != nil {
|
||||||
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
} else if strings.HasPrefix(ct, ApplicationXML) {
|
} else if strings.HasPrefix(ct, MIMEApplicationXML) {
|
||||||
if err = xml.NewDecoder(rq.Body()).Decode(i); err != nil {
|
if err = xml.NewDecoder(rq.Body()).Decode(i); err != nil {
|
||||||
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
|
|||||||
func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
auth := c.Request().Header().Get(echo.Authorization)
|
auth := c.Request().Header().Get(echo.HeaderAuthorization)
|
||||||
l := len(basic)
|
l := len(basic)
|
||||||
|
|
||||||
if len(auth) > l+1 && auth[:l] == basic {
|
if len(auth) > l+1 && auth[:l] == basic {
|
||||||
@ -58,7 +58,7 @@ func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
|
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm=Restricted")
|
||||||
return echo.ErrUnauthorized
|
return echo.ErrUnauthorized
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ func TestBasicAuth(t *testing.T) {
|
|||||||
|
|
||||||
// Valid credentials
|
// Valid credentials
|
||||||
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
||||||
rq.Header().Set(echo.Authorization, auth)
|
rq.Header().Set(echo.HeaderAuthorization, auth)
|
||||||
assert.NoError(t, h(c))
|
assert.NoError(t, h(c))
|
||||||
|
|
||||||
//---------------------
|
//---------------------
|
||||||
@ -36,21 +36,21 @@ func TestBasicAuth(t *testing.T) {
|
|||||||
|
|
||||||
// Incorrect password
|
// Incorrect password
|
||||||
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
|
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
|
||||||
rq.Header().Set(echo.Authorization, auth)
|
rq.Header().Set(echo.HeaderAuthorization, auth)
|
||||||
he := h(c).(*echo.HTTPError)
|
he := h(c).(*echo.HTTPError)
|
||||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
||||||
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.WWWAuthenticate))
|
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
|
||||||
|
|
||||||
// Empty Authorization header
|
// Empty Authorization header
|
||||||
rq.Header().Set(echo.Authorization, "")
|
rq.Header().Set(echo.HeaderAuthorization, "")
|
||||||
he = h(c).(*echo.HTTPError)
|
he = h(c).(*echo.HTTPError)
|
||||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
||||||
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.WWWAuthenticate))
|
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
|
||||||
|
|
||||||
// Invalid Authorization header
|
// Invalid Authorization header
|
||||||
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
|
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
|
||||||
rq.Header().Set(echo.Authorization, auth)
|
rq.Header().Set(echo.HeaderAuthorization, auth)
|
||||||
he = h(c).(*echo.HTTPError)
|
he = h(c).(*echo.HTTPError)
|
||||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
||||||
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.WWWAuthenticate))
|
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,8 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
|||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
rs := c.Response()
|
rs := c.Response()
|
||||||
rs.Header().Add(echo.Vary, echo.AcceptEncoding)
|
rs.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
|
||||||
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
if strings.Contains(c.Request().Header().Get(echo.HeaderAcceptEncoding), scheme) {
|
||||||
rw := rs.Writer()
|
rw := rs.Writer()
|
||||||
gw := pool.Get().(*gzip.Writer)
|
gw := pool.Get().(*gzip.Writer)
|
||||||
gw.Reset(rw)
|
gw.Reset(rw)
|
||||||
@ -64,14 +64,14 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
|||||||
// nothing is written to body or error is returned.
|
// nothing is written to body or error is returned.
|
||||||
// See issue #424, #407.
|
// See issue #424, #407.
|
||||||
rs.SetWriter(rw)
|
rs.SetWriter(rw)
|
||||||
rs.Header().Del(echo.ContentEncoding)
|
rs.Header().Del(echo.HeaderContentEncoding)
|
||||||
gw.Reset(ioutil.Discard)
|
gw.Reset(ioutil.Discard)
|
||||||
}
|
}
|
||||||
gw.Close()
|
gw.Close()
|
||||||
pool.Put(gw)
|
pool.Put(gw)
|
||||||
}()
|
}()
|
||||||
g := gzipResponseWriter{Response: rs, Writer: gw}
|
g := gzipResponseWriter{Response: rs, Writer: gw}
|
||||||
rs.Header().Set(echo.ContentEncoding, scheme)
|
rs.Header().Set(echo.HeaderContentEncoding, scheme)
|
||||||
rs.SetWriter(g)
|
rs.SetWriter(g)
|
||||||
}
|
}
|
||||||
return next(c)
|
return next(c)
|
||||||
@ -80,8 +80,8 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
if g.Header().Get(echo.ContentType) == "" {
|
if g.Header().Get(echo.HeaderContentType) == "" {
|
||||||
g.Header().Set(echo.ContentType, http.DetectContentType(b))
|
g.Header().Set(echo.HeaderContentType, http.DetectContentType(b))
|
||||||
}
|
}
|
||||||
return g.Writer.Write(b)
|
return g.Writer.Write(b)
|
||||||
}
|
}
|
||||||
|
@ -27,14 +27,14 @@ func TestGzip(t *testing.T) {
|
|||||||
assert.Equal(t, "test", rec.Body.String())
|
assert.Equal(t, "test", rec.Body.String())
|
||||||
|
|
||||||
rq = test.NewRequest(echo.GET, "/", nil)
|
rq = test.NewRequest(echo.GET, "/", nil)
|
||||||
rq.Header().Set(echo.AcceptEncoding, "gzip")
|
rq.Header().Set(echo.HeaderAcceptEncoding, "gzip")
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rec, e)
|
c = echo.NewContext(rq, rec, e)
|
||||||
|
|
||||||
// Gzip
|
// Gzip
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
|
assert.Equal(t, "gzip", rec.Header().Get(echo.HeaderContentEncoding))
|
||||||
assert.Contains(t, rec.Header().Get(echo.ContentType), echo.TextPlain)
|
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
||||||
r, err := gzip.NewReader(rec.Body)
|
r, err := gzip.NewReader(rec.Body)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
@ -54,8 +54,8 @@ func TestGzipNoContent(t *testing.T) {
|
|||||||
})
|
})
|
||||||
h(c)
|
h(c)
|
||||||
|
|
||||||
assert.Empty(t, rec.Header().Get(echo.ContentEncoding))
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
||||||
assert.Empty(t, rec.Header().Get(echo.ContentType))
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
|
||||||
b, err := ioutil.ReadAll(rec.Body)
|
b, err := ioutil.ReadAll(rec.Body)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, 0, len(b))
|
assert.Equal(t, 0, len(b))
|
||||||
@ -72,7 +72,7 @@ func TestGzipErrorReturned(t *testing.T) {
|
|||||||
rec := test.NewResponseRecorder()
|
rec := test.NewResponseRecorder()
|
||||||
e.ServeHTTP(rq, rec)
|
e.ServeHTTP(rq, rec)
|
||||||
|
|
||||||
assert.Empty(t, rec.Header().Get(echo.ContentEncoding))
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
||||||
b, err := ioutil.ReadAll(rec.Body)
|
b, err := ioutil.ReadAll(rec.Body)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, "error", string(b))
|
assert.Equal(t, "error", string(b))
|
||||||
|
@ -90,9 +90,9 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
|||||||
return w.Write([]byte(time.Now().Format(time.RFC3339)))
|
return w.Write([]byte(time.Now().Format(time.RFC3339)))
|
||||||
case "remote_ip":
|
case "remote_ip":
|
||||||
ra := rq.RemoteAddress()
|
ra := rq.RemoteAddress()
|
||||||
if ip := rq.Header().Get(echo.XRealIP); ip != "" {
|
if ip := rq.Header().Get(echo.HeaderXRealIP); ip != "" {
|
||||||
ra = ip
|
ra = ip
|
||||||
} else if ip = rq.Header().Get(echo.XForwardedFor); ip != "" {
|
} else if ip = rq.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
||||||
ra = ip
|
ra = ip
|
||||||
} else {
|
} else {
|
||||||
ra, _, _ = net.SplitHostPort(ra)
|
ra, _, _ = net.SplitHostPort(ra)
|
||||||
|
@ -63,14 +63,14 @@ func TestLoggerIPAddress(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// With X-Real-IP
|
// With X-Real-IP
|
||||||
rq.Header().Add(echo.XRealIP, ip)
|
rq.Header().Add(echo.HeaderXRealIP, ip)
|
||||||
h(c)
|
h(c)
|
||||||
assert.Contains(t, ip, buf.String())
|
assert.Contains(t, ip, buf.String())
|
||||||
|
|
||||||
// With X-Forwarded-For
|
// With X-Forwarded-For
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
rq.Header().Del(echo.XRealIP)
|
rq.Header().Del(echo.HeaderXRealIP)
|
||||||
rq.Header().Add(echo.XForwardedFor, ip)
|
rq.Header().Add(echo.HeaderXForwardedFor, ip)
|
||||||
h(c)
|
h(c)
|
||||||
assert.Contains(t, ip, buf.String())
|
assert.Contains(t, ip, buf.String())
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
|
|||||||
|
|
||||||
// Create a directory index
|
// Create a directory index
|
||||||
rs := c.Response()
|
rs := c.Response()
|
||||||
rs.Header().Set(echo.ContentType, echo.TextHTMLCharsetUTF8)
|
rs.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
|
||||||
if _, err = fmt.Fprintf(rs, "<pre>\n"); err != nil {
|
if _, err = fmt.Fprintf(rs, "<pre>\n"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user