@@ -33,22 +33,27 @@ func IsRequestBodyTooLargeError(err error) bool {
3333 return errors .As (err , & mbe )
3434}
3535
36- func GetRequestBody (c * gin.Context ) ([] byte , error ) {
36+ func GetRequestBody (c * gin.Context ) (io. Seeker , error ) {
3737 // 首先检查是否有 BodyStorage 缓存
3838 if storage , exists := c .Get (KeyBodyStorage ); exists && storage != nil {
3939 if bs , ok := storage .(BodyStorage ); ok {
4040 if _ , err := bs .Seek (0 , io .SeekStart ); err != nil {
4141 return nil , fmt .Errorf ("failed to seek body storage: %w" , err )
4242 }
43- return bs . Bytes ()
43+ return bs , nil
4444 }
4545 }
4646
4747 // 检查旧的缓存方式
4848 cached , exists := c .Get (KeyRequestBody )
4949 if exists && cached != nil {
5050 if b , ok := cached .([]byte ); ok {
51- return b , nil
51+ bs , err := CreateBodyStorage (b )
52+ if err != nil {
53+ return nil , err
54+ }
55+ c .Set (KeyBodyStorage , bs )
56+ return bs , nil
5257 }
5358 }
5459
@@ -74,47 +79,20 @@ func GetRequestBody(c *gin.Context) ([]byte, error) {
7479 // 缓存存储对象
7580 c .Set (KeyBodyStorage , storage )
7681
77- // 获取字节数据
78- body , err := storage .Bytes ()
79- if err != nil {
80- return nil , err
81- }
82-
83- // 同时设置旧的缓存键以保持兼容性
84- c .Set (KeyRequestBody , body )
85-
86- return body , nil
82+ return storage , nil
8783}
8884
8985// GetBodyStorage 获取请求体存储对象(用于需要多次读取的场景)
9086func GetBodyStorage (c * gin.Context ) (BodyStorage , error ) {
91- // 检查是否已有存储
92- if storage , exists := c .Get (KeyBodyStorage ); exists && storage != nil {
93- if bs , ok := storage .(BodyStorage ); ok {
94- if _ , err := bs .Seek (0 , io .SeekStart ); err != nil {
95- return nil , fmt .Errorf ("failed to seek body storage: %w" , err )
96- }
97- return bs , nil
98- }
99- }
100-
101- // 如果没有,调用 GetRequestBody 创建存储
102- _ , err := GetRequestBody (c )
87+ seeker , err := GetRequestBody (c )
10388 if err != nil {
10489 return nil , err
10590 }
106-
107- // 再次获取存储
108- if storage , exists := c .Get (KeyBodyStorage ); exists && storage != nil {
109- if bs , ok := storage .(BodyStorage ); ok {
110- if _ , err := bs .Seek (0 , io .SeekStart ); err != nil {
111- return nil , fmt .Errorf ("failed to seek body storage: %w" , err )
112- }
113- return bs , nil
114- }
91+ bs , ok := seeker .(BodyStorage )
92+ if ! ok {
93+ return nil , errors .New ("unexpected body storage type" )
11594 }
116-
117- return nil , errors .New ("failed to get body storage" )
95+ return bs , nil
11896}
11997
12098// CleanupBodyStorage 清理请求体存储(应在请求结束时调用)
@@ -128,13 +106,14 @@ func CleanupBodyStorage(c *gin.Context) {
128106}
129107
130108func UnmarshalBodyReusable (c * gin.Context , v any ) error {
131- requestBody , err := GetRequestBody (c )
109+ storage , err := GetBodyStorage (c )
110+ if err != nil {
111+ return err
112+ }
113+ requestBody , err := storage .Bytes ()
132114 if err != nil {
133115 return err
134116 }
135- //if DebugEnabled {
136- // println("UnmarshalBodyReusable request body:", string(requestBody))
137- //}
138117 contentType := c .Request .Header .Get ("Content-Type" )
139118 if strings .HasPrefix (contentType , "application/json" ) {
140119 err = Unmarshal (requestBody , v )
@@ -150,7 +129,10 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error {
150129 return err
151130 }
152131 // Reset request body
153- c .Request .Body = io .NopCloser (bytes .NewBuffer (requestBody ))
132+ if _ , seekErr := storage .Seek (0 , io .SeekStart ); seekErr != nil {
133+ return seekErr
134+ }
135+ c .Request .Body = io .NopCloser (storage )
154136 return nil
155137}
156138
@@ -252,7 +234,11 @@ func init() {
252234}
253235
254236func ParseMultipartFormReusable (c * gin.Context ) (* multipart.Form , error ) {
255- requestBody , err := GetRequestBody (c )
237+ storage , err := GetBodyStorage (c )
238+ if err != nil {
239+ return nil , err
240+ }
241+ requestBody , err := storage .Bytes ()
256242 if err != nil {
257243 return nil , err
258244 }
@@ -270,7 +256,10 @@ func ParseMultipartFormReusable(c *gin.Context) (*multipart.Form, error) {
270256 }
271257
272258 // Reset request body
273- c .Request .Body = io .NopCloser (bytes .NewBuffer (requestBody ))
259+ if _ , seekErr := storage .Seek (0 , io .SeekStart ); seekErr != nil {
260+ return nil , seekErr
261+ }
262+ c .Request .Body = io .NopCloser (storage )
274263 return form , nil
275264}
276265
0 commit comments