From 7f9467d1f87652b9946939a453549dd866f357af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E4=BA=AE?= <739476267@qq.com> Date: Wed, 20 Dec 2023 20:25:23 +0800 Subject: [PATCH] fix: #2938 (#3178) --- example/httpserver/upload_file/main.go | 44 +++++++++++++++++++ net/ghttp/ghttp_request_param.go | 2 - net/ghttp/ghttp_z_unit_feature_config_test.go | 8 +++- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 example/httpserver/upload_file/main.go diff --git a/example/httpserver/upload_file/main.go b/example/httpserver/upload_file/main.go new file mode 100644 index 00000000000..e103a572560 --- /dev/null +++ b/example/httpserver/upload_file/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "context" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" +) + +type UploadReq struct { + g.Meta `path:"/upload" method:"POST" tags:"Upload" mime:"multipart/form-data" summary:"上传文件"` + File *ghttp.UploadFile `p:"file" type:"file" dc:"选择上传文件"` + Msg string `dc:"消息"` +} +type UploadRes struct { + FileName string `json:"fileName"` +} + +type cUpload struct{} + +func (u cUpload) Upload(ctx context.Context, req *UploadReq) (*UploadRes, error) { + if req.File != nil { + return &UploadRes{ + FileName: req.File.Filename, + }, nil + } + return nil, nil +} + +func main() { + s := g.Server() + s.Group("/", func(group *ghttp.RouterGroup) { + group.Middleware(ghttp.MiddlewareHandlerResponse) + group.Bind(cUpload{}) + }) + s.SetClientMaxBodySize(600 * 1024 * 1024) // 600M + s.SetPort(8199) + s.SetAccessLogEnabled(true) + s.Run() +} + +// curl --location 'http://127.0.0.1:8199/upload' \ +// --form 'file=@"/D:/下载/goframe-v2.5.pdf"' \ +// --form 'msg="666"' diff --git a/net/ghttp/ghttp_request_param.go b/net/ghttp/ghttp_request_param.go index 806428ae4e4..aaa873468cf 100644 --- a/net/ghttp/ghttp_request_param.go +++ b/net/ghttp/ghttp_request_param.go @@ -269,8 +269,6 @@ func (r *Request) parseForm() { return } if contentType := r.Header.Get("Content-Type"); contentType != "" { - r.MakeBodyRepeatableRead(true) - var err error if gstr.Contains(contentType, "multipart/") { // multipart/form-data, multipart/mixed diff --git a/net/ghttp/ghttp_z_unit_feature_config_test.go b/net/ghttp/ghttp_z_unit_feature_config_test.go index e3542ce2a2d..e408e4f625f 100644 --- a/net/ghttp/ghttp_z_unit_feature_config_test.go +++ b/net/ghttp/ghttp_z_unit_feature_config_test.go @@ -8,6 +8,7 @@ package ghttp_test import ( "fmt" + "strings" "testing" "time" @@ -154,8 +155,11 @@ func Test_ClientMaxBodySize_File(t *testing.T) { t.Assert(gfile.PutBytes(path, data), nil) defer gfile.Remove(path) t.Assert( - gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)), - "Read from request Body failed: http: request body too large", + true, + strings.Contains( + gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)), + "http: request body too large", + ), ) }) }