@@ -72,6 +72,11 @@ type CoProcessor struct {
7272
7373// BuildObject constructs a CoProcessObject from a given http.Request.
7474func (c * CoProcessor ) BuildObject (req * http.Request , res * http.Response , spec * APISpec ) (* coprocess.Object , error ) {
75+ logger := log .WithFields (logrus.Fields {
76+ "prefix" : "coprocess" ,
77+ "func" : "BuildObject" ,
78+ })
79+
7580 headers := ProtoMap (req .Header )
7681
7782 host := req .Host
@@ -103,15 +108,39 @@ func (c *CoProcessor) BuildObject(req *http.Request, res *http.Response, spec *A
103108 }
104109
105110 if req .Body != nil {
106- defer req .Body .Close ()
107111 var err error
108- miniRequestObject .RawBody , err = ioutil .ReadAll (req .Body )
112+ contentLength := req .ContentLength
113+ logger .Debugf ("Request body processing - Content-Length header: %d" , contentLength )
114+
115+ // Check if body is empty (might have been read already)
116+ emptyBody := false
117+ if req .GetBody == nil {
118+ logger .Debug ("Request GetBody is nil, body might have been read already" )
119+ emptyBody = true
120+ }
121+
122+ body , _ := copyBody (req .Body , false )
123+ bodyBytes , err := ioutil .ReadAll (body )
124+ bytesRead := len (bodyBytes )
125+
109126 if err != nil {
110- return nil , err
111- }
127+ logger .WithError (err ).Errorf ("Failed to read request body - Content-Length: %d, Bytes read: %d, Empty body: %v" ,
128+ contentLength , bytesRead , emptyBody )
129+ return nil , fmt .Errorf ("failed to read request body: %w (Content-Length: %d, Bytes read: %d)" ,
130+ err , contentLength , bytesRead )
131+ }
132+
133+ logger .Debugf ("Successfully read request body - Content-Length: %d, Bytes read: %d" ,
134+ contentLength , bytesRead )
135+
136+ body .Close ()
137+
138+ miniRequestObject .RawBody = bodyBytes
112139 if utf8 .Valid (miniRequestObject .RawBody ) && ! c .Middleware .RawBodyOnly {
113140 miniRequestObject .Body = string (miniRequestObject .RawBody )
114141 }
142+ } else {
143+ logger .Debug ("Request body is nil" )
115144 }
116145
117146 object := & coprocess.Object {
@@ -175,10 +204,22 @@ func (c *CoProcessor) BuildObject(req *http.Request, res *http.Response, spec *A
175204 resObj .MultivalueHeaders = append (resObj .MultivalueHeaders , & currentHeader )
176205 }
177206 resObj .StatusCode = int32 (res .StatusCode )
207+ contentLength := res .ContentLength
208+ logger .Debugf ("Response body processing - Content-Length header: %d" , contentLength )
209+
178210 rawBody , err := ioutil .ReadAll (res .Body )
211+ bytesRead := len (rawBody )
212+
179213 if err != nil {
180- return nil , err
181- }
214+ logger .WithError (err ).Errorf ("Failed to read response body - Content-Length: %d, Bytes read: %d" ,
215+ contentLength , bytesRead )
216+ return nil , fmt .Errorf ("failed to read response body: %w (Content-Length: %d, Bytes read: %d)" ,
217+ err , contentLength , bytesRead )
218+ }
219+
220+ logger .Debugf ("Successfully read response body - Content-Length: %d, Bytes read: %d" ,
221+ contentLength , bytesRead )
222+
182223 resObj .RawBody = rawBody
183224 res .Body = ioutil .NopCloser (bytes .NewReader (rawBody ))
184225 if utf8 .Valid (rawBody ) && ! c .Middleware .RawBodyOnly {
0 commit comments