forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
179 lines (150 loc) · 4.37 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
// +build !appengine
package fasthttp
import (
"bytes"
"io"
"mime/multipart"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/log"
"github.com/valyala/fasthttp"
)
type (
// Request implements `engine.Request`.
Request struct {
*fasthttp.RequestCtx
header engine.Header
url engine.URL
logger log.Logger
}
)
// NewRequest returns `Request` instance.
func NewRequest(c *fasthttp.RequestCtx, l log.Logger) *Request {
return &Request{
RequestCtx: c,
url: &URL{URI: c.URI()},
header: &RequestHeader{RequestHeader: &c.Request.Header},
logger: l,
}
}
// IsTLS implements `engine.Request#TLS` function.
func (r *Request) IsTLS() bool {
return r.RequestCtx.IsTLS()
}
// Scheme implements `engine.Request#Scheme` function.
func (r *Request) Scheme() string {
return string(r.RequestCtx.URI().Scheme())
}
// Host implements `engine.Request#Host` function.
func (r *Request) Host() string {
return string(r.RequestCtx.Host())
}
// URL implements `engine.Request#URL` function.
func (r *Request) URL() engine.URL {
return r.url
}
// Header implements `engine.Request#Header` function.
func (r *Request) Header() engine.Header {
return r.header
}
// Referer implements `engine.Request#Referer` function.
func (r *Request) Referer() string {
return string(r.Request.Header.Referer())
}
// ContentLength implements `engine.Request#ContentLength` function.
func (r *Request) ContentLength() int64 {
return int64(r.Request.Header.ContentLength())
}
// UserAgent implements `engine.Request#UserAgent` function.
func (r *Request) UserAgent() string {
return string(r.RequestCtx.UserAgent())
}
// RemoteAddress implements `engine.Request#RemoteAddress` function.
func (r *Request) RemoteAddress() string {
return r.RemoteAddr().String()
}
// Method implements `engine.Request#Method` function.
func (r *Request) Method() string {
return string(r.RequestCtx.Method())
}
// SetMethod implements `engine.Request#SetMethod` function.
func (r *Request) SetMethod(method string) {
r.Request.Header.SetMethodBytes([]byte(method))
}
// URI implements `engine.Request#URI` function.
func (r *Request) URI() string {
return string(r.RequestURI())
}
// SetURI implements `engine.Request#SetURI` function.
func (r *Request) SetURI(uri string) {
r.Request.Header.SetRequestURI(uri)
}
// Body implements `engine.Request#Body` function.
func (r *Request) Body() io.Reader {
return bytes.NewBuffer(r.Request.Body())
}
// SetBody implements `engine.Request#SetBody` function.
func (r *Request) SetBody(reader io.Reader) {
r.Request.SetBodyStream(reader, 0)
}
// FormValue implements `engine.Request#FormValue` function.
func (r *Request) FormValue(name string) string {
return string(r.RequestCtx.FormValue(name))
}
// FormParams implements `engine.Request#FormParams` function.
func (r *Request) FormParams() (params map[string][]string) {
params = make(map[string][]string)
mf, err := r.RequestCtx.MultipartForm()
if err == fasthttp.ErrNoMultipartForm {
r.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 `engine.Request#FormFile` function.
func (r *Request) FormFile(name string) (*multipart.FileHeader, error) {
return r.RequestCtx.FormFile(name)
}
// MultipartForm implements `engine.Request#MultipartForm` function.
func (r *Request) MultipartForm() (*multipart.Form, error) {
return r.RequestCtx.MultipartForm()
}
// Cookie implements `engine.Request#Cookie` function.
func (r *Request) Cookie(name string) (engine.Cookie, error) {
c := new(fasthttp.Cookie)
b := r.Request.Header.Cookie(name)
if b == nil {
return nil, echo.ErrCookieNotFound
}
c.ParseBytes(b)
c.SetKey(name)
return &Cookie{c}, nil
}
// Cookies implements `engine.Request#Cookies` function.
func (r *Request) Cookies() []engine.Cookie {
cookies := make([]engine.Cookie, 0)
r.Request.Header.VisitAllCookie(func(name, value []byte) {
c := new(fasthttp.Cookie)
c.SetKey(string(name))
c.ParseBytes(value)
cookies = append(cookies, &Cookie{c})
})
return cookies
}
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
r.RequestCtx = c
r.header = h
r.url = u
}