-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
38 lines (32 loc) · 850 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package godm
import (
"net/http"
"strconv"
"strings"
)
func getHeaders(client *http.Client, url string, config *DownloadConfig) (*HeaderInfo, error) {
// head request to get metadata
// Note: the uncompressed payload is considered for Content-Length
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
if config.Compress {
req.Header.Set("Accept-Encoding", strings.Join(supportedEncodings, ", "))
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
l, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return nil, err
}
return &HeaderInfo{
IsAcceptRanges: resp.Header.Get("Accept-Ranges") == "bytes",
Length: l,
ETag: resp.Header.Get("ETag"),
Encoding: resp.Header.Get("Content-Encoding"),
}, nil
}