-
Notifications
You must be signed in to change notification settings - Fork 15
/
cookie.go
93 lines (82 loc) · 2.24 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
// Package goexpress helps reading and setting the cookie
// The cookie struct's instance is availaible to both
// goexpress.Request and goexpress.Response
package goexpress
import (
"net/http"
Time "time"
)
// responseCookieInterface is an interface to set the cookie in response
type responseCookieInterface interface {
// addCookie adds a cookie, we only need this method from the response
addCookie(str string, value string)
}
// Cookie struct defines cookies associated with request/response
type cookie struct {
response responseCookieInterface
request *http.Request
cookies map[string]*http.Cookie
init bool
readonly bool
}
// newReadOnlyCookie initialises a Cookie struct for use of Request Struct
func newReadOnlyCookie(request *http.Request) *cookie {
c := &cookie{}
c.request = request
c.addCookiesToMap()
return c
}
// newCookie initialises the Cookie struct with goexpress.Response and http request
func newCookie(response responseCookieInterface, request *http.Request) *cookie {
c := &cookie{}
c.cookies = make(map[string]*http.Cookie)
c.response = response
c.request = request
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
}
// Add adds a cookie
func (c *cookie) Add(cookie *http.Cookie) Cookie {
if c.readonly {
return c
}
c.cookies[cookie.Name] = cookie
return c
}
// Del 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
}
// Get returns a cookie
func (c *cookie) Get(name string) string {
cookie, found := c.cookies[name]
if found == false {
return ""
}
return cookie.Value
}
// GetAll returns the map of all the cookies
func (c *cookie) GetAll() map[string]*http.Cookie {
return c.cookies
}
// Finish is 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)
}
}
}