MCPcopy
hub / github.com/keploy/keploy / parseFinalHTTP

Method parseFinalHTTP

pkg/agent/proxy/integrations/http/http.go:176–321  ·  view source on GitHub ↗

ParseFinalHTTP is used to parse the final http request and response and save it in a yaml file

(ctx context.Context, mock *FinalHTTP, destPort uint, mocks chan<- *models.Mock, opts models.OutgoingOptions, onMockRecorded integrations.PostRecordHook)

Source from the content-addressed store, hash-verified

174
175// ParseFinalHTTP is used to parse the final http request and response and save it in a yaml file
176func (h *HTTP) parseFinalHTTP(ctx context.Context, mock *FinalHTTP, destPort uint, mocks chan<- *models.Mock, opts models.OutgoingOptions, onMockRecorded integrations.PostRecordHook) error {
177 var req *http.Request
178 // converts the request message buffer to http request
179 req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(mock.Req)))
180 if err != nil {
181 utils.LogError(h.Logger, err, "failed to parse the http request message")
182 return err
183 }
184
185 // Set the host header explicitly because the `http.ReadRequest`` trim the host header
186 // func ReadRequest(b *bufio.Reader) (*Request, error) {
187 // req, err := readRequest(b)
188 // if err != nil {
189 // return nil, err
190 // }
191
192 // delete(req.Header, "Host")
193 // return req, err
194 // }
195 req.Header.Set("Host", req.Host)
196
197 var reqBody []byte
198 if req.Body != nil { // Read
199 var err error
200 reqBody, err = io.ReadAll(req.Body)
201 if err != nil {
202 // TODO right way to log errors
203 utils.LogError(h.Logger, err, "failed to read the http request body", zap.Any("metadata", utils.GetReqMeta(req)))
204 return err
205 }
206
207 if req.Header.Get("Content-Encoding") != "" {
208 reqBody, err = pkg.Decompress(h.Logger, req.Header.Get("Content-Encoding"), reqBody)
209 if err != nil {
210 utils.LogError(h.Logger, err, "failed to decode the http request body", zap.Any("metadata", utils.GetReqMeta(req)))
211 return err
212 }
213 }
214 }
215
216 // converts the response message buffer to http response
217 respParsed, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(mock.Resp)), req)
218 if err != nil {
219 utils.LogError(h.Logger, err, "failed to parse the http response message", zap.Any("metadata", utils.GetReqMeta(req)))
220 return err
221 }
222
223 //Add the content length to the headers.
224 var respBody []byte
225 //Checking if the body of the response is empty or does not exist.
226 if respParsed.Body != nil { // Read
227 respBody, err = io.ReadAll(respParsed.Body)
228 if err != nil {
229 utils.LogError(h.Logger, err, "failed to read the the http response body", zap.Any("metadata", utils.GetReqMeta(req)))
230 return err
231 }
232
233 if respParsed.Header.Get("Content-Encoding") != "" {

Calls 14

LogErrorFunction · 0.92
GetReqMetaFunction · 0.92
DecompressFunction · 0.92
IsPassThroughFunction · 0.92
GetVersionFunction · 0.92
MethodTypeAlias · 0.92
ToYamlHTTPHeaderFunction · 0.92
URLParamsFunction · 0.92
FromContextOrGlobalFunction · 0.92
AddMockMethod · 0.80
GetMethod · 0.65
DebugMethod · 0.65