-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
109 lines (91 loc) · 2.17 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
package water
import (
"html/template"
"net/http"
"strconv"
)
func (ctx *Context) Cookie(name string) string {
cookie, err := ctx.Request.Cookie(name)
if err != nil {
return ""
}
return cookie.Value
}
func (ctx *Context) CookieEscape(name string) string {
return template.HTMLEscapeString(ctx.Cookie(name))
}
func (ctx *Context) CookieBool(name string) bool {
v, _ := strconv.ParseBool(ctx.Cookie(name))
return v
}
func (ctx *Context) CookieInt(name string) int {
v, _ := strconv.Atoi(ctx.Cookie(name))
return v
}
func (ctx *Context) CookieInt64(name string) int64 {
v, _ := strconv.ParseInt(ctx.Cookie(name), 10, 64)
return v
}
func (ctx *Context) CookieUint(name string) uint {
v, _ := strconv.ParseUint(ctx.Cookie(name), 10, 64)
return uint(v)
}
func (ctx *Context) CookieUint64(name string) uint64 {
v, _ := strconv.ParseUint(ctx.Cookie(name), 10, 64)
return v
}
func (ctx *Context) CookieFloat64(name string) float64 {
v, _ := strconv.ParseFloat(ctx.Cookie(name), 64)
return v
}
// name/value escape by url.QueryEscape() before SetCookie() if necessary
// others... : MaxAge, Path, Domain, Secure, HttpOnly, SameSite.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
cookie := http.Cookie{}
cookie.Name = name
cookie.Value = value
if len(others) > 0 {
switch v := others[0].(type) {
case int:
cookie.MaxAge = v
case int32:
cookie.MaxAge = int(v)
case int64:
cookie.MaxAge = int(v)
}
}
cookie.Path = "/"
if len(others) > 1 {
if v, ok := others[1].(string); ok && len(v) > 0 {
cookie.Path = v
}
}
if len(others) > 2 {
if v, ok := others[2].(string); ok && len(v) > 0 {
cookie.Domain = v
}
}
if len(others) > 3 {
switch v := others[3].(type) {
case bool:
cookie.Secure = v
default:
if others[3] != nil {
cookie.Secure = true
}
}
}
if len(others) > 4 {
if v, ok := others[4].(bool); ok && v {
cookie.HttpOnly = true
}
}
if len(others) > 5 {
if v, ok := others[5].(http.SameSite); ok {
cookie.SameSite = v
}
}
ctx.ResponseWriter.Header().Add("Set-Cookie", cookie.String())
}