-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathheaders.go
More file actions
39 lines (34 loc) · 809 Bytes
/
headers.go
File metadata and controls
39 lines (34 loc) · 809 Bytes
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
39
package rest
import (
"net/http"
"strconv"
"strings"
)
// ParseSizeFromHeaders parses HTTP response headers to get the full file size.
// Returns -1 if the headers did not exist or were invalid.
func ParseSizeFromHeaders(headers http.Header) (size int64) {
size = -1
var contentLength = headers.Get("Content-Length")
if len(contentLength) != 0 {
var err error
if size, err = strconv.ParseInt(contentLength, 10, 64); err != nil {
return -1
}
}
var contentRange = headers.Get("Content-Range")
if len(contentRange) == 0 {
return size
}
if !strings.HasPrefix(contentRange, "bytes ") {
return -1
}
slash := strings.IndexRune(contentRange, '/')
if slash < 0 {
return -1
}
ret, err := strconv.ParseInt(contentRange[slash+1:], 10, 64)
if err != nil {
return -1
}
return ret
}