From a53eb09c5a96a4321875d2c7f2462d1328bc82ab Mon Sep 17 00:00:00 2001 From: yoyofx Date: Sun, 10 Oct 2021 00:05:22 +0800 Subject: [PATCH] fixed request body max bytes that when upload file --- examples/simpleweb/config_dev.yml | 2 +- examples/simpleweb/contollers/usercontroller.go | 15 +++++++++++++++ examples/simpleweb/go.mod | 4 ++-- web/binding/binding.go | 4 ++++ web/fasthttpserver.go | 5 +++-- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/simpleweb/config_dev.yml b/examples/simpleweb/config_dev.yml index 54b67a78..d708c4d3 100644 --- a/examples/simpleweb/config_dev.yml +++ b/examples/simpleweb/config_dev.yml @@ -6,7 +6,7 @@ yoyogo: type: "fasthttp" address: ":8080" path: "app" - max_request_size: 2096157 + max_request_size: 31457280 session: name: "YOYOGO_SESSIONID" timeout: 3600 diff --git a/examples/simpleweb/contollers/usercontroller.go b/examples/simpleweb/contollers/usercontroller.go index 99f5b94e..4be9bb69 100644 --- a/examples/simpleweb/contollers/usercontroller.go +++ b/examples/simpleweb/contollers/usercontroller.go @@ -124,3 +124,18 @@ func (controller UserController) QueryBinding(ctx *context.HttpContext) mvc.ApiR } return controller.OK(userInfo) } + +type UploadForm struct { + mvc.RequestBody + File *multipart.FileHeader `form:"file1"` + Key string `form:"key"` +} + +func (controller UserController) Upload(form *UploadForm) mvc.ApiResult { + return controller.OK(context.H{ + "file": form.File.Filename, + "size": form.File.Size, + "key": form.Key, + }) + +} diff --git a/examples/simpleweb/go.mod b/examples/simpleweb/go.mod index b6e29bc7..d7c79ec5 100644 --- a/examples/simpleweb/go.mod +++ b/examples/simpleweb/go.mod @@ -5,10 +5,10 @@ go 1.16 require ( github.com/fasthttp/websocket v1.4.3 github.com/go-sql-driver/mysql v1.6.0 - github.com/jinzhu/copier v0.3.2 + github.com/jinzhu/copier v0.3.2 // indirect github.com/yoyofx/yoyogo v0.0.0 github.com/yoyofxteam/dependencyinjection v1.0.1 - github.com/yoyofxteam/reflectx v0.2.3 + github.com/yoyofxteam/reflectx v0.2.3 // indirect gorm.io/gorm v1.21.11 ) diff --git a/web/binding/binding.go b/web/binding/binding.go index 86a6e606..a2b181c3 100644 --- a/web/binding/binding.go +++ b/web/binding/binding.go @@ -2,6 +2,7 @@ package binding import ( "net/http" + "strings" "unsafe" ) @@ -80,6 +81,9 @@ func Default(method, contentType string) Binding { if method == http.MethodGet { return Form } + if strings.HasPrefix(contentType, MIMEMultipartPOSTForm) { + return FormMultipart + } switch contentType { case MIMEJSON: diff --git a/web/fasthttpserver.go b/web/fasthttpserver.go index ebfdb2ad..c07585a2 100644 --- a/web/fasthttpserver.go +++ b/web/fasthttpserver.go @@ -39,8 +39,9 @@ func (server *FastHttpServer) Run(context *abstractions.HostBuilderContext) (e e fastHttpHandler := NewFastHTTPHandler(context.RequestDelegate.(IRequestDelegate)) server.webserver = &fasthttp.Server{ - Handler: fastHttpHandler, - KeepHijackedConns: true, + Handler: fastHttpHandler, + KeepHijackedConns: true, + MaxRequestBodySize: int(context.HostConfiguration.Server.MaxRequestSize), } addr := server.Addr