-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.go
More file actions
68 lines (57 loc) · 1.67 KB
/
range.go
File metadata and controls
68 lines (57 loc) · 1.67 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
63
64
65
66
67
68
package httpreader
import (
"fmt"
"net/http"
"strings"
)
/*
httpRange represents a range of content bytes that can be used in HTTP
responses to specify partial content retrieval.
*/
type httpRange struct {
start int64
end int64
}
/*
The String method converts the httpRange into a string that follows the
HTTP range header format.
Example:
r1 := httpRange{start: 10, end: 20} // Represents bytes 10 to 20
fmt.Println(r1.String()) // Output: "10-20"
r2 := httpRange{start: 5, end: -1} // Represents bytes starting from 5 to the end
fmt.Println(r2.String()) // Output: "5-"
r3 := httpRange{start: -1, end: 15} // Represents the last 15 bytes
fmt.Println(r3.String()) // Output: "-15"
In cases where the start or end are invalid or the range is not well-defined,
the String method 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(url string, headers http.Header, ranges ...*httpRange) (*http.Request, error) {
if req, err := http.NewRequest(http.MethodGet, url, nil); err == nil {
req.Header = headers.Clone()
setRangeHeader(req.Header, ranges...)
return req, err
} else {
return nil, err
}
}