From c3f14a0ad6ee54f0e474a220a8eb81a67a6b6335 Mon Sep 17 00:00:00 2001
From: Chenrui <631807682@qq.com>
Date: Thu, 9 Jul 2020 09:45:40 +0800
Subject: [PATCH] refactor: log error when payload too large
---
error.go | 13 +++++++------
router.go | 2 ++
router_test.go | 2 +-
3 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/error.go b/error.go
index 0b148974..f268f723 100644
--- a/error.go
+++ b/error.go
@@ -363,12 +363,13 @@ func gatewayTimeout(rw http.ResponseWriter, r *http.Request) {
func payloadTooLarge(rw http.ResponseWriter, r *http.Request) {
responseError(rw, r,
413,
- "
The page you have requested is unavailable."+
- "
Perhaps you are here because:"+
- "
"+
- "
The request entity is larger than limits defined by server"+
- "
Please change the request entity and try again."+
- "
",
+ `
The page you have requested is unavailable.
+
Perhaps you are here because:
+
+
The request entity is larger than limits defined by server.
+
Please change the request entity and try again.
+
+ `,
)
}
diff --git a/router.go b/router.go
index 9b257391..d910deb0 100644
--- a/router.go
+++ b/router.go
@@ -707,7 +707,9 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if r.Method != http.MethodGet && r.Method != http.MethodHead {
if BConfig.CopyRequestBody && !context.Input.IsUpload() {
+ // connection will close if the incoming data are larger (RFC 7231, 6.5.11)
if r.ContentLength > BConfig.MaxMemory {
+ logs.Error(errors.New("payload too large"))
exception("413", context)
goto Admin
}
diff --git a/router_test.go b/router_test.go
index e49f38db..8ec7927a 100644
--- a/router_test.go
+++ b/router_test.go
@@ -726,7 +726,7 @@ func TestRouterEntityTooLargeCopyBody(t *testing.T) {
BConfig.CopyRequestBody = _CopyRequestBody
BConfig.MaxMemory = _MaxMemory
- if w.Code != 413 {
+ if w.Code != http.StatusRequestEntityTooLarge {
t.Errorf("TestRouterRequestEntityTooLarge can't run")
}
}