Created
October 7, 2025 07:29
-
-
Save florianl/ca0847ae9f6e67547ea4e68d4369b54d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From 9c4bc84cc73f612182bd831c23fbd375c9db36a8 Mon Sep 17 00:00:00 2001 | |
| From: Florian Lehner <[email protected]> | |
| Date: Tue, 7 Oct 2025 09:28:45 +0200 | |
| Subject: [PATCH] createNewRequest | |
| Signed-off-by: Florian Lehner <[email protected]> | |
| --- | |
| config/confighttp/compression.go | 26 +++++++++++++++----------- | |
| 1 file changed, 15 insertions(+), 11 deletions(-) | |
| diff --git a/config/confighttp/compression.go b/config/confighttp/compression.go | |
| index 4efc6cf22..3008c13ba 100644 | |
| --- a/config/confighttp/compression.go | |
| +++ b/config/confighttp/compression.go | |
| @@ -188,15 +188,7 @@ func newCompressRoundTripper(rt http.RoundTripper, compressionType configcompres | |
| }, nil | |
| } | |
| -func (r *compressRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | |
| - if req.Header.Get(headerContentEncoding) != "" { | |
| - // If the header already specifies a content encoding then skip compression | |
| - // since we don't want to compress it again. This is a safeguard that normally | |
| - // should not happen since CompressRoundTripper is not intended to be used | |
| - // with http clients which already do their own compression. | |
| - return r.rt.RoundTrip(req) | |
| - } | |
| - | |
| +func (r *compressRoundTripper) createNewRequest(orig *http.Request) (*http.Request, error) { | |
| // Compress the body. | |
| buf := r.bufferPool.Get().(*bytes.Buffer) | |
| defer func() { | |
| @@ -204,13 +196,25 @@ func (r *compressRoundTripper) RoundTrip(req *http.Request) (*http.Response, err | |
| r.bufferPool.Put(buf) | |
| }() | |
| - if err := r.compressor.compress(buf, req.Body); err != nil { | |
| + if err := r.compressor.compress(buf, orig.Body); err != nil { | |
| return nil, err | |
| } | |
| // Create a new request since the docs say that we cannot modify the "req" | |
| // (see https://golang.org/pkg/net/http/#RoundTripper). | |
| - cReq, err := http.NewRequestWithContext(req.Context(), req.Method, req.URL.String(), buf) | |
| + return http.NewRequestWithContext(orig.Context(), orig.Method, orig.URL.String(), buf) | |
| +} | |
| + | |
| +func (r *compressRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | |
| + if req.Header.Get(headerContentEncoding) != "" { | |
| + // If the header already specifies a content encoding then skip compression | |
| + // since we don't want to compress it again. This is a safeguard that normally | |
| + // should not happen since CompressRoundTripper is not intended to be used | |
| + // with http clients which already do their own compression. | |
| + return r.rt.RoundTrip(req) | |
| + } | |
| + | |
| + cReq, err := r.createNewRequest(req) | |
| if err != nil { | |
| return nil, err | |
| } | |
| -- | |
| 2.39.5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment