-
Notifications
You must be signed in to change notification settings - Fork 12
/
storage.go
77 lines (66 loc) · 1.37 KB
/
storage.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
package hpkp
import (
"strings"
"sync"
)
// MemStorage is threadsafe hpkp host storage backed by an in-memory map
type MemStorage struct {
domains map[string]Header
mutex sync.Mutex
}
// NewMemStorage initializes hpkp in-memory datastructure
func NewMemStorage() *MemStorage {
m := &MemStorage{}
m.domains = make(map[string]Header)
return m
}
// Lookup returns the corresponding hpkp header information for a given host
func (s *MemStorage) Lookup(host string) *Header {
s.mutex.Lock()
defer s.mutex.Unlock()
d, ok := s.domains[host]
if ok {
return copy(d)
}
// is h a subdomain of an hpkp domain, walk the domain to see if it is a sub
// sub ... sub domain of a domain that has the `includeSubDomains` rule
l := len(host)
for l > 0 {
i := strings.Index(host, ".")
if i > 0 {
host = host[i+1:]
d, ok := s.domains[host]
if ok {
if d.IncludeSubDomains {
return copy(d)
}
}
l = len(host)
} else {
break
}
}
return nil
}
func copy(h Header) *Header {
d := h
return &d
}
// Add a domain to hpkp storage
func (s *MemStorage) Add(host string, d *Header) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.domains == nil {
s.domains = make(map[string]Header)
}
if d.MaxAge == 0 && !d.Permanent {
check, ok := s.domains[host]
if ok {
if !check.Permanent {
delete(s.domains, host)
}
}
} else {
s.domains[host] = *d
}
}