Accept `gzip` for fetching a repository index file.

The change introduces a new `getter` option - `enableCompression`, and enables the feature for downloading a repository index file.

When the option value is `true`:
- `HttpGetter` adds "Accept-Encoding: gzip" header to a request.
- `HttpGetter` performs decompression of a response body if the response contains "Content-Encoding: gzip" header.

Signed-off-by: Alex Prizov <prizov@me.com>
pull/11027/head
prizov 2 years ago committed by Alex Prizov
parent a4d6c736bc
commit 774b135776
No known key found for this signature in database
GPG Key ID: 58381A4D326D1846

@ -42,6 +42,7 @@ type options struct {
passCredentialsAll bool
userAgent string
version string
enableCompression bool
registryClient *registry.Client
timeout time.Duration
transport *http.Transport
@ -115,6 +116,13 @@ func WithRegistryClient(client *registry.Client) Option {
}
}
// WithCompression enables compression
func WithCompression() Option {
return func(opts *options) {
opts.enableCompression = true
}
}
func WithUntar() Option {
return func(opts *options) {
opts.unTar = true

@ -17,6 +17,7 @@ package getter
import (
"bytes"
"compress/gzip"
"crypto/tls"
"io"
"net/http"
@ -78,6 +79,10 @@ func (g *HTTPGetter) get(href string) (*bytes.Buffer, error) {
}
}
if g.opts.enableCompression {
req.Header.Add("Accept-Encoding", "gzip")
}
client, err := g.httpClient()
if err != nil {
return nil, err
@ -93,7 +98,19 @@ func (g *HTTPGetter) get(href string) (*bytes.Buffer, error) {
}
buf := bytes.NewBuffer(nil)
_, err = io.Copy(buf, resp.Body)
body := resp.Body
if g.opts.enableCompression && resp.Header.Get("Content-Encoding") == "gzip" {
body, err = gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer body.Close()
}
_, err = io.Copy(buf, body)
return buf, err
}

@ -16,6 +16,8 @@ limitations under the License.
package getter
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"net/http"
@ -363,6 +365,30 @@ func TestDownloadInsecureSkipTLSVerify(t *testing.T) {
}
func TestHTTPGetterWithCompression(t *testing.T) {
expectedData := []byte("index.yaml")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(w)
gzipWriter.Write(expectedData)
gzipWriter.Close()
}))
defer srv.Close()
g, err := NewHTTPGetter(WithURL(srv.URL), WithCompression())
if err != nil {
t.Fatal(err)
}
data, _ := g.Get(srv.URL)
if bytes.Compare(data.Bytes(), expectedData) != 0 {
t.Fatalf("Expected response with uncompressed data %s, but got %s", expectedData, data.Bytes())
}
}
func TestHTTPGetterTarDownload(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, _ := os.Open("testdata/empty-0.0.1.tgz")

@ -131,6 +131,7 @@ func (r *ChartRepository) DownloadIndexFile() (string, error) {
getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile),
getter.WithBasicAuth(r.Config.Username, r.Config.Password),
getter.WithPassCredentialsAll(r.Config.PassCredentialsAll),
getter.WithCompression(),
)
if err != nil {
return "", err

Loading…
Cancel
Save