MCPcopy Index your code
hub / github.com/keploy/keploy / decodeHTTP

Method decodeHTTP

pkg/agent/proxy/integrations/http/decode.go:31–328  ·  view source on GitHub ↗

Decodes the mocks in test mode so that they can be sent to the user application.

(ctx context.Context, reqBuf []byte, clientConn net.Conn, dstCfg *models.ConditionalDstCfg, mockDb integrations.MockMemDb, opts models.OutgoingOptions)

Source from the content-addressed store, hash-verified

29
30// Decodes the mocks in test mode so that they can be sent to the user application.
31func (h *HTTP) decodeHTTP(ctx context.Context, reqBuf []byte, clientConn net.Conn, dstCfg *models.ConditionalDstCfg, mockDb integrations.MockMemDb, opts models.OutgoingOptions) error {
32 errCh := make(chan error, 1)
33 go func(errCh chan error, reqBuf []byte, opts models.OutgoingOptions) {
34 defer pUtil.Recover(h.Logger, clientConn, nil)
35 defer close(errCh)
36 for {
37 //Check if the expected header is present
38 if bytes.Contains(reqBuf, []byte("Expect: 100-continue")) {
39 h.Logger.Debug("The expect header is present in the request buffer and writing the 100 continue response to the client")
40 //Send the 100 continue response
41 _, err := clientConn.Write([]byte("HTTP/1.1 100 Continue\r\n\r\n"))
42 if err != nil {
43 if ctx.Err() != nil {
44 return
45 }
46 utils.LogError(h.Logger, err, "failed to write the 100 continue response to the user application")
47 errCh <- err
48 return
49 }
50 h.Logger.Debug("The 100 continue response has been sent to the user application")
51 //Read the request buffer again
52 newRequest, err := pUtil.ReadBytes(ctx, h.Logger, clientConn)
53 if err != nil {
54 utils.LogError(h.Logger, err, "failed to read the request buffer from the user application")
55 errCh <- err
56 return
57 }
58 //Append the new request buffer to the old request buffer
59 reqBuf = append(reqBuf, newRequest...)
60 }
61
62 h.Logger.Debug("handling the chunked requests to read the complete request")
63 err := h.HandleChunkedRequests(ctx, &reqBuf, clientConn, nil)
64 if err != nil {
65 utils.LogError(h.Logger, err, "failed to handle chunked requests")
66 errCh <- err
67 return
68 }
69
70 h.Logger.Debug(fmt.Sprintf("This is the complete request:\n%v", string(reqBuf)))
71
72 //Parse the request buffer
73 request, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(reqBuf)))
74 if err != nil {
75 utils.LogError(h.Logger, err, "failed to parse the http request message")
76 errCh <- err
77 return
78 }
79
80 h.Logger.Debug("Decoded HTTP request headers", zap.Any("headers", request.Header))
81 // Set the host header explicitly because the `http.ReadRequest`` trim the host header
82 // func ReadRequest(b *bufio.Reader) (*Request, error) {
83 // req, err := readRequest(b)
84 // if err != nil {
85 // return nil, err
86 // }
87
88 // delete(req.Header, "Host")

Callers 1

MockOutgoingMethod · 0.95

Calls 15

HandleChunkedRequestsMethod · 0.95
matchMethod · 0.95
LogErrorFunction · 0.92
GetReqMetaFunction · 0.92
DecompressFunction · 0.92
NewMockMismatchErrorFunction · 0.92
ToHTTPHeaderFunction · 0.92
CompressFunction · 0.92
DebugMethod · 0.65
WriteMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected