-
Notifications
You must be signed in to change notification settings - Fork 11
/
request.go
202 lines (166 loc) · 3.95 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
package standard
import (
"context"
"io"
"mime/multipart"
"net/http"
"sync"
"github.com/webx-top/echo"
"github.com/webx-top/echo/engine"
)
type Request struct {
config *engine.Config
requestMu sync.RWMutex
request *http.Request
url *URL
header *Header
value *Value
realIP string
maxSize int
}
func NewRequest(r *http.Request) *Request {
req := &Request{
request: r,
url: &URL{url: r.URL},
header: &Header{header: r.Header},
}
req.value = NewValue(req)
return req
}
func (r *Request) Context() context.Context {
return r.request.Context()
}
func (r *Request) WithContext(ctx context.Context) *http.Request {
return r.request.WithContext(ctx)
}
func (r *Request) SetValue(key string, value interface{}) {
r.requestMu.Lock()
*r.request = *r.WithContext(context.WithValue(r.request.Context(), 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 r.request.Host
}
func (r *Request) URL() engine.URL {
return r.url
}
func (r *Request) Header() engine.Header {
return r.header
}
func (r *Request) Proto() string {
return r.request.Proto
}
func (r *Request) RemoteAddress() string {
return r.request.RemoteAddr
}
func (r *Request) Method() string {
return r.request.Method
}
func (r *Request) SetMethod(method string) {
r.request.Method = method
}
func (r *Request) URI() string {
return r.request.RequestURI
}
// SetURI implements `engine.Request#SetURI` function.
func (r *Request) SetURI(uri string) {
r.request.RequestURI = uri
}
func (r *Request) Body() io.ReadCloser {
return r.request.Body
}
// SetBody implements `engine.Request#SetBody` function.
func (r *Request) SetBody(reader io.Reader) {
if readCloser, ok := reader.(io.ReadCloser); ok {
r.request.Body = readCloser
} else {
r.request.Body = io.NopCloser(reader)
}
}
func (r *Request) FormValue(name string) string {
return r.value.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) {
if r.request.MultipartForm != nil {
return r.request.MultipartForm, nil
}
err := r.request.ParseMultipartForm(int64(r.MaxSize()))
return r.request.MultipartForm, err
}
func (r *Request) IsTLS() bool {
return r.request.TLS != nil
}
func (r *Request) Cookie(key string) string {
if cookie, err := r.request.Cookie(key); err == nil {
return cookie.Value
}
return ``
}
func (r *Request) Referer() string {
return r.request.Referer()
}
func (r *Request) UserAgent() string {
return r.request.UserAgent()
}
func (r *Request) Object() interface{} {
return r.request
}
func (r *Request) reset(req *http.Request, h *Header, u *URL) {
r.requestMu = sync.RWMutex{}
r.request = req
r.header = h
r.url = u
r.value = NewValue(r)
r.realIP = ``
r.maxSize = 0
}
func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
r.MultipartForm()
file, fileHeader, err := r.request.FormFile(key)
if err != nil {
return nil, nil, err
}
return file, fileHeader, err
}
// Size implements `engine.Request#ContentLength` function.
func (r *Request) Size() int64 {
return r.request.ContentLength
}
func (r *Request) Scheme() string {
if r.IsTLS() {
return echo.SchemeHTTPS
}
if len(r.request.URL.Scheme) > 0 {
return r.request.URL.Scheme
}
return echo.SchemeHTTP
}
func (r *Request) BasicAuth() (username, password string, ok bool) {
return r.request.BasicAuth()
}
// SetHost implements `engine.Request#SetHost` function.
func (r *Request) SetHost(host string) {
r.request.Host = host
}
func (r *Request) StdRequest() *http.Request {
return r.request
}