-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpcontentrange.go
68 lines (62 loc) · 1.2 KB
/
httpcontentrange.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
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 missinggo
import (
"math"
"regexp"
"strconv"
)
type HTTPBytesContentRange struct {
First, Last, Length int64
}
type HTTPBytesRange struct {
First, Last int64
}
var (
bytesContentRangeRegexp = regexp.MustCompile(`bytes[ =](\d+)-(\d+)/(\d+|\*)`)
httpBytesRangeRegexp = regexp.MustCompile(`bytes[ =](\d+)-(\d*)`)
)
func ParseHTTPBytesRange(s string) (ret HTTPBytesRange, ok bool) {
ss := httpBytesRangeRegexp.FindStringSubmatch(s)
if ss == nil {
return
}
var err error
ret.First, err = strconv.ParseInt(ss[1], 10, 64)
if err != nil {
return
}
if ss[2] == "" {
ret.Last = math.MaxInt64
} else {
ret.Last, err = strconv.ParseInt(ss[2], 10, 64)
if err != nil {
return
}
}
ok = true
return
}
func ParseHTTPBytesContentRange(s string) (ret HTTPBytesContentRange, ok bool) {
ss := bytesContentRangeRegexp.FindStringSubmatch(s)
if ss == nil {
return
}
var err error
ret.First, err = strconv.ParseInt(ss[1], 10, 64)
if err != nil {
return
}
ret.Last, err = strconv.ParseInt(ss[2], 10, 64)
if err != nil {
return
}
if ss[3] == "*" {
ret.Length = -1
} else {
ret.Length, err = strconv.ParseInt(ss[3], 10, 64)
if err != nil {
return
}
}
ok = true
return
}