/
request.go
114 lines (95 loc) · 2.2 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
package cp
import (
"net/http"
"github.com/creamsensation/cp/internal/constant/cookieName"
"github.com/creamsensation/cp/internal/constant/header"
"github.com/creamsensation/cp/internal/constant/requestVar"
"github.com/creamsensation/cp/internal/requester"
)
type Request interface {
ContentType() string
Form() requester.Form
Header() http.Header
Host() string
Ip() string
Is() requester.Is
Lang() string
Method() string
Path() string
Protocol() string
Query(key string, defaultValue ...string) string
Raw() *http.Request
Route() string
UserAgent() string
Var(key string, defaultValue ...string) string
}
type request struct {
*control
}
func (r request) ContentType() string {
return r.request.Header.Get(header.ContentType)
}
func (r request) Form() requester.Form {
return requester.CreateForm(r.request)
}
func (r request) Header() http.Header {
return r.request.Header
}
func (r request) Host() string {
return r.Protocol() + "://" + r.request.Host
}
func (r request) Ip() string {
ip := r.request.Header.Get(header.Ip)
if len(ip) == 0 {
return "localhost"
}
return ip
}
func (r request) Is() requester.Is {
return requester.CreateIs(r.request, r.core.router.localizedPathMatcher)
}
func (r request) Lang() string {
var lc string
if r.config.Router.Localized {
lc = Var[string](r.control, requestVar.Lang)
}
if len(lc) == 0 {
return r.Cookie().Get(cookieName.Lang)
}
return lc
}
func (r request) Method() string {
return r.request.Method
}
func (r request) Path() string {
return r.request.URL.Path
}
func (r request) Protocol() string {
if r.request.TLS == nil {
return "http"
}
return "https"
}
func (r request) Query(key string, defaultValue ...string) string {
q := Query[string](r.control, key)
if len(q) == 0 && len(defaultValue) > 0 {
return defaultValue[0]
}
return q
}
func (r request) Raw() *http.Request {
return r.request
}
func (r request) Route() string {
return r.control.route.Name
}
func (r request) UserAgent() string {
return r.request.Header.Get(header.UserAgent)
}
func (r request) Var(key string, defaultValue ...string) string {
v := Var[string](r.control, key)
if len(v) == 0 && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}