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