Skip to content

Commit

Permalink
fix Content-Length & Content-Encoding if http response is decompresse…
Browse files Browse the repository at this point in the history
…d by k6

fix grafana#3373
  • Loading branch information
lucaske authored and kekaifun committed Oct 9, 2023
1 parent 6aa49bc commit 022acb0
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/netext/httpext/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"

"github.com/andybalholm/brotli"
Expand Down Expand Up @@ -143,6 +144,7 @@ func readResponseBody(
contentEncodings := strings.Split(resp.Header.Get("Content-Encoding"), ",")
// Transparently decompress the body if it's has a content-encoding we
// support. If not, simply return it as it is.
var unprocessedEncodings []string
for i := len(contentEncodings) - 1; i >= 0; i-- {
contentEncoding := strings.TrimSpace(contentEncodings[i])
if compression, err := CompressionTypeString(contentEncoding); err == nil {
Expand All @@ -168,12 +170,16 @@ func readResponseBody(
return nil, newDecompressionError(err)
}
rc = &readCloser{decoder}
} else {
// if there is a content-encoding we don't support, stop decompress and just return the left part as it is.
unprocessedEncodings = contentEncodings[:i+1]
break
}
}

buf := state.BufferPool.Get()
defer state.BufferPool.Put(buf)
_, err := io.Copy(buf, rc.Reader)
n, err := io.Copy(buf, rc.Reader)
if err != nil {
respErr = wrapDecompressionError(err)
}
Expand All @@ -183,6 +189,13 @@ func readResponseBody(
respErr = wrapDecompressionError(err)
}

if len(unprocessedEncodings) == 0 {
resp.Header.Del("Content-Encoding")
} else {
resp.Header.Set("Content-Encoding", strings.Join(unprocessedEncodings, ","))
}
resp.Header.Set("Content-Length", strconv.FormatInt(n, 10))

var result interface{}
// Binary or string
switch respType {
Expand Down

0 comments on commit 022acb0

Please sign in to comment.