-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaltsvcutil.go
213 lines (194 loc) · 3.93 KB
/
altsvcutil.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package altsvcutil
import (
"bytes"
"fmt"
"github.com/imroc/req/v3/internal/netutil"
"github.com/imroc/req/v3/pkg/altsvc"
"io"
"net"
"net/url"
"strconv"
"strings"
"time"
)
type altAvcParser struct {
*bytes.Buffer
}
// validOptionalPort reports whether port is either an empty string
// or matches /^:\d*$/
func validOptionalPort(port string) bool {
if port == "" {
return true
}
if port[0] != ':' {
return false
}
for _, b := range port[1:] {
if b < '0' || b > '9' {
return false
}
}
return true
}
// splitHostPort separates host and port. If the port is not valid, it returns
// the entire input as host, and it doesn't check the validity of the host.
// Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
func splitHostPort(hostPort string) (host, port string) {
host = hostPort
colon := strings.LastIndexByte(host, ':')
if colon != -1 && validOptionalPort(host[colon:]) {
host, port = host[:colon], host[colon+1:]
}
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
host = host[1 : len(host)-1]
}
return
}
// ParseHeader parses the AltSvc from header value.
func ParseHeader(value string) ([]*altsvc.AltSvc, error) {
p := newAltSvcParser(value)
return p.Parse()
}
func newAltSvcParser(value string) *altAvcParser {
buf := bytes.NewBufferString(value)
return &altAvcParser{buf}
}
var endOfTime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
func (p *altAvcParser) Parse() (as []*altsvc.AltSvc, err error) {
for {
a, e := p.parseOne()
if a != nil {
as = append(as, a)
}
if e != nil {
if e == io.EOF {
return
} else {
err = e
return
}
}
}
return
}
func (p *altAvcParser) parseKv() (key, value string, haveNextField bool, err error) {
line, err := p.ReadBytes('=')
if len(line) == 0 {
return
}
key = strings.TrimSpace(string(line[:len(line)-1]))
bs := p.Bytes()
if len(bs) == 0 {
err = io.EOF
return
}
if bs[0] == '"' {
quoteIndex := 0
for i := 1; i < len(bs); i++ {
if bs[i] == '"' {
quoteIndex = i
break
}
}
if quoteIndex == 0 {
err = fmt.Errorf("quote in alt-svc is not complete: %s", bs)
return
}
value = string(bs[1:quoteIndex])
p.Next(quoteIndex + 1)
if len(bs) == quoteIndex+1 {
err = io.EOF
return
}
var b byte
b, err = p.ReadByte()
if err != nil {
return
}
if b == ';' {
haveNextField = true
}
} else {
delimIndex := 0
LOOP:
for i, v := range bs {
switch v {
case ',':
delimIndex = i
break LOOP
case ';':
delimIndex = i
haveNextField = true
break LOOP
}
}
if delimIndex == 0 {
err = io.EOF
value = strings.TrimSpace(string(bs))
return
}
p.Next(delimIndex + 1)
value = string(bs[:delimIndex])
}
return
}
func (p *altAvcParser) parseOne() (as *altsvc.AltSvc, err error) {
proto, addr, haveNextField, err := p.parseKv()
if proto == "" || addr == "" {
return
}
host, port := splitHostPort(addr)
as = &altsvc.AltSvc{
Protocol: proto,
Host: host,
Port: port,
Expire: endOfTime,
}
if !haveNextField {
return
}
key, ma, haveNextField, err := p.parseKv()
if key == "" || ma == "" {
return
}
if key != "ma" {
err = fmt.Errorf("expect ma field, got %s", key)
return
}
maInt, err := strconv.ParseInt(ma, 10, 64)
if err != nil {
return
}
as.Expire = time.Now().Add(time.Duration(maInt) * time.Second)
if !haveNextField {
return
}
// drain useless fields
for {
_, _, haveNextField, err = p.parseKv()
if haveNextField {
continue
} else {
break
}
}
return
}
// ConvertURL converts the raw request url to expected alt-svc's url.
func ConvertURL(a *altsvc.AltSvc, u *url.URL) *url.URL {
host, port := netutil.AuthorityHostPort(u.Scheme, u.Host)
uu := *u
modify := false
if a.Host != "" && a.Host != host {
host = a.Host
modify = true
}
if a.Port != "" && a.Port != port {
port = a.Port
modify = true
}
if modify {
uu.Host = net.JoinHostPort(host, port)
}
return &uu
}