forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
184 lines (162 loc) · 4.25 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
package dotweb
import (
"github.com/devfeel/dotweb/framework/crypto/uuid"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type Request struct {
*http.Request
httpCtx *HttpContext
postBody []byte
realUrl string
isReadBody bool
requestID string
}
//reset response attr
func (req *Request) reset(r *http.Request, ctx *HttpContext) {
req.Request = r
req.isReadBody = false
if ctx.HttpServer().ServerConfig().EnabledRequestID {
req.requestID = uuid.NewV4().String32()
} else {
req.requestID = ""
}
}
func (req *Request) release() {
req.Request = nil
req.isReadBody = false
req.postBody = nil
req.requestID = ""
}
// RequestID get unique ID with current request
// must HttpServer.SetEnabledRequestID(true)
// default is empty string
func (req *Request) RequestID() string {
return req.requestID
}
// QueryStrings 返回Get请求方式下查询字符串map表示
func (req *Request) QueryStrings() url.Values {
return req.URL.Query()
}
// RawQuery 获取原始查询字符串
func (req *Request) RawQuery() string {
return req.URL.RawQuery
}
// QueryString 根据指定key获取在Get请求中对应参数值
func (req *Request) QueryString(key string) string {
return req.URL.Query().Get(key)
}
// 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)
}
//Deprecated: Use the PostFormValue instead
//returns the first value for the named component of the POST
// or PUT request body. URL query parameters are ignored.
func (req *Request) PostString(key string) string {
return req.PostFormValue(key)
}
/*
* 获取post提交的字节数组
*/
func (req *Request) PostBody() []byte {
if !req.isReadBody {
bts, err := ioutil.ReadAll(req.Body)
if err != nil {
return []byte{}
} else {
req.isReadBody = true
req.postBody = bts
}
}
return req.postBody
}
//RemoteAddr to an "IP" address
func (req *Request) RemoteIP() string {
fullIp := req.Request.RemoteAddr
//special: if run in win10, localIp will be like "[::]:port"
//fixed for #20 cann't get RemoteIP and RemoteAddr in win10
lastFlagIndex := strings.LastIndex(fullIp, ":")
if lastFlagIndex >= 0 {
return fullIp[:lastFlagIndex]
}
return fullIp
}
//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 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()
}
}