Skip to content

Commit d979e61

Browse files
committed
Handle compressed response in openvsx proxy
1 parent 8ad398e commit d979e61

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

components/openvsx-proxy/pkg/modifyresponse.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package pkg
66

77
import (
88
"bytes"
9+
"compress/gzip"
910
"fmt"
1011
"io/ioutil"
1112
"net/http"
@@ -48,19 +49,19 @@ func (o *OpenVSXProxy) ModifyResponse(r *http.Response) error {
4849
return nil
4950
}
5051

51-
body, err := ioutil.ReadAll(r.Body)
52+
rawBody, err := ioutil.ReadAll(r.Body)
5253
if err != nil {
53-
log.WithFields(logFields).WithError(err).Error("error reading response body")
54+
log.WithFields(logFields).WithError(err).Error("error reading response raw body")
5455
return err
5556
}
5657
r.Body.Close()
57-
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
58+
r.Body = ioutil.NopCloser(bytes.NewBuffer(rawBody))
5859

5960
if r.StatusCode >= 500 || r.StatusCode == http.StatusTooManyRequests || r.StatusCode == http.StatusRequestTimeout {
6061
// use cache if exists
6162
bodyLogField := "(binary)"
62-
if utf8.Valid(body) {
63-
bodyStr := string(body)
63+
if utf8.Valid(rawBody) {
64+
bodyStr := string(rawBody)
6465
truncatedSuffix := ""
6566
if len(bodyStr) > 500 {
6667
truncatedSuffix = "... [truncated]"
@@ -93,8 +94,25 @@ func (o *OpenVSXProxy) ModifyResponse(r *http.Response) error {
9394
}
9495

9596
// no error (status code < 500)
97+
body := rawBody
9698
contentType := r.Header.Get("Content-Type")
9799
if strings.HasPrefix(contentType, "application/json") {
100+
if strings.EqualFold(r.Header.Get("Content-Encoding"), "gzip") {
101+
gzipReader, err := gzip.NewReader(ioutil.NopCloser(bytes.NewBuffer(rawBody)))
102+
if err != nil {
103+
log.WithFields(logFields).WithError(err)
104+
}
105+
106+
body, err = ioutil.ReadAll(gzipReader)
107+
if err != nil {
108+
log.WithFields(logFields).WithError(err).Error("error reading response body")
109+
return err
110+
}
111+
gzipReader.Close()
112+
113+
r.Header.Del("Content-Encoding")
114+
}
115+
98116
if log.Log.Level >= logrus.DebugLevel {
99117
log.WithFields(logFields).Debugf("replacing %d occurence(s) of '%s' in response body ...", strings.Count(string(body), o.Config.URLUpstream), o.Config.URLUpstream)
100118
}

0 commit comments

Comments
 (0)