forked from venliong/webby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
233 lines (189 loc) · 4.52 KB
/
cookie.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
package webby
import (
"net"
"net/http"
"strings"
"time"
)
func init() {
HtmlFuncBoot.Register(func(w *Web) {
// Get Cookie Value
w.HtmlFunc["cookie"] = func(name string) string {
cookie, err := w.Cookie(name).Get()
if err != nil {
return ""
}
return cookie.Value
}
})
}
// Chainable version of 'net/http.Cookie'
type Cookie struct {
w *Web
c *http.Cookie
}
// New Cookie
func NewCookie(w *Web, name string) Cookie {
return Cookie{
w: w,
c: &http.Cookie{Name: name},
}
}
// Cookie
func (w *Web) Cookie(name string) Cookie {
return NewCookie(w, name)
}
// Set Value
func (c Cookie) Value(value string) Cookie {
c.c.Value = value
return c
}
// Set Path
func (c Cookie) Path(path string) Cookie {
c.c.Path = path
return c
}
// Set Domain
func (c Cookie) Domain(domain string) Cookie {
c.c.Domain = domain
return c
}
// Set Expiry Time of Cookie.
func (c Cookie) Expires(expires time.Time) Cookie {
c.c.Expires = expires
return c
}
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
func (c Cookie) MaxAge(maxage int) Cookie {
c.c.MaxAge = maxage
return c
}
// Make Cookie Secure
func (c Cookie) Secure() Cookie {
c.c.Secure = true
return c
}
// Make Cookie Http Only
func (c Cookie) HttpOnly() Cookie {
c.c.HttpOnly = true
return c
}
// Get *http.Cookie, if Value is not set it will try to get the Cookie from the User Request!
func (c Cookie) Get() (*http.Cookie, error) {
if c.c.Value != "" {
return c.c, nil
}
return c.w.Req.Cookie(c.c.Name)
}
// Delete Cookie
func (c Cookie) Delete() Cookie {
return c.Value("Delete-Me").MaxAge(-1).SaveRes()
}
// Save (Set) Cookie to Response
func (c Cookie) SaveRes() Cookie {
http.SetCookie(c.w, c.pre(c.c))
return c
}
// Prepare Cookie
func (c Cookie) pre(cookie *http.Cookie) *http.Cookie {
var num int
w := c.w
if cookie.Path == "" {
cookie.Path = "/"
}
if cookie.Domain != "" {
goto release_cookie
}
cookie.Domain = w.Req.Host
num = strings.LastIndex(cookie.Domain, "]:")
if num != -1 {
cookie.Domain = cookie.Domain[:num+1]
goto skip_port_check
}
if cookie.Domain[len(cookie.Domain)-1] == ']' {
goto skip_port_check
}
num = strings.LastIndex(cookie.Domain, ":")
if num != -1 {
cookie.Domain = cookie.Domain[:num]
}
skip_port_check:
cookie.Domain = strings.Trim(cookie.Domain, "[]")
if net.ParseIP(cookie.Domain) != nil {
cookie.Domain = ""
goto release_cookie
}
if strings.Count(cookie.Domain, ".") <= 0 {
cookie.Domain = ""
}
release_cookie:
return cookie
}
// Save (Add) Cookie to Request, It won't send anything out to the client.
// But it is a useful feature for CSRF protection for example!.
func (c Cookie) SaveReq() Cookie {
c.w.Req.AddCookie(c.c)
return c
}
// Set to Expire after an hour
func (c Cookie) Hour() Cookie {
return c.Expires(time.Now().Add(1 * time.Hour))
}
// Set to Expire after 6 Hourss
func (c Cookie) SixHours() Cookie {
return c.Expires(time.Now().Add(6 * time.Hour))
}
// Set to Expire after 12 Hourss
func (c Cookie) TwelveHours() Cookie {
return c.Expires(time.Now().Add(12 * time.Hour))
}
// Set to Expire after 1 Day
func (c Cookie) Day() Cookie {
return c.Expires(time.Now().AddDate(0, 0, 1))
}
// Set to Expire after 1 Weeks
func (c Cookie) Week() Cookie {
return c.Expires(time.Now().AddDate(0, 0, 1*7))
}
// Set to Expire after 2 Weeks
func (c Cookie) TwoWeeks() Cookie {
return c.Expires(time.Now().AddDate(0, 0, 2*7))
}
// Set to Expire after 1 Month
func (c Cookie) Month() Cookie {
return c.Expires(time.Now().AddDate(0, 1, 0))
}
// Set to Expire after 3 Months
func (c Cookie) ThreeMonths() Cookie {
return c.Expires(time.Now().AddDate(0, 3, 0))
}
// Set to Expire after 6 Months
func (c Cookie) SixMonths() Cookie {
return c.Expires(time.Now().AddDate(0, 6, 0))
}
// Set to Expire after 9 Months
func (c Cookie) NineMonths() Cookie {
return c.Expires(time.Now().AddDate(0, 9, 0))
}
// Set to Expire after 1 Year
func (c Cookie) Year() Cookie {
return c.Expires(time.Now().AddDate(1, 0, 0))
}
// Set to Expire after 2 Years
func (c Cookie) TwoYears() Cookie {
return c.Expires(time.Now().AddDate(2, 0, 0))
}
// Set to Expire after 3 Years
func (c Cookie) ThreeYears() Cookie {
return c.Expires(time.Now().AddDate(3, 0, 0))
}
// Set to Expire after 4 Years
func (c Cookie) FourYears() Cookie {
return c.Expires(time.Now().AddDate(4, 0, 0))
}
// Set to Expire after 5 Years
func (c Cookie) FiveYears() Cookie {
return c.Expires(time.Now().AddDate(5, 0, 0))
}