-
Notifications
You must be signed in to change notification settings - Fork 7
/
request.go
390 lines (307 loc) · 8.46 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package gin
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"strings"
"sync"
"github.com/bytedance/sonic"
"github.com/gin-gonic/gin"
"github.com/gookit/validate"
"github.com/spf13/cast"
filesystemcontract "github.com/goravel/framework/contracts/filesystem"
httpcontract "github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/log"
validatecontract "github.com/goravel/framework/contracts/validation"
"github.com/goravel/framework/filesystem"
"github.com/goravel/framework/validation"
)
type Request struct {
ctx *Context
instance *gin.Context
postData map[string]any
log log.Log
validation validatecontract.Validation
}
func NewRequest(ctx *Context, log log.Log, validation validatecontract.Validation) httpcontract.Request {
postData, err := getPostData(ctx)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))
}
return &Request{ctx: ctx, instance: ctx.instance, postData: postData, log: log, validation: validation}
}
func (r *Request) AbortWithStatus(code int) {
r.instance.AbortWithStatus(code)
}
func (r *Request) AbortWithStatusJson(code int, jsonObj any) {
r.instance.AbortWithStatusJSON(code, jsonObj)
}
func (r *Request) All() map[string]any {
var (
dataMap = make(map[string]any)
queryMap = make(map[string]any)
)
for key, query := range r.instance.Request.URL.Query() {
queryMap[key] = strings.Join(query, ",")
}
var mu sync.RWMutex
for k, v := range queryMap {
mu.Lock()
dataMap[k] = v
mu.Unlock()
}
for k, v := range r.postData {
mu.Lock()
dataMap[k] = v
mu.Unlock()
}
return dataMap
}
func (r *Request) Bind(obj any) error {
return r.instance.ShouldBind(obj)
}
func (r *Request) Form(key string, defaultValue ...string) string {
if len(defaultValue) == 0 {
return r.instance.PostForm(key)
}
return r.instance.DefaultPostForm(key, defaultValue[0])
}
func (r *Request) File(name string) (filesystemcontract.File, error) {
file, err := r.instance.FormFile(name)
if err != nil {
return nil, err
}
return filesystem.NewFileFromRequest(file)
}
func (r *Request) FullUrl() string {
prefix := "https://"
if r.instance.Request.TLS == nil {
prefix = "http://"
}
if r.instance.Request.Host == "" {
return ""
}
return prefix + r.instance.Request.Host + r.instance.Request.RequestURI
}
func (r *Request) Header(key string, defaultValue ...string) string {
header := r.instance.GetHeader(key)
if header != "" {
return header
}
if len(defaultValue) == 0 {
return ""
}
return defaultValue[0]
}
func (r *Request) Headers() http.Header {
return r.instance.Request.Header
}
func (r *Request) Host() string {
return r.instance.Request.Host
}
func (r *Request) Json(key string, defaultValue ...string) string {
var data map[string]any
if err := r.Bind(&data); err != nil {
if len(defaultValue) == 0 {
return ""
} else {
return defaultValue[0]
}
}
if value, exist := data[key]; exist {
return cast.ToString(value)
}
if len(defaultValue) == 0 {
return ""
}
return defaultValue[0]
}
func (r *Request) Method() string {
return r.instance.Request.Method
}
func (r *Request) Next() {
r.instance.Next()
}
func (r *Request) Query(key string, defaultValue ...string) string {
if len(defaultValue) > 0 {
return r.instance.DefaultQuery(key, defaultValue[0])
}
return r.instance.Query(key)
}
func (r *Request) QueryInt(key string, defaultValue ...int) int {
if val, ok := r.instance.GetQuery(key); ok {
return cast.ToInt(val)
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
func (r *Request) QueryInt64(key string, defaultValue ...int64) int64 {
if val, ok := r.instance.GetQuery(key); ok {
return cast.ToInt64(val)
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return 0
}
func (r *Request) QueryBool(key string, defaultValue ...bool) bool {
if value, ok := r.instance.GetQuery(key); ok {
return stringToBool(value)
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return false
}
func (r *Request) QueryArray(key string) []string {
return r.instance.QueryArray(key)
}
func (r *Request) QueryMap(key string) map[string]string {
return r.instance.QueryMap(key)
}
func (r *Request) Queries() map[string]string {
queries := make(map[string]string)
for key, query := range r.instance.Request.URL.Query() {
queries[key] = strings.Join(query, ",")
}
return queries
}
func (r *Request) Origin() *http.Request {
return r.instance.Request
}
func (r *Request) Path() string {
return r.instance.Request.URL.Path
}
func (r *Request) Input(key string, defaultValue ...string) string {
if value, exist := r.postData[key]; exist {
return cast.ToString(value)
}
if value, exist := r.instance.GetQuery(key); exist {
return value
}
value := r.instance.Param(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
return value
}
func (r *Request) InputInt(key string, defaultValue ...int) int {
value := r.Input(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
return cast.ToInt(value)
}
func (r *Request) InputInt64(key string, defaultValue ...int64) int64 {
value := r.Input(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
return cast.ToInt64(value)
}
func (r *Request) InputBool(key string, defaultValue ...bool) bool {
value := r.Input(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
return stringToBool(value)
}
func (r *Request) Ip() string {
return r.instance.ClientIP()
}
func (r *Request) Route(key string) string {
return r.instance.Param(key)
}
func (r *Request) RouteInt(key string) int {
val := r.instance.Param(key)
return cast.ToInt(val)
}
func (r *Request) RouteInt64(key string) int64 {
val := r.instance.Param(key)
return cast.ToInt64(val)
}
func (r *Request) Url() string {
return r.instance.Request.RequestURI
}
func (r *Request) Validate(rules map[string]string, options ...validatecontract.Option) (validatecontract.Validator, error) {
if len(rules) == 0 {
return nil, errors.New("rules can't be empty")
}
options = append(options, validation.Rules(rules), validation.CustomRules(r.validation.Rules()))
generateOptions := validation.GenerateOptions(options)
var v *validate.Validation
dataFace, err := validate.FromRequest(r.Origin())
if err != nil {
return nil, err
}
if dataFace == nil {
v = validate.NewValidation(dataFace)
} else {
if generateOptions["prepareForValidation"] != nil {
if err := generateOptions["prepareForValidation"].(func(ctx httpcontract.Context, data validatecontract.Data) error)(r.ctx, validation.NewData(dataFace)); err != nil {
return nil, err
}
}
v = dataFace.Create()
}
validation.AppendOptions(v, generateOptions)
return validation.NewValidator(v, dataFace), nil
}
func (r *Request) ValidateRequest(request httpcontract.FormRequest) (validatecontract.Errors, error) {
if err := request.Authorize(r.ctx); err != nil {
return nil, err
}
validator, err := r.Validate(request.Rules(r.ctx), validation.Messages(request.Messages(r.ctx)), validation.Attributes(request.Attributes(r.ctx)), func(options map[string]any) {
options["prepareForValidation"] = request.PrepareForValidation
})
if err != nil {
return nil, err
}
if err := validator.Bind(request); err != nil {
return nil, err
}
return validator.Errors(), nil
}
func getPostData(ctx *Context) (map[string]any, error) {
request := ctx.instance.Request
if request == nil || request.Body == nil || request.ContentLength == 0 {
return nil, nil
}
contentType := ctx.instance.ContentType()
data := make(map[string]any)
if contentType == "application/json" {
bodyBytes, err := io.ReadAll(request.Body)
_ = request.Body.Close()
if err != nil {
return nil, fmt.Errorf("retrieve json error: %v", err)
}
if err := sonic.Unmarshal(bodyBytes, &data); err != nil {
return nil, fmt.Errorf("decode json [%v] error: %v", string(bodyBytes), err)
}
request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
if contentType == "multipart/form-data" {
if request.PostForm == nil {
const defaultMemory = 32 << 20
if err := request.ParseMultipartForm(defaultMemory); err != nil {
return nil, fmt.Errorf("parse multipart form error: %v", err)
}
}
for k, v := range request.PostForm {
data[k] = strings.Join(v, ",")
}
for k, v := range request.MultipartForm.File {
if len(v) > 0 {
data[k] = v[0]
}
}
}
return data, nil
}
func stringToBool(value string) bool {
return value == "1" || value == "true" || value == "on" || value == "yes"
}