From 5913964d55543c7ea965ba635fb637f79993a969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 09:50:24 +0200 Subject: [PATCH 1/8] Check if content-range header is present. If true, don't compress the body. --- compression_handler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compression_handler.go b/compression_handler.go index d5e30f4..88287d6 100644 --- a/compression_handler.go +++ b/compression_handler.go @@ -74,7 +74,7 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r req = req.WithContext(ctx) } - if !reqOption.ShouldCompress() || req.Body == nil { + if !reqOption.ShouldCompress() || contentRangeIsPresent(req.Header) || req.Body == nil { return pipeline.Next(req, middlewareIndex) } if span != nil { @@ -129,6 +129,11 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r return resp, nil } +func contentRangeIsPresent(header http.Header) bool { + _, contentRangePresent := header["Content-Range"] + return contentRangePresent +} + func compressReqBody(reqBody []byte) (io.ReadCloser, int, error) { var buffer bytes.Buffer gzipWriter := gzip.NewWriter(&buffer) From 81f8de358721ce73e07a955d43c55755f446e0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 09:51:47 +0200 Subject: [PATCH 2/8] Set content-length field, if it is present in the abstract header. --- nethttp_request_adapter.go | 1 + 1 file changed, 1 insertion(+) diff --git a/nethttp_request_adapter.go b/nethttp_request_adapter.go index 243483d..e668c4f 100644 --- a/nethttp_request_adapter.go +++ b/nethttp_request_adapter.go @@ -295,6 +295,7 @@ func (a *NetHttpRequestAdapter) getRequestFromRequestInformation(ctx context.Con } if request.Header.Get("Content-Length") != "" { contentLenVal, _ := strconv.Atoi(request.Header.Get("Content-Length")) + request.ContentLength = int64(contentLenVal) spanForAttributes.SetAttributes( attribute.Int("http.request_content_length", contentLenVal), ) From 47c4d882fd921ae3f928c08835b121be43f1aff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 10:28:13 +0200 Subject: [PATCH 3/8] Add test for compression handling in presence of a Content-Range header --- compression_handler_test.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/compression_handler_test.go b/compression_handler_test.go index 1e6a3f7..ebb82a5 100644 --- a/compression_handler_test.go +++ b/compression_handler_test.go @@ -42,7 +42,7 @@ func TestCompressionHandlerAddsContentEncodingHeader(t *testing.T) { assert.Equal(t, contentTypeHeader, "gzip") } -func TestCopmressionHandlerCopmressesRequestBody(t *testing.T) { +func TestCompressionHandlerCompressesRequestBody(t *testing.T) { postBody, _ := json.Marshal(map[string]string{"name": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.a line in section 1.10.32. `, "email": "Test@Test.com"}) var compressedBody []byte @@ -59,6 +59,24 @@ func TestCopmressionHandlerCopmressesRequestBody(t *testing.T) { } +func TestCompressionHandlerContentRangeRequestBody(t *testing.T) { + postBody, _ := json.Marshal(map[string]string{"name": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.a line in section 1.10.32. + `, "email": "Test@Test.com"}) + var compressedBody []byte + testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) { + compressedBody, _ = io.ReadAll(req.Body) + fmt.Fprint(res, `{}`) + })) + defer testServer.Close() + + client := GetDefaultClient(NewCompressionHandler()) + req, _ := nethttp.NewRequest("PUT", testServer.URL, bytes.NewBuffer(postBody)) + req.Header.Add("Content-Range", "bytes 0-3/4") + client.Do(req) + + assert.Equal(t, len(postBody), len(compressedBody)) +} + func TestCompressionHandlerRetriesRequest(t *testing.T) { postBody, _ := json.Marshal(map[string]string{"name": "Test", "email": "Test@Test.com"}) status := 415 From 1893bd46d3cea885cab82cb40133e96a4192f2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 10:50:05 +0200 Subject: [PATCH 4/8] fixing typo --- compression_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compression_handler.go b/compression_handler.go index 88287d6..62d84e6 100644 --- a/compression_handler.go +++ b/compression_handler.go @@ -35,7 +35,7 @@ func NewCompressionHandler() *CompressionHandler { return NewCompressionHandlerWithOptions(options) } -// NewCompressionHandlerWithOptions creates an instance of the compression middlerware with +// NewCompressionHandlerWithOptions creates an instance of the compression middleware with // specified configurations. func NewCompressionHandlerWithOptions(option CompressionOptions) *CompressionHandler { return &CompressionHandler{options: option} From 0475bec50730b8dc7f2065cb3e3d8b25252bac1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 11:07:40 +0200 Subject: [PATCH 5/8] Update CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0ad101..594aba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +## [1.4.2] - 2024-07-16 + +### Changed + +- Prevent compression if Content-Range header is present. +- Fix bug which leads to a mission Content-Length header. + ## [1.4.1] - 2024-05-09 ### Changed From e2aac710325154cfe4f4471378ba092e6bf51073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 11:10:29 +0200 Subject: [PATCH 6/8] Fix typo in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 594aba3..2815b3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Prevent compression if Content-Range header is present. -- Fix bug which leads to a mission Content-Length header. +- Fix bug which leads to a missing Content-Length header. ## [1.4.1] - 2024-05-09 From 9b6c5cf2d6abaa0c23eae8d06b4c811fea0016f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 12:55:23 +0200 Subject: [PATCH 7/8] Check if content-range is a bytes range --- compression_handler.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/compression_handler.go b/compression_handler.go index 62d84e6..3ef82d5 100644 --- a/compression_handler.go +++ b/compression_handler.go @@ -74,7 +74,7 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r req = req.WithContext(ctx) } - if !reqOption.ShouldCompress() || contentRangeIsPresent(req.Header) || req.Body == nil { + if !reqOption.ShouldCompress() || contentRangeBytesIsPresent(req.Header) || req.Body == nil { return pipeline.Next(req, middlewareIndex) } if span != nil { @@ -129,9 +129,14 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r return resp, nil } -func contentRangeIsPresent(header http.Header) bool { - _, contentRangePresent := header["Content-Range"] - return contentRangePresent +func contentRangeBytesIsPresent(header http.Header) bool { + contentRanges, _ := header["Content-Range"] + for _, contentRange := range contentRanges { + if strings.Contains(strings.ToLower(contentRange), "bytes") { + return true + } + } + return false } func compressReqBody(reqBody []byte) (io.ReadCloser, int, error) { From 1671364669c5176cd1e72f2ebe9f4677d4376041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=BCchele?= Date: Tue, 16 Jul 2024 12:56:18 +0200 Subject: [PATCH 8/8] Add missing import --- compression_handler.go | 1 + 1 file changed, 1 insertion(+) diff --git a/compression_handler.go b/compression_handler.go index 3ef82d5..309264d 100644 --- a/compression_handler.go +++ b/compression_handler.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "io" "net/http" + "strings" abstractions "github.com/microsoft/kiota-abstractions-go" "go.opentelemetry.io/otel"