fix: replace with github.com/andybalholm/brotli to avoid using cgo

This commit is contained in:
debugtalk
2022-02-21 18:25:01 +08:00
parent fd29a6cf8c
commit b3762dbbbc
3 changed files with 26 additions and 5 deletions

View File

@@ -10,6 +10,7 @@ import (
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
@@ -25,7 +26,7 @@ import (
"testing"
"time"
"github.com/google/brotli/go/cbrotli"
"github.com/andybalholm/brotli"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -807,10 +808,30 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
return stepResult, err
}
// convert brotli.Reader to io.ReadCloser,
// so that we can assign it to resp.Body
type brotliReader struct {
r io.Reader
}
func (br *brotliReader) Read(p []byte) (n int, err error) {
return br.r.Read(p)
}
func (br *brotliReader) Close() (err error) {
return nil
}
func newBrotliReader(r io.Reader) io.ReadCloser {
b := &brotliReader{}
b.r = brotli.NewReader(r)
return b
}
func decodeResponseBody(resp *http.Response) error {
switch resp.Header.Get("Content-Encoding") {
case "br":
resp.Body = cbrotli.NewReader(resp.Body)
resp.Body = newBrotliReader(resp.Body)
case "gzip":
gr, err := gzip.NewReader(resp.Body)
if err != nil {