This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
239 lines (191 loc) · 4.25 KB
/
reader.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
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
package h1
import (
"io"
"sync"
)
type RequestReader struct {
R io.Reader
ReadBuffer []byte
NextBuffer []byte
Request Request
}
func (r *RequestReader) Reset() {
r.ReadBuffer = r.ReadBuffer[:cap(r.ReadBuffer)]
r.NextBuffer = r.ReadBuffer[:0]
r.Request.Reset()
}
func (r *RequestReader) Fill() (n int, err error) {
// Copy the remaining bytes to the read buffer
n0 := copy(r.ReadBuffer[:cap(r.ReadBuffer)], r.NextBuffer)
// Read more bytes
n1, err := r.R.Read(r.ReadBuffer[n0:cap(r.ReadBuffer)])
if err != nil {
return 0, err
}
// Set the next buffer to the read buffer
r.NextBuffer = r.ReadBuffer[:n0+n1]
return n1, nil
}
func (r *RequestReader) Next() (remaining int, err error) {
var retryCount int = 0
if r.Remaining() == 0 {
n, err := r.R.Read(r.ReadBuffer[:cap(r.ReadBuffer)])
if err != nil {
return 0, err
}
r.NextBuffer = r.ReadBuffer[:n]
}
parse:
// Reset the request
r.Request.Reset()
// Read request line
r.NextBuffer, err = ParseRequestLine(&r.Request, r.NextBuffer)
if err != nil {
if err == ErrBufferTooSmall {
// Buffer is too small, read more bytes
_, err = r.Fill()
if err != nil {
return 0, err
}
if len(r.NextBuffer) == cap(r.ReadBuffer) {
return 0, ErrRequestHeaderTooLarge
}
// Retry parsing
retryCount++
if retryCount > 1 {
return 0, ErrBufferTooSmall
}
goto parse
}
}
// Read headers
r.NextBuffer, err = ParseHeaders(&r.Request, r.NextBuffer)
if err != nil {
if err == ErrBufferTooSmall {
// Buffer is too small, read more bytes
_, err = r.Fill()
if err != nil {
return 0, err
}
if len(r.NextBuffer) == cap(r.ReadBuffer) {
return 0, ErrRequestHeaderTooLarge
}
// Retry parsing
retryCount++
if retryCount > 1 {
return 0, ErrBufferTooSmall
}
goto parse
}
}
// Parse URI
r.Request.URI.Parse(r.Request.RawURI)
return len(r.NextBuffer), nil
}
func (r *RequestReader) Remaining() int {
return len(r.NextBuffer)
}
func (r *RequestReader) Body() *BodyReader {
br := GetBodyReader()
br.Limit = int(r.Request.ContentLength)
br.Upstream = r
return br
}
type BodyReader struct {
Upstream *RequestReader
Limit int
Index int
}
func (r *BodyReader) reset() {
r.Upstream = nil
r.Limit = 0
r.Index = 0
}
var BodyReaderPool = &sync.Pool{
New: func() any {
return &BodyReader{}
},
}
func GetBodyReader() *BodyReader {
return BodyReaderPool.Get().(*BodyReader)
}
func PutBodyReader(r *BodyReader) {
r.reset()
BodyReaderPool.Put(r)
}
func (r *BodyReader) Read(p []byte) (n int, err error) {
if r.Index >= r.Limit {
return 0, io.EOF
}
// If p is bigger than the remaining bytes, set the limit to the remaining bytes
if len(p) > r.Limit-r.Index {
p = p[:r.Limit-r.Index]
}
// Copy the remaining bytes to the read buffer
n0 := copy(p, r.Upstream.NextBuffer)
r.Upstream.NextBuffer = r.Upstream.NextBuffer[n0:]
r.Index += n0
if len(p) == n0 {
// No more bytes to read
return n0, nil
}
// Fill The buffer
_, err = r.Upstream.Fill()
if err != nil {
return n0, err
}
// If p is bigger than Uptream.ReadBuffer then read directly from the upstream
if len(p)-n0 > len(r.Upstream.ReadBuffer) {
n1, err := r.Upstream.R.Read(p[n0:])
if err != nil {
return n0 + n1, err
}
return n0 + n1, nil
}
// Read more bytes
n1, err := r.Read(p[n0:])
if err != nil {
return n0 + n1, err
}
return n0 + n1, nil
}
func (r *BodyReader) Close() error {
PutBodyReader(r)
return nil
}
type HijackReader struct {
Upstream *RequestReader
}
func (h HijackReader) Read(p []byte) (n int, err error) {
// Copy the remaining bytes to the read buffer
n0 := copy(p, h.Upstream.NextBuffer)
h.Upstream.NextBuffer = h.Upstream.NextBuffer[n0:]
if len(p) == n0 {
// No more bytes to read
return n0, nil
}
// Fill The buffer
_, err = h.Upstream.Fill()
if err != nil {
return n0, err
}
// If p is bigger than Uptream.ReadBuffer then read directly from the upstream
if len(p)-n0 > len(h.Upstream.ReadBuffer) {
n1, err := h.Upstream.R.Read(p[n0:])
if err != nil {
return n0 + n1, err
}
return n0 + n1, nil
}
// Read more bytes
n1, err := h.Read(p[n0:])
if err != nil {
return n0 + n1, err
}
return n0 + n1, nil
}
func (r *RequestReader) Hijack() HijackReader {
return HijackReader{
Upstream: r,
}
}