forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
151 lines (132 loc) · 2.85 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
package dotweb
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type Request struct {
*http.Request
postBody []byte
isReadBody bool
}
//reset response attr
func (req *Request) reset(r *http.Request) {
req.Request = r
req.isReadBody = false
}
func (req *Request) release() {
req.Request = nil
req.isReadBody = false
req.postBody = nil
}
/*
* 返回查询字符串map表示
*/
func (req *Request) QueryStrings() url.Values {
return req.URL.Query()
}
/*
* 获取原始查询字符串
*/
func (req *Request) RawQuery() string {
return req.URL.RawQuery
}
/*
* 根据指定key获取对应value
*/
func (req *Request) QueryString(key string) string {
return req.URL.Query().Get(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
}
}
/*
* 获取包括post、put和get内的值
*/
func (req *Request) FormValues() map[string][]string {
req.parseForm()
return map[string][]string(req.Form)
}
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
}
func (req *Request) QueryHeader(key string) string {
return req.Header.Get(key)
}
/*
* 根据指定key获取包括在post、put内的值
*/
func (req *Request) PostFormValue(key string) string {
return req.PostFormValue(key)
}
/*
* 根据指定key获取包括在post、put内的值
* Obsolete("use PostFormValue replace this")
*/
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
s := strings.Split(fullIp, ":")
if len(s) > 1 {
return s[0]
} else {
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"
}
// Host returns requested host.
//
// The host is valid until returning from RequestHandler.
func (ctx *HttpContext) Host() string {
return ctx.Request.Host
}
func (ctx *HttpContext) Method() string {
return ctx.Request.Method
}