Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
bugfix: set tls config if existed when dfget downloads from the sourc…
Browse files Browse the repository at this point in the history
…e station

Signed-off-by: zhouchencheng <[email protected]>
  • Loading branch information
zcc35357949 committed Sep 29, 2019
1 parent d9b3ac0 commit 1bb19a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dfget/core/downloader/back_downloader/back_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (bd *BackDownloader) Run() error {
bd.tempFileName = f.Name()
defer f.Close()

if resp, err = httputils.HTTPGet(bd.URL, netutils.ConvertHeaders(bd.cfg.Header)); err != nil {
if resp, err = httputils.HTTPGetWithTLS(bd.URL, netutils.ConvertHeaders(bd.cfg.Header), 0, bd.cfg.Cacerts, bd.cfg.Insecure); err != nil {
return err
}
defer resp.Body.Close()
Expand Down
41 changes: 37 additions & 4 deletions pkg/httputils/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package httputils

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"reflect"
Expand Down Expand Up @@ -210,16 +213,37 @@ func Do(url string, headers map[string]string, timeout time.Duration) (string, e

// HTTPGet sends an HTTP GET request with headers.
func HTTPGet(url string, headers map[string]string) (*http.Response, error) {
return HTTPWithHeaders("GET", url, headers, 0)
return HTTPWithHeaders("GET", url, headers, 0, nil)
}

// HTTPGetTimeout sends an HTTP GET request with timeout.
func HTTPGetTimeout(url string, headers map[string]string, timeout time.Duration) (*http.Response, error) {
return HTTPWithHeaders("GET", url, headers, timeout)
return HTTPWithHeaders("GET", url, headers, timeout, nil)
}

// HTTPGetWithTLS sends an HTTP GET request with TLS config.
func HTTPGetWithTLS(url string, headers map[string]string, timeout time.Duration, cacerts []string, insecure bool) (*http.Response, error) {
roots := x509.NewCertPool()
appendSuccess := false
for _, certPath := range cacerts {
certBytes, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
appendSuccess = appendSuccess || roots.AppendCertsFromPEM(certBytes)
}

tlsConfig := &tls.Config{
InsecureSkipVerify: insecure,
}
if appendSuccess {
tlsConfig.RootCAs = roots
}
return HTTPWithHeaders("GET", url, headers, timeout, tlsConfig)
}

// HTTPWithHeaders sends an HTTP request with headers and specified method.
func HTTPWithHeaders(method, url string, headers map[string]string, timeout time.Duration) (*http.Response, error) {
func HTTPWithHeaders(method, url string, headers map[string]string, timeout time.Duration, tlsConfig *tls.Config) (*http.Response, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
Expand All @@ -229,7 +253,16 @@ func HTTPWithHeaders(method, url string, headers map[string]string, timeout time
req.Header.Add(k, v)
}

c := &http.Client{}
var transport http.RoundTripper
if tlsConfig != nil {
transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
}

c := &http.Client{
Transport: transport,
}
if timeout > 0 {
c.Timeout = timeout
}
Expand Down

0 comments on commit 1bb19a6

Please sign in to comment.