/
cookie.go
92 lines (82 loc) · 1.93 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
// Cookies Package helps reading and setting the cookie
// The cookie struct's instance is availaible to both
// goexpress.Request and goexpress.Response
package cookie
import(
"net/http"
Time "time"
)
// An interface to set the cookie in response
type Response interface {
AddCookie(str string, value string)
}
// Cookie struct
type Cookie struct{
response Response
request *http.Request
cookies map[string]*http.Cookie
init bool
readonly bool
}
// Initialise a Cookie struct for use of Request Struct
func (c *Cookie) InitReadOnly(request *http.Request) *Cookie{
c.request = request
c.addCookiesToMap()
return c
}
func (c *Cookie) addCookiesToMap() *Cookie{
c.cookies = make(map[string]*http.Cookie)
var cookies = c.request.Cookies()
var length = len(cookies)
for i := 0; i < length; i++ {
c.cookies[cookies[i].Name] = cookies[i]
}
return c
}
// Initialise the Cookie struct with goexpress.Response and http request
func (c *Cookie) Init(response Response, request *http.Request) *Cookie{
if c.init {
return c
}
c.cookies = make(map[string]*http.Cookie)
c.response = response
c.request = request
return c
}
// Adds a cookie
func (c *Cookie) Add(cookie *http.Cookie) *Cookie{
if c.readonly {
return c
}
c.cookies[cookie.Name] = cookie
return c
}
// Deletes a cookie
func (c *Cookie) Del(name string) *Cookie{
var cookie = &http.Cookie{Name: name, Expires: Time.Unix(0, 0)}
c.cookies[name] = cookie
return c
}
// Returns a cookie
func (c *Cookie) Get(name string) string{
cookie, found := c.cookies[name]
if found == false {
return ""
}
return cookie.Value
}
// Returns the map of all the cookies
func (c *Cookie) GetAll() map[string]*http.Cookie {
return c.cookies
}
// An internal function to set all the cookies before pushing response body
func (c *Cookie) Finish(){
if c.readonly {
return
}
for _, cookie := range c.cookies {
if v := cookie.String(); v != "" {
c.response.AddCookie("Set-Cookie", v)
}
}
}