-
Notifications
You must be signed in to change notification settings - Fork 180
/
request.go
222 lines (195 loc) · 5.36 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
package dotweb
import (
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
)
var maxBodySize int64 = 32 << 20 // 32 MB
type Request struct {
*http.Request
httpCtx Context
postBody []byte
realUrl string
isReadBody bool
requestID string
}
// reset response attr
func (req *Request) reset(r *http.Request, ctx Context) {
req.httpCtx = ctx
req.Request = r
req.isReadBody = false
if ctx.HttpServer().ServerConfig().EnabledRequestID {
req.requestID = ctx.HttpServer().DotApp.IDGenerater()
ctx.Response().SetHeader(HeaderRequestID, req.requestID)
} else {
req.requestID = ""
}
}
func (req *Request) release() {
req.Request = nil
req.isReadBody = false
req.postBody = nil
req.requestID = ""
req.realUrl = ""
}
func (req *Request) httpServer() *HttpServer {
return req.httpCtx.HttpServer()
}
func (req *Request) httpApp() *DotWeb {
return req.httpCtx.HttpServer().DotApp
}
// RequestID get unique ID with current request
// must HttpServer.SetEnabledRequestID(true)
// default is empty string
func (req *Request) RequestID() string {
return req.requestID
}
// QueryStrings parses RawQuery and returns the corresponding values.
func (req *Request) QueryStrings() url.Values {
return req.URL.Query()
}
// RawQuery returns the original query string
func (req *Request) RawQuery() string {
return req.URL.RawQuery
}
// QueryString returns the first value associated with the given key.
func (req *Request) QueryString(key string) string {
return req.URL.Query().Get(key)
}
// ExistsQueryKey check is exists from query params with the given key.
func (req *Request) ExistsQueryKey(key string) bool {
_, isExists := req.URL.Query()[key]
return isExists
}
// FormFile get file by form key
func (req *Request) FormFile(key string) (*UploadFile, error) {
file, header, err := req.Request.FormFile(key)
if err != nil {
return nil, err
} else {
return NewUploadFile(file, header), nil
}
}
// FormFiles get multi files
// fixed #92
func (req *Request) FormFiles() (map[string]*UploadFile, error) {
files := make(map[string]*UploadFile)
req.parseForm()
if req.Request.MultipartForm == nil || req.Request.MultipartForm.File == nil {
return nil, http.ErrMissingFile
}
for key, fileMap := range req.Request.MultipartForm.File {
if len(fileMap) > 0 {
file, err := fileMap[0].Open()
if err == nil {
files[key] = NewUploadFile(file, fileMap[0])
}
}
}
return files, nil
}
// FormValues including both the URL field's query parameters and the POST or PUT form data
func (req *Request) FormValues() map[string][]string {
req.parseForm()
return map[string][]string(req.Form)
}
// PostValues contains the parsed form data from POST, PATCH, or PUT body parameters
func (req *Request) PostValues() map[string][]string {
req.parseForm()
return map[string][]string(req.PostForm)
}
func (req *Request) parseForm() error {
if strings.HasPrefix(req.QueryHeader(HeaderContentType), MIMEMultipartForm) {
if err := req.ParseMultipartForm(defaultMemory); err != nil {
return err
}
} else {
if err := req.ParseForm(); err != nil {
return err
}
}
return nil
}
// ContentType get ContentType
func (req *Request) ContentType() string {
return req.Header.Get(HeaderContentType)
}
// QueryHeader query header value by key
func (req *Request) QueryHeader(key string) string {
return req.Header.Get(key)
}
// PostString returns the first value for the named component of the POST
// or PUT request body. URL query parameters are ignored.
// Deprecated: Use the PostFormValue instead
func (req *Request) PostString(key string) string {
return req.PostFormValue(key)
}
// PostBody returns data from the POST or PUT request body
func (req *Request) PostBody() []byte {
if !req.isReadBody {
if req.httpCtx != nil {
switch req.httpCtx.HttpServer().DotApp.Config.Server.MaxBodySize {
case -1:
break
case 0:
req.Body = http.MaxBytesReader(req.httpCtx.Response().Writer(), req.Body, maxBodySize)
break
default:
req.Body = http.MaxBytesReader(req.httpCtx.Response().Writer(), req.Body, req.httpApp().Config.Server.MaxBodySize)
break
}
}
bts, err := ioutil.ReadAll(req.Body)
if err != nil {
//if err, panic it
panic(err)
} else {
req.isReadBody = true
req.postBody = bts
}
}
return req.postBody
}
// RemoteIP RemoteAddr to an "IP" address
func (req *Request) RemoteIP() string {
host, _, _ := net.SplitHostPort(req.RemoteAddr)
return host
}
// RealIP returns the first ip from 'X-Forwarded-For' or 'X-Real-IP' header key
// if not exists data, returns request.RemoteAddr
// fixed for #164
func (req *Request) RealIP() string {
if ip := req.Header.Get(HeaderXForwardedFor); ip != "" {
return strings.Split(ip, ", ")[0]
}
if ip := req.Header.Get(HeaderXRealIP); ip != "" {
return ip
}
host, _, _ := net.SplitHostPort(req.RemoteAddr)
return host
}
// FullRemoteIP RemoteAddr to an "IP:port" address
func (req *Request) FullRemoteIP() string {
fullIp := req.Request.RemoteAddr
return fullIp
}
// Path returns requested path.
//
// The path is valid until returning from RequestHandler.
func (req *Request) Path() string {
return req.URL.Path
}
// IsAJAX returns if it is a ajax request
func (req *Request) IsAJAX() bool {
return strings.Contains(req.Header.Get(HeaderXRequestedWith), "XMLHttpRequest")
}
// Url get request url
func (req *Request) Url() string {
if req.realUrl != "" {
return req.realUrl
} else {
return req.URL.String()
}
}