🩹 handle invalid http method

pull/672/head
kiyon 2020-07-24 22:55:57 +08:00
parent c2f552d92e
commit cf22aa2ae5
2 changed files with 24 additions and 0 deletions

View File

@ -805,6 +805,22 @@ func Test_Ctx_Method(t *testing.T) {
utils.AssertEqual(t, MethodPost, ctx.Method())
}
// go test -run Test_Ctx_InvalidMethod
func Test_Ctx_InvalidMethod(t *testing.T) {
t.Parallel()
app := New()
app.Get("/", func(c *Ctx) {})
fctx := &fasthttp.RequestCtx{}
fctx.Request.Header.SetMethod("InvalidMethod")
fctx.Request.SetRequestURI("/")
app.handler(fctx)
utils.AssertEqual(t, 400, fctx.Response.StatusCode())
utils.AssertEqual(t, []byte("Invalid http method"), fctx.Response.Body())
}
// go test -run Test_Ctx_MultipartForm
func Test_Ctx_MultipartForm(t *testing.T) {
t.Parallel()

View File

@ -128,6 +128,14 @@ func (app *App) next(ctx *Ctx) bool {
func (app *App) handler(rctx *fasthttp.RequestCtx) {
// Acquire Ctx with fasthttp request from pool
ctx := app.AcquireCtx(rctx)
// handle invalid http method directly
if ctx.methodINT == -1 {
ctx.Status(StatusBadRequest).SendString("Invalid http method")
app.ReleaseCtx(ctx)
return
}
// Prettify path
ctx.prettifyPath()
// Find match in stack