forked from webview/webview_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie_internal.go
More file actions
102 lines (92 loc) · 2.2 KB
/
cookie_internal.go
File metadata and controls
102 lines (92 loc) · 2.2 KB
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
package webview
import (
"encoding/json"
"errors"
"time"
)
type nativeCookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
Path string `json:"path"`
ExpiresUnix *int64 `json:"expires_unix"`
Secure bool `json:"secure"`
HTTPOnly bool `json:"http_only"`
}
type preparedCookie struct {
Name string
Value string
Domain string
Path string
ExpiresUnix float64
HasExpires bool
Secure bool
HTTPOnly bool
}
type cookieIdentity struct {
Name string
Domain string
Path string
}
func defaultCookiePath(path string) string {
if path == "" {
return "/"
}
return path
}
func decodeCookiesJSON(raw string) ([]Cookie, error) {
items := []nativeCookie{}
if err := json.Unmarshal([]byte(raw), &items); err != nil {
return nil, err
}
cookies := make([]Cookie, 0, len(items))
for _, item := range items {
cookie := Cookie{
Name: item.Name,
Value: item.Value,
Domain: item.Domain,
Path: item.Path,
Secure: item.Secure,
HTTPOnly: item.HTTPOnly,
}
if item.ExpiresUnix != nil {
cookie.Expires = time.Unix(*item.ExpiresUnix, 0)
}
cookies = append(cookies, cookie)
}
return cookies, nil
}
func prepareCookieForNative(cookie Cookie) (preparedCookie, error) {
if cookie.Name == "" {
return preparedCookie{}, errors.New("cookie name is required")
}
if cookie.Domain == "" {
return preparedCookie{}, errors.New("cookie domain is required")
}
prepared := preparedCookie{
Name: cookie.Name,
Value: cookie.Value,
Domain: cookie.Domain,
Path: defaultCookiePath(cookie.Path),
Secure: cookie.Secure,
HTTPOnly: cookie.HTTPOnly,
}
if !cookie.Expires.IsZero() {
prepared.HasExpires = true
prepared.ExpiresUnix = float64(cookie.Expires.Unix())
}
return prepared, nil
}
func prepareCookieIdentity(name string, domain string, path string) (cookieIdentity, error) {
if name == "" {
return cookieIdentity{}, errors.New("cookie name is required")
}
if domain == "" {
return cookieIdentity{}, errors.New("cookie domain is required")
}
return cookieIdentity{
Name: name,
Domain: domain,
Path: defaultCookiePath(path),
}, nil
}