-
Notifications
You must be signed in to change notification settings - Fork 1
/
responsereader.go
207 lines (179 loc) · 4.76 KB
/
responsereader.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
package main
import (
"bytes"
"fmt"
"strconv"
)
type reseponseReaderState int
// States for the response reader state machine.
const (
stateReadResponseLine reseponseReaderState = iota
stateReadNextHeaderLine
stateReadBodyWithContentLength
stateReadBodyChunkedLengthLine
stateReadBodyChunkedBytes
stateDone
)
type ResponseReader struct {
ResponseCode int
BodyBytesRead int
state reseponseReaderState
carry []byte // Header-bytes carried over from previous call to read, in case the previous header ended abruptly.
contentLength int
transferEncodingChunked bool
curChunkLength int
curChunkBytesRead int
}
var headerKeyTransferEncoding = []byte("Transfer-Encoding")
var headerValChunked = []byte("chunked")
var headerKeyContentLength = []byte("Content-Length")
const maxCarrySizeBytes = 1024 * 50
func (r *ResponseReader) Read(input []byte) (done bool, err error) {
bb := input
if len(r.carry) > 0 {
bb = r.carry
bb = append(bb, input...)
r.carry = nil
}
for {
switch r.state {
case stateReadResponseLine:
n := bytes.IndexByte(bb, '\n')
if n == -1 {
if len(bb) > maxCarrySizeBytes {
err = fmt.Errorf("response line spanning multiple packets too long")
return
}
r.carry = make([]byte, len(bb))
copy(r.carry, bb)
return
}
responseLine := bb[:n]
bb = bb[n+1:]
// Skip past HTTP version.
n = bytes.IndexByte(responseLine, ' ')
if n == -1 {
err = fmt.Errorf("invalid respose line")
return
}
responseLineRest := responseLine[n+1:]
// Get response code.
n = bytes.IndexByte(responseLineRest, ' ')
if n == -1 {
n = len(responseLineRest)
}
r.ResponseCode, err = strconv.Atoi(string(responseLineRest[:n]))
if err != nil {
err = fmt.Errorf("no response code in respose line")
return
}
r.state = stateReadNextHeaderLine
case stateReadNextHeaderLine:
n := bytes.IndexByte(bb, '\n')
if n == -1 {
if len(bb) > maxCarrySizeBytes {
err = fmt.Errorf("response header spanning multiple packets too long")
return
}
r.carry = make([]byte, len(bb))
copy(r.carry, bb)
return
}
headerLine := bb[:n]
headerLine = bytes.TrimSuffix(headerLine, []byte{'\r'})
bb = bb[n+1:]
if len(headerLine) == 0 {
// Empty header line means end of headers.
r.state = stateReadBodyWithContentLength
if r.transferEncodingChunked {
r.state = stateReadBodyChunkedLengthLine
}
continue
}
// Get header name.
n = bytes.IndexByte(headerLine, ':')
if n == -1 {
err = fmt.Errorf("invalid header")
return
}
headerName := bytes.TrimSpace(headerLine[:n])
// Interpret the headers that are of importance to us
if bytes.EqualFold(headerName, headerKeyContentLength) {
headerVal := string(bytes.ToLower(bytes.TrimSpace(headerLine[n+1:])))
r.contentLength, err = strconv.Atoi(headerVal)
if err != nil {
err = fmt.Errorf("invalid content-length header")
return
}
} else if bytes.EqualFold(headerName, headerKeyTransferEncoding) {
headerVal := bytes.ToLower(bytes.TrimSpace(headerLine[n+1:]))
r.transferEncodingChunked = bytes.EqualFold(headerVal, headerValChunked)
}
case stateReadBodyWithContentLength:
remaining := r.contentLength - r.BodyBytesRead
n := len(bb)
if n > remaining {
n = remaining
}
r.BodyBytesRead += n
if r.BodyBytesRead == r.contentLength {
r.state = stateDone
done = true
}
return
case stateReadBodyChunkedLengthLine:
var chunkLengthLine []byte
for {
n := bytes.IndexByte(bb, '\n')
if n == -1 {
if len(bb) > 20 {
err = fmt.Errorf("chunk length line too long")
return
}
r.carry = make([]byte, len(bb))
copy(r.carry, bb)
return
}
chunkLengthLine = bb[:n]
bb = bb[n+1:]
if len(bytes.TrimSpace(chunkLengthLine)) == 0 {
continue // This looping is to consume line breaks after the chunk bytes right before a chunk length.
}
break
}
var l int64
l, err = strconv.ParseInt(string(bytes.TrimSpace(chunkLengthLine)), 16, 64)
if err != nil {
err = fmt.Errorf("invalid chunk length")
return
}
r.curChunkLength = int(l)
r.curChunkBytesRead = 0
if r.curChunkLength == 0 {
r.state = stateDone
done = true
return
}
r.state = stateReadBodyChunkedBytes
case stateReadBodyChunkedBytes:
remaining := r.curChunkLength - r.curChunkBytesRead
n := len(bb)
if n > remaining {
n = remaining
}
r.curChunkBytesRead += n
r.BodyBytesRead += n
bb = bb[n:]
if r.curChunkBytesRead == r.curChunkLength {
r.state = stateReadBodyChunkedLengthLine
if len(bb) > 0 {
continue
}
}
return
case stateDone:
done = true
return
}
}
}