MCPcopy
hub / github.com/keploy/keploy / chunkedRequest

Method chunkedRequest

pkg/agent/proxy/integrations/http/chunk.go:185–227  ·  view source on GitHub ↗

Handled chunked requests when transfer-encoding is given.

(ctx context.Context, finalReq *[]byte, clientConn, destConn net.Conn, _ string)

Source from the content-addressed store, hash-verified

183
184// Handled chunked requests when transfer-encoding is given.
185func (h *HTTP) chunkedRequest(ctx context.Context, finalReq *[]byte, clientConn, destConn net.Conn, _ string) error {
186
187 for {
188 select {
189 case <-ctx.Done():
190 return ctx.Err()
191 default:
192 //TODO: we have to implement a way to read the buffer chunk wise according to the chunk size (chunk size comes in hexadecimal)
193 // because it can happen that some chunks come after 5 seconds.
194 err := clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
195 if err != nil {
196 utils.LogError(h.Logger, err, "failed to set the read deadline for the client conn")
197 return err
198 }
199 requestChunked, err := pUtil.ReadBytes(ctx, h.Logger, clientConn)
200 if err != nil {
201 if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
202 break
203 }
204 utils.LogError(h.Logger, nil, "failed to read the response message from the destination server")
205 return err
206 }
207
208 *finalReq = append(*finalReq, requestChunked...)
209 // destConn is nil in case of test mode.
210 if destConn != nil {
211 _, err = destConn.Write(requestChunked)
212 if err != nil {
213 if ctx.Err() != nil {
214 return ctx.Err()
215 }
216 utils.LogError(h.Logger, nil, "failed to write request message to the destination server")
217 return err
218 }
219 }
220
221 //check if the initial request is completed
222 if strings.HasSuffix(string(requestChunked), "0\r\n\r\n") {
223 return nil
224 }
225 }
226 }
227}
228
229func (h *HTTP) handleChunkedResponses(ctx context.Context, finalResp *[]byte, clientConn, destConn net.Conn, resp []byte) error {
230

Callers 1

HandleChunkedRequestsMethod · 0.95

Calls 5

LogErrorFunction · 0.92
AddMethod · 0.80
TimeoutMethod · 0.65
WriteMethod · 0.65
SetReadDeadlineMethod · 0.45

Tested by

no test coverage detected