Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/http_range/range.go
1560 views
1
// Package http_range implements http range parsing.
2
package http_range
3
4
import (
5
"errors"
6
"fmt"
7
"net/http"
8
"net/textproto"
9
"strconv"
10
"strings"
11
)
12
13
// Range specifies the byte range to be sent to the client.
14
type Range struct {
15
Start int64
16
Length int64 // limit of bytes to read, -1 for unlimited
17
}
18
19
// ContentRange returns Content-Range header value.
20
func (r Range) ContentRange(size int64) string {
21
return fmt.Sprintf("bytes %d-%d/%d", r.Start, r.Start+r.Length-1, size)
22
}
23
24
var (
25
// ErrNoOverlap is returned by ParseRange if first-byte-pos of
26
// all the byte-range-spec values is greater than the content size.
27
ErrNoOverlap = errors.New("invalid range: failed to overlap")
28
29
// ErrInvalid is returned by ParseRange on invalid input.
30
ErrInvalid = errors.New("invalid range")
31
)
32
33
// ParseRange parses a Range header string as per RFC 7233.
34
// ErrNoOverlap is returned if none of the ranges overlap.
35
// ErrInvalid is returned if s is invalid range.
36
func ParseRange(s string, size int64) ([]Range, error) { // nolint:gocognit
37
if s == "" {
38
return nil, nil // header not present
39
}
40
const b = "bytes="
41
if !strings.HasPrefix(s, b) {
42
return nil, ErrInvalid
43
}
44
var ranges []Range
45
noOverlap := false
46
for _, ra := range strings.Split(s[len(b):], ",") {
47
ra = textproto.TrimString(ra)
48
if ra == "" {
49
continue
50
}
51
i := strings.Index(ra, "-")
52
if i < 0 {
53
return nil, ErrInvalid
54
}
55
start, end := textproto.TrimString(ra[:i]), textproto.TrimString(ra[i+1:])
56
var r Range
57
if start == "" {
58
// If no start is specified, end specifies the
59
// range start relative to the end of the file,
60
// and we are dealing with <suffix-length>
61
// which has to be a non-negative integer as per
62
// RFC 7233 Section 2.1 "Byte-Ranges".
63
if end == "" || end[0] == '-' {
64
return nil, ErrInvalid
65
}
66
i, err := strconv.ParseInt(end, 10, 64)
67
if i < 0 || err != nil {
68
return nil, ErrInvalid
69
}
70
if i > size {
71
i = size
72
}
73
r.Start = size - i
74
r.Length = size - r.Start
75
} else {
76
i, err := strconv.ParseInt(start, 10, 64)
77
if err != nil || i < 0 {
78
return nil, ErrInvalid
79
}
80
if i >= size {
81
// If the range begins after the size of the content,
82
// then it does not overlap.
83
noOverlap = true
84
continue
85
}
86
r.Start = i
87
if end == "" {
88
// If no end is specified, range extends to end of the file.
89
r.Length = size - r.Start
90
} else {
91
i, err := strconv.ParseInt(end, 10, 64)
92
if err != nil || r.Start > i {
93
return nil, ErrInvalid
94
}
95
if i >= size {
96
i = size - 1
97
}
98
r.Length = i - r.Start + 1
99
}
100
}
101
ranges = append(ranges, r)
102
}
103
if noOverlap && len(ranges) == 0 {
104
// The specified ranges did not overlap with the content.
105
return nil, ErrNoOverlap
106
}
107
return ranges, nil
108
}
109
110
// ParseContentRange this function parse content-range in http response
111
func ParseContentRange(s string) (start, end int64, err error) {
112
if s == "" {
113
return 0, 0, ErrInvalid
114
}
115
const b = "bytes "
116
if !strings.HasPrefix(s, b) {
117
return 0, 0, ErrInvalid
118
}
119
p1 := strings.Index(s, "-")
120
p2 := strings.Index(s, "/")
121
if p1 < 0 || p2 < 0 {
122
return 0, 0, ErrInvalid
123
}
124
startStr, endStr := textproto.TrimString(s[len(b):p1]), textproto.TrimString(s[p1+1:p2])
125
start, startErr := strconv.ParseInt(startStr, 10, 64)
126
end, endErr := strconv.ParseInt(endStr, 10, 64)
127
128
return start, end, errors.Join(startErr, endErr)
129
}
130
131
func (r Range) MimeHeader(contentType string, size int64) textproto.MIMEHeader {
132
return textproto.MIMEHeader{
133
"Content-Range": {r.ContentRange(size)},
134
"Content-Type": {contentType},
135
}
136
}
137
138
// ApplyRangeToHttpHeader for http request header
139
func ApplyRangeToHttpHeader(p Range, headerRef http.Header) http.Header {
140
header := headerRef
141
if header == nil {
142
header = http.Header{}
143
}
144
if p.Start == 0 && p.Length < 0 {
145
header.Del("Range")
146
} else {
147
end := ""
148
if p.Length >= 0 {
149
end = strconv.FormatInt(p.Start+p.Length-1, 10)
150
}
151
header.Set("Range", fmt.Sprintf("bytes=%v-%v", p.Start, end))
152
}
153
return header
154
}
155
156