-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
191 lines (174 loc) · 3.62 KB
/
request.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
// Copyright (c) 2020 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package request implements an HTTP request reader.
package request
import (
"bufio"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"unsafe"
)
const (
contentLength = "Content-Length"
host = "Host"
)
var bodyPool = sync.Pool{
New: func() interface{} {
return &body{}
},
}
func freeBody(b *body) {
*b = body{}
bodyPool.Put(b)
}
var cancelPool = sync.Pool{
New: func() interface{} {
return make(<-chan struct{}, 1)
},
}
func freeCancel(c <-chan struct{}) {
select {
case <-c:
default:
}
cancelPool.Put(c)
}
var headerPool = sync.Pool{
New: func() interface{} {
return make(http.Header)
},
}
func freeHeader(h http.Header) {
if h == nil {
return
}
for key := range h {
h.Del(key)
}
headerPool.Put(h)
}
var requestPool = sync.Pool{
New: func() interface{} {
return &http.Request{}
},
}
// FreeRequest frees the request.
func FreeRequest(r *http.Request) {
if r == nil {
return
}
freeHeader(r.Header)
if body, ok := r.Body.(*body); ok {
freeBody(body)
}
freeCancel(r.Cancel)
*r = http.Request{}
requestPool.Put(r)
}
// ReadRequest reads and parses an incoming request from b.
//
// ReadRequest is a low-level function and should only be used for
// specialized applications; most code should use the Server to read
// requests and handle them via the Handler interface. ReadRequest
// only supports HTTP/1.x requests.
func ReadRequest(b *bufio.Reader) (*http.Request, error) {
return http.ReadRequest(b)
}
// ReadFastRequest is like ReadRequest but with the simple request parser.
func ReadFastRequest(b *bufio.Reader) (*http.Request, error) {
statusLineData, err := readLine(b)
if err != nil {
return nil, err
}
statusLine := *(*string)(unsafe.Pointer(&statusLineData))
strs := strings.Split(statusLine, " ")
if len(strs) != 3 {
return nil, errors.New("status line error ")
}
req := requestPool.Get().(*http.Request)
req.Header = headerPool.Get().(http.Header)
req.Method = strs[0]
URL, err := url.Parse(strs[1])
if err != nil {
return nil, err
}
req.URL = URL
req.Proto = strs[2]
for {
lineData, err := readLine(b)
if err != nil {
return nil, err
}
line := *(*string)(unsafe.Pointer(&lineData))
if len(line) > 0 {
strs := strings.Split(line, " ")
key := strs[0][:len(strs[0])-1]
req.Header[key] = strs[1:]
} else {
break
}
}
if v, ok := req.Header[host]; ok {
req.Host = v[0]
}
if v, ok := req.Header[contentLength]; ok {
ContentLength, _ := strconv.ParseInt(v[0], 0, 64)
req.ContentLength = ContentLength
}
body := bodyPool.Get().(*body)
body.i = 0
body.l = req.ContentLength
body.b = b
req.Body = body
req.Cancel = cancelPool.Get().(<-chan struct{})
return req, nil
}
type body struct {
b *bufio.Reader
i int64 // current reading index
l int64 // content length
}
// Read implements the io.Reader interface.
func (b *body) Read(p []byte) (n int, err error) {
if b.i >= b.l {
return 0, io.EOF
}
if b.i+int64(len(p)) <= b.l {
n, err = b.b.Read(p)
b.i += int64(n)
return
}
n, err = b.b.Read(p[:b.l-b.i])
b.i += int64(n)
return
}
// Read implements the io.Closer interface.
func (b *body) Close() error {
if b.l <= 0 {
return nil
}
if b.i >= b.l {
return nil
}
io.CopyN(ioutil.Discard, b, b.l-b.i)
return nil
}
func readLine(b *bufio.Reader) ([]byte, error) {
line, isPrefix, err := b.ReadLine()
if !isPrefix {
return line, err
}
for {
fragment, isPrefix, err := b.ReadLine()
line = append(line, fragment...)
if !isPrefix {
return line, err
}
}
}