Skip to content

Commit

Permalink
style: adds debug logs in proxy/http-parser (#1656)
Browse files Browse the repository at this point in the history
* style: adds debug logs in proxy/http-parser

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>

---------

Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
  • Loading branch information
re-Tick committed Mar 11, 2024
1 parent 70e0ba9 commit 8018b19
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
30 changes: 22 additions & 8 deletions pkg/proxy/integrations/httpparser/httpparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ type HttpParser struct {
}

// ProcessOutgoing implements proxy.DepInterface.
func (http *HttpParser) ProcessOutgoing(request []byte, clientConn, destConn net.Conn, ctx context.Context) {
func (h *HttpParser) ProcessOutgoing(request []byte, clientConn, destConn net.Conn, ctx context.Context) {
h.logger.Debug("Processing outgoing http call for mocking")
switch models.GetMode() {
case models.MODE_RECORD:
err := encodeOutgoingHttp(request, clientConn, destConn, http.logger, http.hooks, ctx)
h.logger.Debug("Recording the outgoing http call")
err := encodeOutgoingHttp(request, clientConn, destConn, h.logger, h.hooks, ctx)
if err != nil {
http.logger.Error("failed to encode the http message into the yaml", zap.Error(err))
h.logger.Error("failed to encode the http message into the yaml", zap.Error(err))
return
}

case models.MODE_TEST:
decodeOutgoingHttp(request, clientConn, destConn, http.hooks, http.logger)
h.logger.Debug("Mocking the outgoing http call in test mode")
decodeOutgoingHttp(request, clientConn, destConn, h.hooks, h.logger)
default:
http.logger.Info("Invalid mode detected while intercepting outgoing http call", zap.Any("mode", models.GetMode()))
h.logger.Info("Invalid mode detected while intercepting outgoing http call", zap.Any("mode", models.GetMode()))
}

}
Expand All @@ -58,14 +61,16 @@ func NewHttpParser(logger *zap.Logger, h *hooks.Hook) *HttpParser {
// IsOutgoingHTTP function determines if the outgoing network call is HTTP by comparing the
// message format with that of an HTTP text message.
func (h *HttpParser) OutgoingType(buffer []byte) bool {
return bytes.HasPrefix(buffer[:], []byte("HTTP/")) ||
isHttp := bytes.HasPrefix(buffer[:], []byte("HTTP/")) ||
bytes.HasPrefix(buffer[:], []byte("GET ")) ||
bytes.HasPrefix(buffer[:], []byte("POST ")) ||
bytes.HasPrefix(buffer[:], []byte("PUT ")) ||
bytes.HasPrefix(buffer[:], []byte("PATCH ")) ||
bytes.HasPrefix(buffer[:], []byte("DELETE ")) ||
bytes.HasPrefix(buffer[:], []byte("OPTIONS ")) ||
bytes.HasPrefix(buffer[:], []byte("HEAD "))
h.logger.Debug("checking if the outgoing network call is HTTP", zap.Any("is Http Protocol", isHttp))
return isHttp
}

func isJSON(body []byte) bool {
Expand Down Expand Up @@ -431,29 +436,37 @@ func checkIfGzipped(check io.ReadCloser) (bool, *bufio.Reader) {

// Decodes the mocks in test mode so that they can be sent to the user application.
func decodeOutgoingHttp(requestBuffer []byte, clientConn, destConn net.Conn, h *hooks.Hook, logger *zap.Logger) {
logger.Debug("into the decodeOutgoingHttp function to mock the external Http call", zap.Any("requestBuffer", string(requestBuffer)))

//Matching algorithmm
//Get the mocks
for {
logger.Debug("started the keep-alive loop for the http mocking")
remoteAddr := clientConn.RemoteAddr().(*net.TCPAddr)
sourcePort := remoteAddr.Port
//Check if the expected header is present
if bytes.Contains(requestBuffer, []byte("Expect: 100-continue")) {
logger.Debug("The expect header is present in the request buffer and writing the 100 continue response to the client")
//Send the 100 continue response
_, err := clientConn.Write([]byte("HTTP/1.1 100 Continue\r\n\r\n"))
if err != nil {
logger.Error("failed to write the 100 continue response to the user application", zap.Error(err))
return
}

logger.Debug("The 100 continue response has been sent to the user application")
//Read the request buffer again
newRequest, err := util.ReadBytes(clientConn)
if err != nil {
logger.Error("failed to read the request buffer from the user application", zap.Error(err))
return
}

logger.Debug("This is the new request buffer after the 100 continue response:\n" + string(newRequest))
//Append the new request buffer to the old request buffer
requestBuffer = append(requestBuffer, newRequest...)
}

logger.Debug("handling the chunked requests to read the complete request")
err := handleChunkedRequests(&requestBuffer, clientConn, destConn, logger)
if err != nil {
logger.Error("failed to handle chunk request", zap.Error(err))
Expand All @@ -469,6 +482,7 @@ func decodeOutgoingHttp(requestBuffer []byte, clientConn, destConn net.Conn, h *
return
}

logger.Debug("parsed the http request")
reqBody, err := io.ReadAll(req.Body)
if err != nil {
logger.Error("failed to read from request body", zap.Any("metadata", getReqMeta(req)), zap.Error(err))
Expand All @@ -486,10 +500,10 @@ func decodeOutgoingHttp(requestBuffer []byte, clientConn, destConn net.Conn, h *
isReqBodyJSON := isJSON(reqBody)

isMatched, stub, err := match(req, reqBody, reqURL, isReqBodyJSON, h, logger, clientConn, destConn, requestBuffer, h.Recover)

if err != nil {
logger.Error("error while matching http mocks", zap.Any("metadata", getReqMeta(req)), zap.Error(err))
}
logger.Debug("after matchin the http request", zap.Any("isMatched", isMatched), zap.Any("stub", stub), zap.Error(err))
if !isMatched {
passthroughHost := false
for _, filters := range h.GetPassThroughHosts().Filters {
Expand Down
1 change: 1 addition & 0 deletions pkg/proxy/integrations/httpparser/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func match(req *http.Request, reqBody []byte, reqURL *url.URL, isReqBodyJSON boo
if err != nil {
return false, nil, fmt.Errorf("error while getting tcs mocks %v", err)
}
logger.Debug("mocks to match with the http request", zap.Any("tcsMocks", tcsMocks))

var eligibleMock []*models.Mock

Expand Down
6 changes: 5 additions & 1 deletion pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ func (ps *ProxySet) handleConnection(conn net.Conn, port uint32, ctx context.Con
destConnId := util.GetNextID()
logger := ps.logger.With(zap.Any("Client IP Address", conn.RemoteAddr().String()), zap.Any("Client ConnectionID", clientConnId), zap.Any("Destination IP Address", actualAddress), zap.Any("Destination ConnectionID", destConnId))
if isTLS {
logger.Debug("", zap.Any("isTLS", isTLS))
logger.Debug("the external call is tls-encrypted", zap.Any("isTLS", isTLS))
config := &tls.Config{
InsecureSkipVerify: true,
ServerName: destinationUrl,
Expand All @@ -944,15 +944,19 @@ func (ps *ProxySet) handleConnection(conn net.Conn, port uint32, ctx context.Con
}
}

logger.Debug("checking for the passThroughProts in the proxy server...", zap.Any("passThroughPorts", ps.PassThroughPorts), zap.Any("destPort", destInfo.DestPort))

for _, port := range ps.PassThroughPorts {
if port == uint(destInfo.DestPort) {
logger.Debug("passThroughPort found", zap.Any("for port", port))
err = ps.callNext(buffer, conn, dst, logger)
if err != nil {
logger.Error("failed to pass through the outgoing call", zap.Error(err), zap.Any("for port", port))
return
}
}
}
logger.Debug("checking the buffer packet protocol", zap.Any("buffer", string(buffer)))
genericCheck := true
//Checking for all the parsers.
for _, parser := range ParsersMap {
Expand Down

0 comments on commit 8018b19

Please sign in to comment.