-
Notifications
You must be signed in to change notification settings - Fork 5
/
context_cookie.go
49 lines (43 loc) · 1.04 KB
/
context_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
package xingyun
import (
"net/http"
"time"
)
func (ctx *Context) SetCookie(name string, value interface{}) {
ctx.SetExpireCookie(name, value, 0)
}
func (ctx *Context) SetExpireCookie(name string, value interface{}, sec int64) {
ctx.checkHeaderWrite()
cookier := ctx.Server.SecureCookie
encoded, err := cookier.Encode(name, value)
if err != nil {
ctx.Logger.Errorf(err.Error())
return
}
cookie := &http.Cookie{
Name: name,
Value: encoded,
Path: "/",
}
if sec != 0 {
cookie.Expires = time.Unix(time.Now().Unix()+sec, 0)
}
http.SetCookie(ctx.ResponseWriter, cookie)
}
func (ctx *Context) GetCookie(name string, value interface{}) error {
cookier := ctx.Server.SecureCookie
r := ctx.Request
cookie, err := r.Cookie(name)
if err != nil {
return err
}
return cookier.Decode(name, cookie.Value, value)
}
func (ctx *Context) GetStringCookie(name string) (value string, err error) {
var s string
err = ctx.GetCookie(name, &s)
return s, err
}
func (ctx *Context) RemoveCookie(name string) {
ctx.SetExpireCookie(name, "", -1)
}