Debian Codegraph @

cache/httpcache/httpcache.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package httpcache
15
16 import (
17 "encoding/json"
18 "time"
19
20 "github.com/gobwas/glob"
21 "github.com/gohugoio/hugo/common/predicate"
22 "github.com/gohugoio/hugo/config"
23 "github.com/mitchellh/mapstructure"
24 )
25
26 // DefaultConfig holds the default configuration for the HTTP cache.
27 var DefaultConfig = Config{
28 RespectCacheControlNoStoreInRequest: true,
29 RespectCacheControlNoStoreInResponse: false,
30 Cache: Cache{
31 For: GlobMatcher{
32 Excludes: []string{"**"},
33 },
34 },
35 Polls: []PollConfig{
36 {
37 For: GlobMatcher{
38 Includes: []string{"**"},
39 },
40 Disable: true,
41 },
42 },
43 }
44
45 // Config holds the configuration for the HTTP cache.
46 type Config struct {
47 // When enabled and there's a Cache-Control: no-store directive in the request, response will never be stored in disk cache.
48 RespectCacheControlNoStoreInRequest bool
49
50 // When enabled and there's a Cache-Control: no-store directive in the response, response will never be stored in disk cache.
51 RespectCacheControlNoStoreInResponse bool
52
53 // Enables HTTP cache behavior (RFC 9111) for these resources.
54 // When this is not enabled for a resource, Hugo will go straight to the file cache.
55 Cache Cache
56
57 // Polls holds a list of configurations for polling remote resources to detect changes in watch mode.
58 // This can be disabled for some resources, typically if they are known to not change.
59 Polls []PollConfig
60 }
61
62 type Cache struct {
63 // Enable HTTP cache behavior (RFC 9111) for these resources.
64 For GlobMatcher
65 }
66
67 func (c *Config) Compile() (ConfigCompiled, error) {
68 cc := ConfigCompiled{
69 Base: *c,
70 }
71
72 p, err := c.Cache.For.CompilePredicate()
73 if err != nil {
74 return cc, err
75 }
76
77 cc.For = p
78
79 for _, pc := range c.Polls {
80
81 p, err := pc.For.CompilePredicate()
82 if err != nil {
83 return cc, err
84 }
85
86 cc.PollConfigs = append(cc.PollConfigs, PollConfigCompiled{
87 For: p,
88 Config: pc,
89 })
90 }
91
92 return cc, nil
93 }
94
95 // PollConfig holds the configuration for polling remote resources to detect changes in watch mode.
96 type PollConfig struct {
97 // What remote resources to apply this configuration to.
98 For GlobMatcher
99
100 // Disable polling for this configuration.
101 Disable bool
102
103 // Low is the lower bound for the polling interval.
104 // This is the starting point when the resource has recently changed,
105 // if that resource stops changing, the polling interval will gradually increase towards High.
106 Low time.Duration
107
108 // High is the upper bound for the polling interval.
109 // This is the interval used when the resource is stable.
110 High time.Duration
111 }
112
113 func (c PollConfig) MarshalJSON() (b []byte, err error) {
114 // Marshal the durations as strings.
115 type Alias PollConfig
116 return json.Marshal(&struct {
117 Low string
118 High string
119 Alias
120 }{
121 Low: c.Low.String(),
122 High: c.High.String(),
123 Alias: (Alias)(c),
124 })
125 }
126
127 type GlobMatcher struct {
128 // Excludes holds a list of glob patterns that will be excluded.
129 Excludes []string
130
131 // Includes holds a list of glob patterns that will be included.
132 Includes []string
133 }
134
135 func (gm GlobMatcher) IsZero() bool {
136 return len(gm.Includes) == 0 && len(gm.Excludes) == 0
137 }
138
139 type ConfigCompiled struct {
140 Base Config
141 For predicate.P[string]
142 PollConfigs []PollConfigCompiled
143 }
144
145 func (c *ConfigCompiled) PollConfigFor(s string) PollConfigCompiled {
146 for _, pc := range c.PollConfigs {
147 if pc.For(s) {
148 return pc
149 }
150 }
151 return PollConfigCompiled{}
152 }
153
154 func (c *ConfigCompiled) IsPollingDisabled() bool {
155 for _, pc := range c.PollConfigs {
156 if !pc.Config.Disable {
157 return false
158 }
159 }
160 return true
161 }
162
163 type PollConfigCompiled struct {
164 For predicate.P[string]
165 Config PollConfig
166 }
167
168 func (p PollConfigCompiled) IsZero() bool {
169 return p.For == nil
170 }
171
172 func (gm *GlobMatcher) CompilePredicate() (func(string) bool, error) {
173 if gm.IsZero() {
174 panic("no includes or excludes")
175 }
176 var b predicate.PR[string]
177 for _, include := range gm.Includes {
178 g, err := glob.Compile(include, '/')
179 if err != nil {
180 return nil, err
181 }
182 fn := func(s string) predicate.Match {
183 return predicate.BoolMatch(g.Match(s))
184 }
185 b = b.Or(fn)
186 }
187
188 for _, exclude := range gm.Excludes {
189 g, err := glob.Compile(exclude, '/')
190 if err != nil {
191 return nil, err
192 }
193 fn := func(s string) predicate.Match {
194 return predicate.BoolMatch(!g.Match(s))
195 }
196 b = b.And(fn)
197 }
198
199 return b.BoolFunc(), nil
200 }
201
202 func DecodeConfig(_ config.BaseConfig, m map[string]any) (Config, error) {
203 if len(m) == 0 {
204 return DefaultConfig, nil
205 }
206
207 var c Config
208
209 dc := &mapstructure.DecoderConfig{
210 Result: &c,
211 DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
212 WeaklyTypedInput: true,
213 }
214
215 decoder, err := mapstructure.NewDecoder(dc)
216 if err != nil {
217 return c, err
218 }
219
220 if err := decoder.Decode(m); err != nil {
221 return c, err
222 }
223
224 if c.Cache.For.IsZero() {
225 c.Cache.For = DefaultConfig.Cache.For
226 }
227
228 for pci := range c.Polls {
229 if c.Polls[pci].For.IsZero() {
230 c.Polls[pci].For = DefaultConfig.Cache.For
231 c.Polls[pci].Disable = true
232 }
233 }
234
235 if len(c.Polls) == 0 {
236 c.Polls = DefaultConfig.Polls
237 }
238
239 return c, nil
240 }