Skip to content

Commit

Permalink
Merge pull request #1214 from microsoft/bg-dropped-reqbody-#46
Browse files Browse the repository at this point in the history
Bg-dropped-reqbody-#46
  • Loading branch information
baywet authored Feb 16, 2022
2 parents 5a656c4 + f7b3c31 commit 2851572
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed a bug where request body would get dropped by the compression handler in Go
- Fixed an issue where multiple api clients could run into racing conditions in Go.
- Fixed a bug where empty additional data in Go would lead to invalid JSON payloads during serialization.
- Fixed a bug where Go serialization would write empty arrays for nil values.
Expand Down
18 changes: 8 additions & 10 deletions http/go/nethttp/compression_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r
return nil, err
}

compressedBody, contentLength, err := compressReqBody(req.Body)
compressedBody, size, err := compressReqBody(unCompressedBody)
if err != nil {
return nil, err
}

req.Header.Set("Content-Encoding", "gzip")
req.Body = compressedBody
req.ContentLength = int64(contentLength)
req.ContentLength = int64(size)

// Sending request with compressed body
resp, err := pipeline.Next(req, middlewareIndex)
Expand All @@ -99,18 +99,16 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r
return resp, nil
}

func compressReqBody(reqBody io.ReadCloser) (io.ReadCloser, int, error) {
body, err := ioutil.ReadAll(reqBody)
if err != nil {
func compressReqBody(reqBody []byte) (io.ReadCloser, int, error) {
var buffer bytes.Buffer
gzipWriter := gzip.NewWriter(&buffer)
if _, err := gzipWriter.Write(reqBody); err != nil {
return nil, 0, err
}

var buffer bytes.Buffer
gzipWriter := gzip.NewWriter(&buffer)
if _, err := gzipWriter.Write(body); err != nil {
if err := gzipWriter.Close(); err != nil {
return nil, 0, err
}
defer gzipWriter.Close()

return ioutil.NopCloser(bytes.NewBuffer(buffer.Bytes())), len(buffer.Bytes()), nil
return ioutil.NopCloser(&buffer), buffer.Len(), nil
}
11 changes: 5 additions & 6 deletions http/go/nethttp/compression_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,21 @@ func TestCompressionHandlerAddsContentEncodingHeader(t *testing.T) {
assert.Equal(t, contentTypeHeader, "gzip")
}

func TestCompressionHandlerCompressesRequestBody(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": "Test", "email": "Test@Test.com"})
func TestCopmressionHandlerCopmressesRequestBody(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)
res.Header().Set("Content-Type", "application/json")
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()

client := getDefaultClientWithoutMiddleware()
client.Transport = NewCustomTransport(NewCompressionHandler())
client := GetDefaultClient(NewCompressionHandler())
client.Post(testServer.URL, "application/json", bytes.NewBuffer(postBody))

assert.Greater(t, len(postBody), len(compressedBody))

}

func TestCompressionHandlerRetriesRequest(t *testing.T) {
Expand Down

0 comments on commit 2851572

Please sign in to comment.