-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.go
More file actions
62 lines (54 loc) · 1.52 KB
/
range.go
File metadata and controls
62 lines (54 loc) · 1.52 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package httpreader
import (
"fmt"
"net/http"
"net/url"
"strings"
)
/*
httpRange represents a range of content bytes that can be used in HTTP
responses to specify partial content retrieval.
Example:
httpRange{start: 10, end: 20} // Represents "Range: bytes=10-20"
httpRange{start: 5, end: -1} // Represents "Range: bytes=5-"
httpRange{start: -1, end: 15} // Represents "Range: bytes=-15"
*/
type httpRange struct {
start int64
end int64
}
/*
The String method converts the httpRange into a string that follows the
HTTP range header format. In cases where the start or end are invalid or
the range is not well-defined, String() returns an empty string.
*/
func (r httpRange) String() string {
if r.start < 0 && r.end >= 0 {
return fmt.Sprintf("-%d", r.end) // -<suffix-length>
}
if r.start >= 0 && r.end < 0 {
return fmt.Sprintf("%d-", r.start) // <start>-
}
if r.start >= 0 && r.end >= 0 && r.start <= r.end {
return fmt.Sprintf("%d-%d", r.start, r.end) // <start>-<end>
}
return ""
}
func setRangeHeader(header http.Header, ranges ...*httpRange) {
var r []string
for _, httpRange := range ranges {
if s := httpRange.String(); s != "" {
r = append(r, s)
}
}
header.Set("Range", "bytes="+strings.Join(r, ","))
}
func newRangeRequest(u *url.URL, headers http.Header, ranges ...*httpRange) (*http.Request, error) {
if req, err := http.NewRequest(http.MethodGet, u.String(), nil); err == nil {
req.Header = headers.Clone()
setRangeHeader(req.Header, ranges...)
return req, err
} else {
return nil, err
}
}