From ac2569aa80c9be529051471288302c639d0bc888 Mon Sep 17 00:00:00 2001 From: Tate Date: Tue, 25 Jun 2024 07:44:16 -0600 Subject: [PATCH] Only set the body as binary when the Content-Type Header is a binary type. --- internal/server/http/dump.go | 29 ++++++++++++++++++++++++++--- internal/server/http/server_test.go | 22 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/internal/server/http/dump.go b/internal/server/http/dump.go index 861bfb4..b9b4255 100644 --- a/internal/server/http/dump.go +++ b/internal/server/http/dump.go @@ -166,6 +166,25 @@ func writeLog(writer io.Writer, params handlers.LogFormatterParams, body string) writer.Write(buf) } +// isBinaryContent checks to see if the body is a common binary content type +func isBinaryContent(r *http.Request) bool { + contentType := r.Header.Get("Content-Type") + binaryContentTypes := []string{ + "application/octet-stream", + "image/", + "audio/", + "video/", + "application/pdf", + } + + for _, binaryType := range binaryContentTypes { + if strings.HasPrefix(contentType, binaryType) { + return true + } + } + return false +} + // CustomLoggingHandler provides a way to supply a custom log formatter // while taking advantage of the mechanisms in this package func CustomLoggingHandler(out io.Writer, h http.Handler, s *Server) http.Handler { @@ -176,18 +195,22 @@ func CustomLoggingHandler(out io.Writer, h http.Handler, s *Server) http.Handler return } params.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) // Reset the body - base64Body := base64.StdEncoding.EncodeToString(bodyBytes) + body := string(bodyBytes) + // if content is binary, encode it to base64 + if isBinaryContent(params.Request) { + body = base64.StdEncoding.EncodeToString(bodyBytes) + } verbose := s.verbose // record request, if error set verbose to true to log current request since // it didn't make it into a full channel - err = recordRequest(params.Request, s, base64Body) + err = recordRequest(params.Request, s, body) if err != nil { verbose = true } if verbose { - writeLog(writer, params, base64Body) + writeLog(writer, params, body) } }) } diff --git a/internal/server/http/server_test.go b/internal/server/http/server_test.go index e62239f..b60daa5 100644 --- a/internal/server/http/server_test.go +++ b/internal/server/http/server_test.go @@ -219,6 +219,7 @@ func TestBuildLogRequests(t *testing.T) { testCases := map[string]struct { method string path string + contentType string body string expectedLog string expectedStatus int @@ -226,6 +227,15 @@ func TestBuildLogRequests(t *testing.T) { "GET valid imposter request": { method: "GET", path: "/yamlTestDumpRequest", + contentType: "text/plain", + body: "Dumped", + expectedLog: "GET /yamlTestDumpRequest HTTP/1.1\" 200 17 Dumped\n", + expectedStatus: http.StatusOK, + }, + "GET valid imposter binary request": { + method: "GET", + path: "/yamlTestDumpRequest", + contentType: "application/octet-stream", body: "Dumped", expectedLog: "GET /yamlTestDumpRequest HTTP/1.1\" 200 17 RHVtcGVk\n", expectedStatus: http.StatusOK, @@ -233,6 +243,7 @@ func TestBuildLogRequests(t *testing.T) { "GET valid imposter request no body": { method: "GET", path: "/yamlTestDumpRequest", + contentType: "text/plain", body: "", expectedLog: "GET /yamlTestDumpRequest HTTP/1.1\" 200 17\n", expectedStatus: http.StatusOK, @@ -240,6 +251,15 @@ func TestBuildLogRequests(t *testing.T) { "GET invalid imposter request": { method: "GET", path: "/doesnotexist", + contentType: "text/plain", + body: "Dumped", + expectedLog: "GET /doesnotexist HTTP/1.1\" 404 19 Dumped\n", + expectedStatus: http.StatusNotFound, + }, + "GET invalid imposter binary request": { + method: "GET", + path: "/doesnotexist", + contentType: "video/mp4", body: "Dumped", expectedLog: "GET /doesnotexist HTTP/1.1\" 404 19 RHVtcGVk\n", expectedStatus: http.StatusNotFound, @@ -247,6 +267,7 @@ func TestBuildLogRequests(t *testing.T) { "GET invalid imposter request no body": { method: "GET", path: "/doesnotexist", + contentType: "text/plain", body: "", expectedLog: "GET /doesnotexist HTTP/1.1\" 404 19\n", expectedStatus: http.StatusNotFound, @@ -270,6 +291,7 @@ func TestBuildLogRequests(t *testing.T) { w := httptest.NewRecorder() req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body)) + req.Header.Set("Content-Type", tc.contentType) server.httpServer.Handler.ServeHTTP(w, req)