Set DisableCompression to true to disable unwanted httpclient transformation

Signed-off-by: Idan Elhalwani <idan@elhalwani.com>
pull/8070/head
Idan Elhalwani 4 years ago
parent cdc809cb88
commit 93b0eee0df

@ -90,6 +90,10 @@ func NewHTTPGetter(options ...Option) (Getter, error) {
}
func (g *HTTPGetter) httpClient() (*http.Client, error) {
transport := &http.Transport{
DisableCompression: true,
Proxy: http.ProxyFromEnvironment,
}
if (g.opts.certFile != "" && g.opts.keyFile != "") || g.opts.caFile != "" {
tlsConf, err := tlsutil.NewClientTLS(g.opts.certFile, g.opts.keyFile, g.opts.caFile)
if err != nil {
@ -103,28 +107,19 @@ func (g *HTTPGetter) httpClient() (*http.Client, error) {
}
tlsConf.ServerName = sni
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConf,
Proxy: http.ProxyFromEnvironment,
},
}
return client, nil
transport.TLSClientConfig = tlsConf
}
if g.opts.insecureSkipVerifyTLS {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Proxy: http.ProxyFromEnvironment,
},
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
return client, nil
}
return http.DefaultClient, nil
client := &http.Client{
Transport: transport,
}
return client, nil
}

@ -17,10 +17,13 @@ package getter
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
@ -248,3 +251,39 @@ func TestDownloadInsecureSkipTLSVerify(t *testing.T) {
}
}
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")
defer f.Close()
b := make([]byte, 512)
f.Read(b)
//Get the file size
FileStat, _ := f.Stat()
FileSize := strconv.FormatInt(FileStat.Size(), 10)
//Simulating improper header values from bitbucket
w.Header().Set("Content-Type", "application/x-tar")
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Length", FileSize)
f.Seek(0, 0)
io.Copy(w, f)
}))
defer srv.Close()
g, err := NewHTTPGetter(WithURL(srv.URL))
if err != nil {
t.Fatal(err)
}
data, _ := g.Get(srv.URL)
mimeType := http.DetectContentType(data.Bytes())
expectedMimeType := "application/x-gzip"
if mimeType != expectedMimeType {
t.Fatalf("Expected response with MIME type %s, but got %s", expectedMimeType, mimeType)
}
}

Binary file not shown.
Loading…
Cancel
Save