-
Notifications
You must be signed in to change notification settings - Fork 11
/
request.go
268 lines (229 loc) · 5.78 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
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
//go:build !appengine
// +build !appengine
package fasthttp
import (
"bytes"
"context"
"encoding/base64"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"sync"
"github.com/admpub/fasthttp"
"github.com/webx-top/echo"
"github.com/webx-top/echo/engine"
)
type Request struct {
config *engine.Config
requestMu sync.RWMutex
context *fasthttp.RequestCtx
url engine.URL
header engine.Header
value *Value
realIP string
stdRequest *http.Request
maxSize int
}
func NewRequest(c *fasthttp.RequestCtx) *Request {
req := &Request{
context: c,
url: &URL{url: c.URI()},
header: &RequestHeader{
header: &c.Request.Header,
stdhdr: nil,
},
}
req.value = NewValue(req)
return req
}
func (r *Request) Context() context.Context {
return r.context
}
func (r *Request) WithContext(ctx context.Context) *http.Request {
return r.StdRequest().WithContext(ctx)
}
func (r *Request) SetValue(key string, value interface{}) {
r.requestMu.Lock()
r.context.SetUserValue(key, value)
r.requestMu.Unlock()
}
func (r *Request) SetMaxSize(maxSize int) {
r.maxSize = maxSize
}
func (r *Request) MaxSize() int {
if r.maxSize <= 0 {
maxMemory := engine.DefaultMaxRequestBodySize
if r.config != nil && r.config.MaxRequestBodySize != 0 {
maxMemory = r.config.MaxRequestBodySize
}
return maxMemory
}
return r.maxSize
}
func (r *Request) Host() string {
return engine.Bytes2str(r.context.Host())
}
func (r *Request) URI() string {
return engine.Bytes2str(r.context.RequestURI())
}
// SetURI implements `engine.Request#SetURI` function.
func (r *Request) SetURI(uri string) {
r.context.Request.Header.SetRequestURI(uri)
}
func (r *Request) URL() engine.URL {
return r.url
}
func (r *Request) Header() engine.Header {
return r.header
}
func (r *Request) Proto() string {
return "HTTP/1.1"
}
func (r *Request) RemoteAddress() string {
return r.context.RemoteAddr().String()
}
func (r *Request) Method() string {
return engine.Bytes2str(r.context.Method())
}
func (r *Request) SetMethod(method string) {
r.context.Request.Header.SetMethod(method)
}
func (r *Request) Body() io.ReadCloser {
return io.NopCloser(bytes.NewBuffer(r.context.PostBody()))
}
// SetBody implements `engine.Request#SetBody` function.
func (r *Request) SetBody(reader io.Reader) {
r.context.Request.SetBodyStream(reader, 0)
}
func (r *Request) FormValue(name string) string {
//return string(r.context.FormValue(name))
return r.Form().Get(name)
}
func (r *Request) Form() engine.URLValuer {
return r.value
}
func (r *Request) PostForm() engine.URLValuer {
return r.value.postArgs
}
func (r *Request) MultipartForm() (*multipart.Form, error) {
return r.context.MultipartForm()
}
func (r *Request) IsTLS() bool {
return r.context.IsTLS()
}
func (r *Request) Cookie(key string) string {
return engine.Bytes2str(r.context.Request.Header.Cookie(key))
}
func (r *Request) Referer() string {
return engine.Bytes2str(r.context.Referer())
}
func (r *Request) UserAgent() string {
return engine.Bytes2str(r.context.UserAgent())
}
func (r *Request) Object() interface{} {
return r.context
}
func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
fileHeader, err := r.context.FormFile(key)
if err != nil {
return nil, nil, err
}
var file multipart.File
file, err = fileHeader.Open()
return file, fileHeader, err
}
func (r *Request) Scheme() string {
if r.IsTLS() {
return echo.SchemeHTTPS
}
scheme := engine.Bytes2str(r.context.URI().Scheme())
if len(scheme) > 0 {
return scheme
}
return echo.SchemeHTTP
}
// Size implements `engine.Request#ContentLength` function.
func (r *Request) Size() int64 {
return int64(r.context.Request.Header.ContentLength())
}
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
r.config = nil
r.requestMu = sync.RWMutex{}
r.context = c
r.header = h
r.url = u
r.value = NewValue(r)
r.realIP = ``
r.stdRequest = nil
r.maxSize = 0
}
// BasicAuth returns the username and password provided in the request's
// Authorization header, if the request uses HTTP Basic Authentication.
// See RFC 2617, Section 2.
func (r *Request) BasicAuth() (username, password string, ok bool) {
auth := r.Header().Get(echo.HeaderAuthorization)
if auth == "" {
return
}
return parseBasicAuth(auth)
}
// SetHost implements `engine.Request#SetHost` function.
func (r *Request) SetHost(host string) {
r.context.Request.SetHost(host)
}
func (r *Request) StdRequest() *http.Request {
if r.stdRequest != nil {
return r.stdRequest
}
var req http.Request
ctx := r.context
req.Method = r.Method()
req.Proto = "HTTP/1.1"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.RequestURI = r.URI()
req.ContentLength = r.Size()
req.Host = r.Host()
req.RemoteAddr = r.RemoteAddress()
req.TLS = ctx.TLSConnectionState()
hdr := make(http.Header)
ctx.Request.Header.VisitAll(func(k, v []byte) {
sk := engine.Bytes2str(k)
sv := engine.Bytes2str(v)
switch sk {
case echo.HeaderTransferEncoding:
req.TransferEncoding = append(req.TransferEncoding, sv)
default:
hdr.Set(sk, sv)
}
})
req.Header = hdr
req.Body = r.Body()
rURL, err := url.ParseRequestURI(req.RequestURI)
if err != nil {
ctx.Logger().Printf("cannot parse requestURI %q: %s", req.RequestURI, err)
}
req.URL = rURL
r.stdRequest = req.WithContext(r.context)
return r.stdRequest
}
// parseBasicAuth parses an HTTP Basic Authentication string.
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
func parseBasicAuth(auth string) (username, password string, ok bool) {
const prefix = "Basic "
if !strings.HasPrefix(auth, prefix) {
return
}
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}
return cs[:s], cs[s+1:], true
}