forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vishal Rana <[email protected]>
- Loading branch information
Showing
9 changed files
with
150 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/labstack/gommon/bytes" | ||
) | ||
|
||
type ( | ||
// BodyLimitConfig defines the config for body limit middleware. | ||
BodyLimitConfig struct { | ||
Limit string `json:"limit"` | ||
limit int | ||
} | ||
|
||
limitedReader struct { | ||
BodyLimitConfig | ||
reader io.Reader | ||
read int | ||
context echo.Context | ||
} | ||
) | ||
|
||
// BodyLimit returns a body limit middleware. | ||
// | ||
// BodyLimit middleware sets the maximum allowed size for a request body, if the | ||
// size exceeds the configured limit, it sends "413 - Request Entity Too Large" | ||
// response. The body limit is determined based on the actually read and not `Content-Length` | ||
// request header, which makes it super secure. | ||
// Limit can be specifed as `4x` or `4xB`, where x is one of the multple from K, M, | ||
// G, T or P. | ||
func BodyLimit(limit string) echo.MiddlewareFunc { | ||
return BodyLimitWithConfig(BodyLimitConfig{Limit: limit}) | ||
} | ||
|
||
// BodyLimitWithConfig returns a body limit middleware from config. | ||
// See `BodyLimit()`. | ||
func BodyLimitWithConfig(config BodyLimitConfig) echo.MiddlewareFunc { | ||
limit, err := bytes.Parse(config.Limit) | ||
if err != nil { | ||
panic(fmt.Errorf("invalid body-limit=%s", config.Limit)) | ||
} | ||
config.limit = limit | ||
pool := limitedReaderPool(config) | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
req := c.Request() | ||
r := pool.Get().(*limitedReader) | ||
r.Reset(req.Body(), c) | ||
defer pool.Put(r) | ||
req.SetBody(r) | ||
return next(c) | ||
} | ||
} | ||
} | ||
|
||
func (r *limitedReader) Read(b []byte) (n int, err error) { | ||
n, err = r.reader.Read(b) | ||
r.read += n | ||
if r.read > r.limit { | ||
s := http.StatusRequestEntityTooLarge | ||
r.context.String(s, http.StatusText(s)) | ||
return n, echo.ErrStatusRequestEntityTooLarge | ||
} | ||
return | ||
} | ||
|
||
func (r *limitedReader) Reset(reader io.Reader, context echo.Context) { | ||
r.reader = reader | ||
r.context = context | ||
} | ||
|
||
func limitedReaderPool(c BodyLimitConfig) sync.Pool { | ||
return sync.Pool{ | ||
New: func() interface{} { | ||
return &limitedReader{BodyLimitConfig: c} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package middleware | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"bytes" | ||
|
||
"github.com/labstack/echo" | ||
"github.com/labstack/echo/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBodyLimit(t *testing.T) { | ||
e := echo.New() | ||
req := test.NewRequest(echo.POST, "/", bytes.NewReader([]byte("Hello, World!"))) | ||
rec := test.NewResponseRecorder() | ||
c := e.NewContext(req, rec) | ||
h := func(c echo.Context) error { | ||
body, _ := ioutil.ReadAll(c.Request().Body()) | ||
return c.String(http.StatusOK, string(body)) | ||
} | ||
|
||
// Within limit | ||
BodyLimit("2M")(h)(c) | ||
assert.Equal(t, http.StatusOK, rec.Status()) | ||
assert.Equal(t, "Hello, World!", rec.Body.String()) | ||
|
||
// Overlimit | ||
// BodyLimit("2B")(h)(c) | ||
// assert.Equal(t, "Hello, World!", rec.Body.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters