mirror of
https://github.com/labstack/echo.git
synced 2025-01-26 03:20:08 +02:00
parent
0edb31b3bd
commit
1ac5425ec4
3
echo.go
3
echo.go
@ -382,7 +382,7 @@ func (e *Echo) URI(h Handler, params ...interface{}) string {
|
|||||||
return uri.String()
|
return uri.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL is an alias for URI
|
// URL is an alias for URI.
|
||||||
func (e *Echo) URL(h Handler, params ...interface{}) string {
|
func (e *Echo) URL(h Handler, params ...interface{}) string {
|
||||||
return e.URI(h, params...)
|
return e.URI(h, params...)
|
||||||
}
|
}
|
||||||
@ -400,6 +400,7 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
c.reset(r, w, e)
|
c.reset(r, w, e)
|
||||||
if h == nil {
|
if h == nil {
|
||||||
|
c.response.status = http.StatusNotFound // Helpful to skip middleware
|
||||||
h = e.notFoundHandler
|
h = e.notFoundHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,8 @@ func Gzip() echo.MiddlewareFunc {
|
|||||||
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c *echo.Context) error {
|
return func(c *echo.Context) error {
|
||||||
if (c.Request().Header.Get(echo.Upgrade)) != echo.WebSocket && // Skip for WebSocket
|
if (c.Request().Header.Get(echo.Upgrade)) != echo.WebSocket && // Skip for WebSocket
|
||||||
strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) {
|
strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) &&
|
||||||
|
c.Response().Status() != http.StatusNotFound { // Skip for "404 - Not Found"
|
||||||
w := gzip.NewWriter(c.Response().Writer())
|
w := gzip.NewWriter(c.Response().Writer())
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()}
|
gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()}
|
||||||
|
@ -1,30 +1,38 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"bytes"
|
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGzip(t *testing.T) {
|
func TestGzip(t *testing.T) {
|
||||||
// Empty Accept-Encoding header
|
|
||||||
req, _ := http.NewRequest(echo.GET, "/", nil)
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
||||||
h := func(c *echo.Context) error {
|
h := func(c *echo.Context) error {
|
||||||
return c.String(http.StatusOK, "test")
|
return c.String(http.StatusOK, "test")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip if no Accept-Encoding header
|
||||||
Gzip()(h)(c)
|
Gzip()(h)(c)
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
assert.Equal(t, "test", rec.Body.String())
|
assert.Equal(t, "test", rec.Body.String())
|
||||||
|
|
||||||
// With Accept-Encoding header
|
// Skip if WebSocket
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
c = echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
||||||
|
c.Request().Header.Set(echo.Upgrade, echo.WebSocket)
|
||||||
|
Gzip()(h)(c)
|
||||||
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
|
assert.NotEqual(t, "gzip", rec.Header().Get(echo.ContentEncoding))
|
||||||
|
|
||||||
|
// Gzip
|
||||||
req, _ = http.NewRequest(echo.GET, "/", nil)
|
req, _ = http.NewRequest(echo.GET, "/", nil)
|
||||||
req.Header.Set(echo.AcceptEncoding, "gzip")
|
req.Header.Set(echo.AcceptEncoding, "gzip")
|
||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
|
@ -74,7 +74,7 @@ types of handlers.
|
|||||||
|
|
||||||
Routes with common prefix can be grouped to define a new sub-router with optional
|
Routes with common prefix can be grouped to define a new sub-router with optional
|
||||||
middleware. If middleware is passed to the function, it overrides parent middleware
|
middleware. If middleware is passed to the function, it overrides parent middleware
|
||||||
- helpful if you want a completly new middleware stack for the group. To add middleware
|
- helpful if you want a completely new middleware stack for the group. To add middleware
|
||||||
later you can use `Group.Use(m ...Middleware)`. Groups can also be nested.
|
later you can use `Group.Use(m ...Middleware)`. Groups can also be nested.
|
||||||
|
|
||||||
In the code below, we create an admin group which requires basic HTTP authentication
|
In the code below, we create an admin group which requires basic HTTP authentication
|
||||||
@ -254,7 +254,7 @@ RedirectToSlashOptions struct {
|
|||||||
e.Use(mw.RedirectToSlash())
|
e.Use(mw.RedirectToSlash())
|
||||||
```
|
```
|
||||||
|
|
||||||
*Note*: StripTrailingSlash and RedirectToSlash should not be used together.
|
> StripTrailingSlash and RedirectToSlash middleware should not be used together.
|
||||||
|
|
||||||
[Examples](https://github.com/labstack/echo/tree/master/examples/middleware)
|
[Examples](https://github.com/labstack/echo/tree/master/examples/middleware)
|
||||||
|
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
.social a {
|
.social a {
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.social a:not(:first-child) {
|
||||||
|
margin-left: 20px;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user