-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemorystorage.go
More file actions
186 lines (148 loc) · 3.75 KB
/
memorystorage.go
File metadata and controls
186 lines (148 loc) · 3.75 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cookiejarx
import (
"sort"
"sync"
"time"
)
type inMemoryEntry struct {
*Entry
// seqNum is a sequence number so that Cookies returns cookies in a
// deterministic order, even for cookies that have equal Path length and
// equal Creation time. This simplifies testing.
seqNum uint64
}
// InMemoryStorage provides thread-safe in-memory entry storage with predictable entry sorting
type InMemoryStorage struct {
// mu locks the remaining fields.
mu sync.Mutex
// entries is a set of entries, keyed by their eTLD+1 and subkeyed by
// their name/domain/path.
entries map[string]map[string]inMemoryEntry
// nextSeqNum is the next sequence number assigned to a new cookie
// created SetCookies.
nextSeqNum uint64
}
// NewInMemoryStorage returns new InMemoryStorage instance
func NewInMemoryStorage() *InMemoryStorage {
return &InMemoryStorage{
entries: make(map[string]map[string]inMemoryEntry),
}
}
// EntriesDump returns all entries persisted in in-memory storage
func (s *InMemoryStorage) EntriesDump() (entries []*Entry) {
s.mu.Lock()
defer s.mu.Unlock()
for _, submap := range s.entries {
for _, e := range submap {
entries = append(entries, e.Entry)
}
}
return entries
}
// EntriesRestore adds provide entries to current in-memory storage
func (s *InMemoryStorage) EntriesRestore(entries []*Entry) {
s.mu.Lock()
defer s.mu.Unlock()
for _, e := range entries {
s.saveEntry(e)
}
}
// EntriesClear empties current in-memory storage
func (s *InMemoryStorage) EntriesClear() {
s.mu.Lock()
defer s.mu.Unlock()
s.entries = make(map[string]map[string]inMemoryEntry)
}
// SaveEntry in-memory implementation of Storage.SaveEntry
func (s *InMemoryStorage) SaveEntry(entry *Entry) {
s.mu.Lock()
defer s.mu.Unlock()
s.saveEntry(entry)
}
func (s *InMemoryStorage) saveEntry(entry *Entry) {
submap := s.entries[entry.Key]
if submap == nil {
submap = make(map[string]inMemoryEntry)
}
e := inMemoryEntry{
Entry: entry,
}
id := entry.ID
if old, ok := submap[id]; ok {
e.Creation = old.Creation
e.seqNum = old.seqNum
} else {
e.seqNum = s.nextSeqNum
s.nextSeqNum++
}
submap[id] = e
s.entries[entry.Key] = submap
}
// RemoveEntry in-memory implementation of Storage.RemoveEntry
func (s *InMemoryStorage) RemoveEntry(key, id string) {
s.mu.Lock()
defer s.mu.Unlock()
submap := s.entries[key]
var modified bool
if submap != nil {
if _, ok := submap[id]; ok {
delete(submap, id)
modified = true
}
}
if modified && len(submap) == 0 {
delete(s.entries, key)
}
}
// Entries in-memory implementation of Storage.Entries
func (s *InMemoryStorage) Entries(https bool, host, path, key string, now time.Time) (entries []*Entry) {
s.mu.Lock()
defer s.mu.Unlock()
submap := s.entries[key]
if submap == nil {
return entries
}
modified := false
var selected []inMemoryEntry
for id, e := range submap {
if e.Persistent && !e.Expires.After(now) {
delete(submap, id)
modified = true
continue
}
if !e.ShouldSend(https, host, path) {
continue
}
e.LastAccess = now
submap[id] = e
selected = append(selected, e)
modified = true
}
if modified {
if len(submap) == 0 {
delete(s.entries, key)
} else {
s.entries[key] = submap
}
}
// sort according to RFC 6265 section 5.4 point 2: by longest
// path and then by earliest creation time.
sort.Slice(selected, func(i, j int) bool {
sel := selected
if len(sel[i].Path) != len(sel[j].Path) {
return len(sel[i].Path) > len(sel[j].Path)
}
if !sel[i].Creation.Equal(sel[j].Creation) {
return sel[i].Creation.Before(sel[j].Creation)
}
return sel[i].seqNum < sel[j].seqNum
})
if len(selected) == 0 {
return entries
}
entries = make([]*Entry, len(selected))
for i, sel := range selected {
entries[i] = sel.Entry
}
return entries
}