From 9bcfa7a412c2cfb8d0dc2d3d0ea611c5189b284f Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Tue, 20 Aug 2024 13:38:20 +0200 Subject: [PATCH] handle gzipped requests --- main.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 2a12cbc..83d51fa 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "compress/gzip" "flag" "fmt" "io" @@ -16,12 +17,18 @@ func requestHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s %s from %s", r.Method, r.RequestURI, r.RemoteAddr) if *body { + var requestbody []byte + r.Body = http.MaxBytesReader(w, r.Body, 1048576) - body, err := io.ReadAll(r.Body) + if r.Header.Get("Content-Encoding") == "gzip" { + gzipreader, _ := gzip.NewReader(r.Body) + requestbody, _ = io.ReadAll(gzipreader) + } + requestbody, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "unable to read response", http.StatusBadRequest) } - fmt.Printf("\n--- BODY ---\n%s\n", string(body)) + fmt.Printf("\n--- BODY ---\n%s\n", string(requestbody)) } if *header {