mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
parent
576dfeb71d
commit
b47abdb9ec
12
echo.go
12
echo.go
@ -131,9 +131,7 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
//--------------
|
|
||||||
// HTTP methods
|
// HTTP methods
|
||||||
//--------------
|
|
||||||
const (
|
const (
|
||||||
CONNECT = "CONNECT"
|
CONNECT = "CONNECT"
|
||||||
DELETE = "DELETE"
|
DELETE = "DELETE"
|
||||||
@ -146,9 +144,7 @@ const (
|
|||||||
TRACE = "TRACE"
|
TRACE = "TRACE"
|
||||||
)
|
)
|
||||||
|
|
||||||
//-------------
|
|
||||||
// Media types
|
// Media types
|
||||||
//-------------
|
|
||||||
const (
|
const (
|
||||||
ApplicationJSON = "application/json"
|
ApplicationJSON = "application/json"
|
||||||
ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8
|
ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8
|
||||||
@ -167,16 +163,12 @@ const (
|
|||||||
OctetStream = "application/octet-stream"
|
OctetStream = "application/octet-stream"
|
||||||
)
|
)
|
||||||
|
|
||||||
//---------
|
|
||||||
// Charset
|
// Charset
|
||||||
//---------
|
|
||||||
const (
|
const (
|
||||||
CharsetUTF8 = "charset=utf-8"
|
CharsetUTF8 = "charset=utf-8"
|
||||||
)
|
)
|
||||||
|
|
||||||
//---------
|
|
||||||
// Headers
|
// Headers
|
||||||
//---------
|
|
||||||
const (
|
const (
|
||||||
AcceptEncoding = "Accept-Encoding"
|
AcceptEncoding = "Accept-Encoding"
|
||||||
Authorization = "Authorization"
|
Authorization = "Authorization"
|
||||||
@ -207,9 +199,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
//--------
|
|
||||||
// Errors
|
// Errors
|
||||||
//--------
|
|
||||||
var (
|
var (
|
||||||
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
|
||||||
ErrNotFound = NewHTTPError(http.StatusNotFound)
|
ErrNotFound = NewHTTPError(http.StatusNotFound)
|
||||||
@ -219,9 +209,7 @@ var (
|
|||||||
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
|
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
|
||||||
)
|
)
|
||||||
|
|
||||||
//----------------
|
|
||||||
// Error handlers
|
// Error handlers
|
||||||
//----------------
|
|
||||||
var (
|
var (
|
||||||
notFoundHandler = HandlerFunc(func(c Context) error {
|
notFoundHandler = HandlerFunc(func(c Context) error {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
|
@ -48,14 +48,22 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
|
|||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
||||||
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
||||||
w := pool.Get().(*gzip.Writer)
|
rw := c.Response().Writer()
|
||||||
w.Reset(c.Response().Writer())
|
gw := pool.Get().(*gzip.Writer)
|
||||||
|
gw.Reset(rw)
|
||||||
defer func() {
|
defer func() {
|
||||||
w.Close()
|
if c.Response().Size() == 0 {
|
||||||
pool.Put(w)
|
// We have to reset response to it's pristine state when
|
||||||
w.Close()
|
// nothing is written to body or error is returned.
|
||||||
|
// See issue #424, #407.
|
||||||
|
c.Response().SetWriter(rw)
|
||||||
|
c.Response().Header().Del(echo.ContentEncoding)
|
||||||
|
gw.Reset(ioutil.Discard)
|
||||||
|
}
|
||||||
|
gw.Close()
|
||||||
|
pool.Put(gw)
|
||||||
}()
|
}()
|
||||||
g := gzipResponseWriter{Response: c.Response(), Writer: w}
|
g := gzipResponseWriter{Response: c.Response(), Writer: gw}
|
||||||
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
||||||
c.Response().SetWriter(g)
|
c.Response().SetWriter(g)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
@ -10,31 +12,12 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type closeNotifyingRecorder struct {
|
|
||||||
*test.ResponseRecorder
|
|
||||||
closed chan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCloseNotifyingRecorder() *closeNotifyingRecorder {
|
|
||||||
return &closeNotifyingRecorder{
|
|
||||||
test.NewResponseRecorder(),
|
|
||||||
make(chan bool, 1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *closeNotifyingRecorder) close() {
|
|
||||||
c.closed <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
|
|
||||||
return c.closed
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGzip(t *testing.T) {
|
func TestGzip(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
req := test.NewRequest(echo.GET, "/", nil)
|
req := test.NewRequest(echo.GET, "/", nil)
|
||||||
rec := test.NewResponseRecorder()
|
rec := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(req, rec, e)
|
c := echo.NewContext(req, rec, e)
|
||||||
|
|
||||||
// Skip if no Accept-Encoding header
|
// Skip if no Accept-Encoding header
|
||||||
h := Gzip()(echo.HandlerFunc(func(c echo.Context) error {
|
h := Gzip()(echo.HandlerFunc(func(c echo.Context) error {
|
||||||
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
||||||
@ -50,7 +33,6 @@ func TestGzip(t *testing.T) {
|
|||||||
|
|
||||||
// Gzip
|
// Gzip
|
||||||
h.Handle(c)
|
h.Handle(c)
|
||||||
// assert.Equal(t, http.StatusOK, rec.Status())
|
|
||||||
assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
|
assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
|
||||||
assert.Contains(t, rec.Header().Get(echo.ContentType), echo.TextPlain)
|
assert.Contains(t, rec.Header().Get(echo.ContentType), echo.TextPlain)
|
||||||
r, err := gzip.NewReader(rec.Body)
|
r, err := gzip.NewReader(rec.Body)
|
||||||
@ -62,79 +44,37 @@ func TestGzip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestGzipFlush(t *testing.T) {
|
func TestGzipNoContent(t *testing.T) {
|
||||||
// res := test.NewResponseRecorder()
|
e := echo.New()
|
||||||
// buf := new(bytes.Buffer)
|
req := test.NewRequest(echo.GET, "/", nil)
|
||||||
// w := gzip.NewWriter(buf)
|
rec := test.NewResponseRecorder()
|
||||||
// gw := gzipWriter{Writer: w, ResponseWriter: res}
|
c := echo.NewContext(req, rec, e)
|
||||||
//
|
h := Gzip()(echo.HandlerFunc(func(c echo.Context) error {
|
||||||
// n0 := buf.Len()
|
return c.NoContent(http.StatusOK)
|
||||||
// if n0 != 0 {
|
}))
|
||||||
// t.Fatalf("buffer size = %d before writes; want 0", n0)
|
h.Handle(c)
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if err := gw.Flush(); err != nil {
|
|
||||||
// t.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// n1 := buf.Len()
|
|
||||||
// if n1 == 0 {
|
|
||||||
// t.Fatal("no data after first flush")
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// gw.Write([]byte("x"))
|
|
||||||
//
|
|
||||||
// n2 := buf.Len()
|
|
||||||
// if n1 != n2 {
|
|
||||||
// t.Fatalf("after writing a single byte, size changed from %d to %d; want no change", n1, n2)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if err := gw.Flush(); err != nil {
|
|
||||||
// t.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// n3 := buf.Len()
|
|
||||||
// if n2 == n3 {
|
|
||||||
// t.Fatal("Flush didn't flush any data")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestGzipCloseNotify(t *testing.T) {
|
assert.Empty(t, rec.Header().Get(echo.ContentEncoding))
|
||||||
// rec := newCloseNotifyingRecorder()
|
assert.Empty(t, rec.Header().Get(echo.ContentType))
|
||||||
// buf := new(bytes.Buffer)
|
b, err := ioutil.ReadAll(rec.Body)
|
||||||
// w := gzip.NewWriter(buf)
|
if assert.NoError(t, err) {
|
||||||
// gw := gzipWriter{Writer: w, ResponseWriter: rec}
|
assert.Equal(t, 0, len(b))
|
||||||
// closed := false
|
}
|
||||||
// notifier := gw.CloseNotify()
|
}
|
||||||
// rec.close()
|
|
||||||
//
|
func TestGzipErrorReturned(t *testing.T) {
|
||||||
// select {
|
e := echo.New()
|
||||||
// case <-notifier:
|
e.Use(Gzip())
|
||||||
// closed = true
|
e.Get("/", echo.HandlerFunc(func(c echo.Context) error {
|
||||||
// case <-time.After(time.Second):
|
return echo.NewHTTPError(http.StatusInternalServerError, "error")
|
||||||
// }
|
}))
|
||||||
//
|
req := test.NewRequest(echo.GET, "/", nil)
|
||||||
// assert.Equal(t, closed, true)
|
rec := test.NewResponseRecorder()
|
||||||
// }
|
e.ServeHTTP(req, rec)
|
||||||
//
|
|
||||||
// func BenchmarkGzip(b *testing.B) {
|
assert.Empty(t, rec.Header().Get(echo.ContentEncoding))
|
||||||
// b.StopTimer()
|
b, err := ioutil.ReadAll(rec.Body)
|
||||||
// b.ReportAllocs()
|
if assert.NoError(t, err) {
|
||||||
//
|
assert.Equal(t, "error", string(b))
|
||||||
// h := func(c echo.Context) error {
|
}
|
||||||
// c.Response().Write([]byte("test")) // For Content-Type sniffing
|
}
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
// req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
||||||
// req.Header().Set(echo.AcceptEncoding, "gzip")
|
|
||||||
//
|
|
||||||
// b.StartTimer()
|
|
||||||
//
|
|
||||||
// for i := 0; i < b.N; i++ {
|
|
||||||
// e := echo.New()
|
|
||||||
// res := test.NewResponseRecorder()
|
|
||||||
// c := echo.NewContext(req, res, e)
|
|
||||||
// Gzip()(h)(c)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
@ -78,7 +78,7 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
|
|||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if err := next.Handle(c); err != nil {
|
if err := next.Handle(c); err != nil {
|
||||||
return err
|
c.Error(err)
|
||||||
}
|
}
|
||||||
stop := time.Now()
|
stop := time.Now()
|
||||||
method := []byte(req.Method())
|
method := []byte(req.Method())
|
||||||
|
Loading…
Reference in New Issue
Block a user