-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
72 lines (62 loc) · 1.28 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
package hsts
import (
"strings"
"sync"
)
// MemStorage is threadsafe hsts host storage backed by an in-memory map
type MemStorage struct {
domains map[string]Domain
mutex sync.Mutex
}
// NewMemStorage initializes hsts in-memory datastructure
func NewMemStorage() *MemStorage {
m := &MemStorage{}
m.domains = make(map[string]Domain)
return m
}
// Contains whether storage has host
func (hs *MemStorage) Contains(h string) bool {
hs.mutex.Lock()
defer hs.mutex.Unlock()
d, ok := hs.domains[h]
if ok {
// exact match!
return d.Check(h)
}
// is h a subdomain of an hsts domain, walk the domain to see if it is a sub
// sub ... sub domain of a domain that has the `includeSubDomains` rule
l := len(h)
originalHost := h
for l > 0 {
i := strings.Index(h, ".")
if i > 0 {
h = h[i+1:]
d, ok := hs.domains[h]
if ok {
return d.Check(originalHost)
}
l = len(h)
} else {
break
}
}
return false
}
// Add a domain to hsts storage
func (hs *MemStorage) Add(d *Domain) {
hs.mutex.Lock()
defer hs.mutex.Unlock()
if hs.domains == nil {
hs.domains = make(map[string]Domain)
}
if d.MaxAge == 0 && !d.Permanent {
check, ok := hs.domains[d.Host]
if ok {
if !check.Permanent {
delete(hs.domains, d.Host)
}
}
} else {
hs.domains[d.Host] = *d
}
}