Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always set accept encoding header #644

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions v2/pkg/engine/datasource/httpclient/nethttpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (
const (
ContentEncodingHeader = "Content-Encoding"
AcceptEncodingHeader = "Accept-Encoding"
AcceptHeader = "Accept"
ContentTypeHeader = "Content-Type"

EncodingGzip = "gzip"
EncodingDeflate = "deflate"

ContentTypeJSON = "application/json"
)

var (
Expand Down Expand Up @@ -90,16 +97,18 @@ func Do(client *http.Client, ctx context.Context, requestInput []byte, out io.Wr
request.URL.RawQuery = query.Encode()
}

request.Header.Add("accept", "application/json")
request.Header.Add("content-type", "application/json")
request.Header.Add(AcceptHeader, ContentTypeJSON)
request.Header.Add(ContentTypeHeader, ContentTypeJSON)
request.Header.Set(AcceptEncodingHeader, EncodingGzip)
request.Header.Add(AcceptEncodingHeader, EncodingDeflate)

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

respReader, err := respBodyReader(request, response)
respReader, err := respBodyReader(response)
if err != nil {
return err
}
Expand All @@ -108,17 +117,13 @@ func Do(client *http.Client, ctx context.Context, requestInput []byte, out io.Wr
return
}

func respBodyReader(req *http.Request, resp *http.Response) (io.ReadCloser, error) {
if req.Header.Get(AcceptEncodingHeader) == "" {
return resp.Body, nil
}

func respBodyReader(resp *http.Response) (io.ReadCloser, error) {
switch resp.Header.Get(ContentEncodingHeader) {
case "gzip":
case EncodingGzip:
return gzip.NewReader(resp.Body)
case "deflate":
case EncodingDeflate:
return flate.NewReader(resp.Body), nil
default:
return resp.Body, nil
}

return resp.Body, nil
}
Loading