🔧 Enhance response body handling with gzip decompression and logging improvements in main.go

This commit is contained in:
2026-04-19 12:31:34 +08:00
parent 906947ce91
commit 6dcd2ae46d
+29 -1
View File
@@ -2,6 +2,8 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"compress/gzip"
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
@@ -108,7 +110,33 @@ func handleProxy(cfg Config) http.HandlerFunc {
log.Println("Request streaming: ", requestId, isStreamingRequest) log.Println("Request streaming: ", requestId, isStreamingRequest)
if !isStreamingRequest { if !isStreamingRequest {
io.Copy(w, resp.Body) log.Println("Request streaming: ", requestId, false)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Read response error: ", requestId, err)
io.Copy(w, resp.Body)
return
}
printRawBody := true
contentEncoding := resp.Header.Get("Content-Encoding")
if contentEncoding == "gzip" {
gr, err := gzip.NewReader(bytes.NewReader(body))
if err != nil {
log.Println("Decompress error: ", requestId, err)
} else {
decodedBody, err := io.ReadAll(gr)
if err != nil {
log.Println("Decompress error: ", requestId, err)
}
gr.Close()
printRawBody = false
log.Println("Response[decoded]: ", requestId, contentEncoding, string(decodedBody))
}
}
if printRawBody {
log.Println("Response[raw]: ", requestId, string(body))
}
w.Write(body)
return return
} }