-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
63 lines (56 loc) · 1.39 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
package gerrittest
import (
"net/http"
"net/url"
"sync"
)
func hostname(u *url.URL) string {
hostname := u.Hostname()
switch u.Hostname() {
case "127.0.0.1", "localhost":
return "localhost"
}
return hostname
}
// CookieJar is an implementation of a cookie jar similar to
// http/cookiejar. This implementation is only intended for
// local development.
type CookieJar struct {
mtx *sync.Mutex
cookies map[string]map[string]*http.Cookie
}
// SetCookies will set cookies for the given url.
func (c *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
c.mtx.Lock()
defer c.mtx.Unlock()
host := hostname(u)
hostCookies, ok := c.cookies[host]
if !ok {
hostCookies = map[string]*http.Cookie{}
c.cookies[host] = hostCookies
}
keys := []string{}
for _, cookie := range cookies {
hostCookies[cookie.Name] = cookie
keys = append(keys, cookie.Name)
}
}
// Cookies returns the cookies associated with the given url.
func (c *CookieJar) Cookies(u *url.URL) (cookies []*http.Cookie) {
c.mtx.Lock()
defer c.mtx.Unlock()
host := hostname(u)
names := []string{}
for _, cookie := range c.cookies[host] {
names = append(names, cookie.Name)
cookies = append(cookies, cookie)
}
return cookies
}
// NewCookieJar constructs and returns a *CookieJar struct.
func NewCookieJar() *CookieJar {
return &CookieJar{
mtx: &sync.Mutex{},
cookies: map[string]map[string]*http.Cookie{},
}
}