forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
270 lines (236 loc) Β· 7.09 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package goyave
import (
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/System-Glitch/goyave/v3/cors"
"github.com/System-Glitch/goyave/v3/helper/filesystem"
"github.com/System-Glitch/goyave/v3/validation"
"github.com/google/uuid"
)
// Request struct represents an http request.
// Contains the validated body in the Data attribute if the route was defined with a request generator function
type Request struct {
httpRequest *http.Request
corsOptions *cors.Options
cookies []*http.Cookie
route *Route
User interface{}
Rules *validation.Rules
Data map[string]interface{}
Params map[string]string
Lang string
}
// Request return the raw http request.
// Prefer using the "goyave.Request" accessors.
func (r *Request) Request() *http.Request {
return r.httpRequest
}
// Method specifies the HTTP method (GET, POST, PUT, etc.).
func (r *Request) Method() string {
return r.httpRequest.Method
}
// Protocol the protocol used by this request, "HTTP/1.1" for example.
func (r *Request) Protocol() string {
return r.httpRequest.Proto
}
// URI specifies the URI being requested.
// Use this if you absolutely need the raw query params, url, etc.
// Otherwise use the provided methods and fields of the "goyave.Request".
func (r *Request) URI() *url.URL {
return r.httpRequest.URL
}
// Route returns the current route.
func (r *Request) Route() *Route {
return r.route
}
// Header contains the request header fields either received
// by the server or to be sent by the client.
// Header names are case-insensitive.
//
// If the raw request has the following header lines,
//
// Host: example.com
// accept-encoding: gzip, deflate
// Accept-Language: en-us
// fOO: Bar
// foo: two
//
// then the header map will look like this:
//
// Header = map[string][]string{
// "Accept-Encoding": {"gzip, deflate"},
// "Accept-Language": {"en-us"},
// "Foo": {"Bar", "two"},
// }
func (r *Request) Header() http.Header {
return r.httpRequest.Header
}
// ContentLength records the length of the associated content.
// The value -1 indicates that the length is unknown.
func (r *Request) ContentLength() int64 {
return r.httpRequest.ContentLength
}
// RemoteAddress allows to record the network address that
// sent the request, usually for logging.
func (r *Request) RemoteAddress() string {
return r.httpRequest.RemoteAddr
}
// Cookies returns the HTTP cookies sent with the request.
func (r *Request) Cookies(name string) []*http.Cookie {
if r.cookies == nil {
r.cookies = r.httpRequest.Cookies()
}
return r.cookies
}
// Referrer returns the referring URL, if sent in the request.
func (r *Request) Referrer() string {
return r.httpRequest.Referer()
}
// UserAgent returns the client's User-Agent, if sent in the request.
func (r *Request) UserAgent() string {
return r.httpRequest.UserAgent()
}
// BasicAuth returns the username and password provided in the request's
// Authorization header, if the request uses HTTP Basic Authentication.
func (r *Request) BasicAuth() (username, password string, ok bool) {
return r.httpRequest.BasicAuth()
}
// BearerToken extract the auth token from the "Authorization" header.
// Only takes tokens of type "Bearer".
// Returns empty string if no token found or the header is invalid.
func (r *Request) BearerToken() (string, bool) {
const schema = "Bearer "
header := r.Header().Get("Authorization")
if !strings.HasPrefix(header, schema) {
return "", false
}
return strings.TrimSpace(header[len(schema):]), true
}
// CORSOptions returns the CORS options applied to this request, or nil.
// The returned object is a copy of the options applied to the router.
// Therefore, altering the returned object will not alter the router's options.
func (r *Request) CORSOptions() *cors.Options {
if r.corsOptions == nil {
return nil
}
cpy := *r.corsOptions
return &cpy
}
// Has check if the given field exists in the request data.
func (r *Request) Has(field string) bool {
_, exists := r.Data[field]
return exists
}
// String get a string field from the request data.
// Panics if the field is not a string.
func (r *Request) String(field string) string {
str, ok := r.Data[field].(string)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not a string", field)
}
return str
}
// Numeric get a numeric field from the request data.
// Panics if the field is not numeric.
func (r *Request) Numeric(field string) float64 {
str, ok := r.Data[field].(float64)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not numeric", field)
}
return str
}
// Integer get an integer field from the request data.
// Panics if the field is not an integer.
func (r *Request) Integer(field string) int {
str, ok := r.Data[field].(int)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not an integer", field)
}
return str
}
// Bool get a bool field from the request data.
// Panics if the field is not a bool.
func (r *Request) Bool(field string) bool {
str, ok := r.Data[field].(bool)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not a bool", field)
}
return str
}
// File get a file field from the request data.
// Panics if the field is not numeric.
func (r *Request) File(field string) []filesystem.File {
str, ok := r.Data[field].([]filesystem.File)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not a file", field)
}
return str
}
// Timezone get a timezone field from the request data.
// Panics if the field is not a timezone.
func (r *Request) Timezone(field string) *time.Location {
str, ok := r.Data[field].(*time.Location)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not a timezone", field)
}
return str
}
// IP get an IP field from the request data.
// Panics if the field is not an IP.
func (r *Request) IP(field string) net.IP {
str, ok := r.Data[field].(net.IP)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not an IP", field)
}
return str
}
// URL get a URL field from the request data.
// Panics if the field is not a URL.
func (r *Request) URL(field string) *url.URL {
str, ok := r.Data[field].(*url.URL)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not a URL", field)
}
return str
}
// UUID get a UUID field from the request data.
// Panics if the field is not a UUID.
func (r *Request) UUID(field string) uuid.UUID {
str, ok := r.Data[field].(uuid.UUID)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not an UUID", field)
}
return str
}
// Date get a date field from the request data.
// Panics if the field is not a date.
func (r *Request) Date(field string) time.Time {
str, ok := r.Data[field].(time.Time)
if !ok {
ErrLogger.Panicf("Field \"%s\" is not a date", field)
}
return str
}
// Object get an object field from the request data.
// Panics if the field is not an object.
func (r *Request) Object(field string) map[string]interface{} {
str, ok := r.Data[field].(map[string]interface{})
if !ok {
ErrLogger.Panicf("Field \"%s\" is not an object", field)
}
return str
}
func (r *Request) validate() validation.Errors {
if r.Rules == nil {
return nil
}
contentType := r.httpRequest.Header.Get("Content-Type")
errors := validation.Validate(r.Data, r.Rules, strings.HasPrefix(contentType, "application/json"), r.Lang)
if len(errors) > 0 {
return errors
}
return nil
}