|
|
@ -10,6 +10,7 @@ import ( |
|
|
|
"io" |
|
|
|
"net/http" |
|
|
|
"net/url" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
// broltiDecode handles brolti encoding in http responses.
|
|
|
@ -81,23 +82,36 @@ func createClient(remoteUrl string, proxyUrl string) (http.Client, error) { |
|
|
|
}, nil |
|
|
|
} |
|
|
|
|
|
|
|
// download obtains resource file from the remote server and supports proxy.
|
|
|
|
func download(url string, proxy string) ([]byte, error) { |
|
|
|
// assetDate attempts to obtain the last modification time of the remote
|
|
|
|
// file and returns nil if it does not exist or is invalid.
|
|
|
|
func assetDate(resp *http.Response) *time.Time { |
|
|
|
date, err := http.ParseTime(resp.Header.Get(headers.LastModified)) |
|
|
|
if err != nil { |
|
|
|
logger.Warnf("Unable to get remote data modification time") |
|
|
|
return nil |
|
|
|
} |
|
|
|
logger.Debugf("Remote data modification time -> `%v`", date) |
|
|
|
return &date |
|
|
|
} |
|
|
|
|
|
|
|
// download obtains resource file from the remote server, gets its
|
|
|
|
// modification time, and supports proxy acquisition.
|
|
|
|
func download(url string, proxy string) ([]byte, *time.Time, error) { |
|
|
|
client, err := createClient(url, proxy) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil) |
|
|
|
if err != nil { |
|
|
|
logger.Errorf("Failed to create http request -> %v", err) |
|
|
|
return nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
req.Header.Set(headers.AcceptEncoding, "gzip, deflate, br") |
|
|
|
resp, err := client.Do(req) |
|
|
|
if err != nil { |
|
|
|
logger.Errorf("Failed to execute http request -> %v", err) |
|
|
|
return nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
defer resp.Body.Close() |
|
|
|
logger.Debugf("Remote data downloaded successfully") |
|
|
@ -117,8 +131,8 @@ func download(url string, proxy string) ([]byte, error) { |
|
|
|
content, err = nonDecode(resp.Body) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, nil, err |
|
|
|
} |
|
|
|
logger.Debugf("Download `%s` successfully -> %d bytes", url, len(content)) |
|
|
|
return content, nil |
|
|
|
return content, assetDate(resp), nil |
|
|
|
} |
|
|
|