-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
157 lines (132 loc) · 3.82 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
package macross
import (
"bytes"
"io"
"mime/multipart"
"net"
"github.com/valyala/fasthttp"
)
// IsTLS implements `Context#TLS` function.
func (c *Context) IsTLS() bool {
return c.RequestCtx.IsTLS()
}
// Scheme implements `Context#Scheme` function.
func (c *Context) Scheme() string {
return string(c.RequestCtx.URI().Scheme())
}
// Host implements `Context#Host` function.
func (c *Context) Host() string {
return string(c.RequestCtx.Host())
}
// SetHost implements `Context#SetHost` function.
func (c *Context) SetHost(host string) {
c.RequestCtx.Request.SetHost(host)
}
// Referer implements `Context#Referer` function.
func (c *Context) Referer() string {
return string(c.Request.Header.Referer())
}
// ContentLength implements `Context#ContentLength` function.
func (c *Context) ContentLength() int64 {
return int64(c.Request.Header.ContentLength())
}
// UserAgent implements `Context#UserAgent` function.
func (c *Context) UserAgent() string {
return string(c.RequestCtx.UserAgent())
}
// RemoteAddress implements `Context#RemoteAddress` function.
func (c *Context) RemoteAddress() string {
return c.RemoteAddr().String()
}
// RealIP implements `Context#RealIP` function.
func (c *Context) RealIP() string {
ra := c.RemoteAddress()
if ip := c.Request.Header.Peek(HeaderXForwardedFor); ip != nil {
ra = string(ip)
} else if ip := c.Request.Header.Peek(HeaderXRealIP); ip != nil {
ra = string(ip)
} else {
ra, _, _ = net.SplitHostPort(ra)
}
return ra
}
// Method implements `Context#Method` function.
func (c *Context) Method() string {
return string(c.RequestCtx.Method())
}
// SetMethod implements `Context#SetMethod` function.
func (c *Context) SetMethod(method string) {
c.Request.Header.SetMethodBytes([]byte(method))
}
// URI implements `Context#URI` function.
func (c *Context) GetURI() string {
return string(c.RequestURI())
}
// SetURI implements `Context#SetURI` function.
func (c *Context) SetURI(uri string) {
c.Request.Header.SetRequestURI(uri)
}
// Body implements `Context#Body` function.
func (c *Context) Body() io.Reader {
return bytes.NewBuffer(c.Request.Body())
}
// SetBody implements `Context#SetBody` function.
func (c *Context) SetBody(reader io.Reader) {
c.Request.SetBodyStream(reader, 0)
}
// FormValue implements `Context#FormValue` function.
func (c *Context) FormValue(name string) string {
return string(c.RequestCtx.FormValue(name))
}
// FormParams implements `Context#FormParams` function.
func (c *Context) FormParams() (params map[string][]string) {
params = make(map[string][]string)
mf, err := c.RequestCtx.MultipartForm()
if err == fasthttp.ErrNoMultipartForm {
c.PostArgs().VisitAll(func(k, v []byte) {
key := string(k)
if _, ok := params[key]; ok {
params[key] = append(params[key], string(v))
} else {
params[string(k)] = []string{string(v)}
}
})
} else if err == nil {
for k, v := range mf.Value {
if len(v) > 0 {
params[k] = v
}
}
}
return
}
// FormFile implements `Context#FormFile` function.
func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {
return c.RequestCtx.FormFile(name)
}
// MultipartForm implements `Context#MultipartForm` function.
func (c *Context) MultipartForm() (*multipart.Form, error) {
return c.RequestCtx.MultipartForm()
}
// Cookie implements `Context#Cookie` function.
func (ctx *Context) Cookie(name string) (*Cookie, error) {
c := fasthttp.Cookie{}
b := ctx.Request.Header.Cookie(name)
if b == nil {
return nil, ErrCookieNotFound
}
c.SetKey(name)
c.SetValueBytes(b)
return &Cookie{c}, nil
}
// Cookies implements `Context#Cookies` function.
func (ctx *Context) Cookies() []*Cookie {
cookies := []*Cookie{}
ctx.Request.Header.VisitAllCookie(func(name, value []byte) {
c := fasthttp.Cookie{}
c.SetKeyBytes(name)
c.SetValueBytes(value)
cookies = append(cookies, &Cookie{c})
})
return cookies
}