-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttp.go
More file actions
321 lines (263 loc) · 5.66 KB
/
Copy pathhttp.go
File metadata and controls
321 lines (263 loc) · 5.66 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package wildcat
import (
"bytes"
"io"
"strconv"
"github.com/vektra/errors"
)
const OptimalBufferSize = 1500
type header struct {
Name []byte
Value []byte
}
type HTTPParser struct {
Method, Path, Version []byte
Headers []header
TotalHeaders int
host []byte
hostRead bool
contentLength int64
contentLengthRead bool
}
const DefaultHeaderSlice = 10
// Create a new parser
func NewHTTPParser() *HTTPParser {
return NewSizedHTTPParser(DefaultHeaderSlice)
}
// Create a new parser allocating size for size headers
func NewSizedHTTPParser(size int) *HTTPParser {
return &HTTPParser{
Headers: make([]header, size),
TotalHeaders: size,
contentLength: -1,
}
}
var (
ErrBadProto = errors.New("bad protocol")
ErrMissingData = errors.New("missing data")
ErrUnsupported = errors.New("unsupported http feature")
)
const (
eNextHeader int = iota
eNextHeaderN
eHeader
eHeaderValueSpace
eHeaderValue
eHeaderValueN
eMLHeaderStart
eMLHeaderValue
)
// Parse the buffer as an HTTP Request. The buffer must contain the entire
// request or Parse will return ErrMissingData for the caller to get more
// data. (this thusly favors getting a completed request in a single Read()
// call).
//
// Returns the number of bytes used by the header (thus where the body begins).
// Also can return ErrUnsupported if an HTTP feature is detected but not supported.
func (hp *HTTPParser) Parse(input []byte) (int, error) {
var headers int
var path int
var ok bool
total := len(input)
method:
for i := 0; i < total; i++ {
switch input[i] {
case ' ', '\t':
hp.Method = input[0:i]
ok = true
path = i + 1
break method
}
}
if !ok {
return 0, ErrMissingData
}
var version int
ok = false
path:
for i := path; i < total; i++ {
switch input[i] {
case ' ', '\t':
ok = true
hp.Path = input[path:i]
version = i + 1
break path
}
}
if !ok {
return 0, ErrMissingData
}
var readN bool
ok = false
loop:
for i := version; i < total; i++ {
c := input[i]
switch readN {
case false:
switch c {
case '\r':
hp.Version = input[version:i]
readN = true
case '\n':
hp.Version = input[version:i]
headers = i + 1
ok = true
break loop
}
case true:
if c != '\n' {
return 0, errors.Context(ErrBadProto, "missing newline in version")
}
headers = i + 1
ok = true
break loop
}
}
if !ok {
return 0, ErrMissingData
}
var h int
var headerName []byte
state := eNextHeader
start := headers
for i := headers; i < total; i++ {
switch state {
case eNextHeader:
switch input[i] {
case '\r':
state = eNextHeaderN
case '\n':
return i + 1, nil
case ' ', '\t':
state = eMLHeaderStart
default:
start = i
state = eHeader
}
case eNextHeaderN:
if input[i] != '\n' {
return 0, ErrBadProto
}
return i + 1, nil
case eHeader:
if input[i] == ':' {
headerName = input[start:i]
state = eHeaderValueSpace
}
case eHeaderValueSpace:
switch input[i] {
case ' ', '\t':
continue
}
start = i
state = eHeaderValue
case eHeaderValue:
switch input[i] {
case '\r':
state = eHeaderValueN
case '\n':
state = eNextHeader
default:
continue
}
hp.Headers[h] = header{headerName, input[start:i]}
h++
if h == hp.TotalHeaders {
newHeaders := make([]header, hp.TotalHeaders+10)
copy(newHeaders, hp.Headers)
hp.Headers = newHeaders
hp.TotalHeaders += 10
}
case eHeaderValueN:
if input[i] != '\n' {
return 0, ErrBadProto
}
state = eNextHeader
case eMLHeaderStart:
switch input[i] {
case ' ', '\t':
continue
}
start = i
state = eMLHeaderValue
case eMLHeaderValue:
switch input[i] {
case '\r':
state = eHeaderValueN
case '\n':
state = eNextHeader
default:
continue
}
cur := hp.Headers[h-1].Value
newheader := make([]byte, len(cur)+1+(i-start))
copy(newheader, cur)
copy(newheader[len(cur):], []byte(" "))
copy(newheader[len(cur)+1:], input[start:i])
hp.Headers[h-1].Value = newheader
}
}
return 0, ErrMissingData
}
// Return a value of a header matching name.
func (hp *HTTPParser) FindHeader(name []byte) []byte {
for _, header := range hp.Headers {
if bytes.Equal(header.Name, name) {
return header.Value
}
}
for _, header := range hp.Headers {
if bytes.EqualFold(header.Name, name) {
return header.Value
}
}
return nil
}
// Return all values of a header matching name.
func (hp *HTTPParser) FindAllHeaders(name []byte) [][]byte {
var headers [][]byte
for _, header := range hp.Headers {
if bytes.EqualFold(header.Name, name) {
headers = append(headers, header.Value)
}
}
return headers
}
var cHost = []byte("Host")
// Return the value of the Host header
func (hp *HTTPParser) Host() []byte {
if hp.hostRead {
return hp.host
}
hp.hostRead = true
hp.host = hp.FindHeader(cHost)
return hp.host
}
var cContentLength = []byte("Content-Length")
// Return the value of the Content-Length header.
// A value of -1 indicates the header was not set.
func (hp *HTTPParser) ContentLength() int64 {
if hp.contentLengthRead {
return hp.contentLength
}
header := hp.FindHeader(cContentLength)
if header != nil {
i, err := strconv.ParseInt(string(header), 10, 0)
if err == nil {
hp.contentLength = i
}
}
hp.contentLengthRead = true
return hp.contentLength
}
func (hp *HTTPParser) BodyReader(rest []byte, in io.ReadCloser) io.ReadCloser {
return BodyReader(hp.ContentLength(), rest, in)
}
var cGet = []byte("GET")
func (hp *HTTPParser) Get() bool {
return bytes.Equal(hp.Method, cGet)
}
var cPost = []byte("POST")
func (hp *HTTPParser) Post() bool {
return bytes.Equal(hp.Method, cPost)
}