]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix potential nilpointer in httpcache config
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 24 Feb 2025 16:37:20 +0000 (17:37 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 24 Feb 2025 17:42:50 +0000 (18:42 +0100)
cache/httpcache/httpcache.go
cache/httpcache/httpcache_integration_test.go

index 98f7fedd4c5f7b0e7d869bf10290e8b49122742c..7fa76e5c3c3c00e7a402618d8b55d1e6559e5c57 100644 (file)
@@ -122,6 +122,10 @@ type GlobMatcher struct {
        Includes []string
 }
 
+func (gm GlobMatcher) IsZero() bool {
+       return len(gm.Includes) == 0 && len(gm.Excludes) == 0
+}
+
 type ConfigCompiled struct {
        For         predicate.P[string]
        PollConfigs []PollConfigCompiled
@@ -155,6 +159,9 @@ func (p PollConfigCompiled) IsZero() bool {
 }
 
 func (gm *GlobMatcher) CompilePredicate() (func(string) bool, error) {
+       if gm.IsZero() {
+               panic("no includes or excludes")
+       }
        var p predicate.P[string]
        for _, include := range gm.Includes {
                g, err := glob.Compile(include, '/')
@@ -203,5 +210,9 @@ func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
                return c, err
        }
 
+       if c.Cache.For.IsZero() {
+               c.Cache.For = DefaultConfig.Cache.For
+       }
+
        return c, nil
 }
index d3337c023a1f7a0933ea416de7cc82f0b2ffe71a..4d6a5f7184849c4fc022be2ac27844e7e24a4b21 100644 (file)
@@ -22,6 +22,8 @@ import (
 )
 
 func TestConfigCustom(t *testing.T) {
+       t.Parallel()
+
        files := `
 -- hugo.toml --
 [httpcache]
@@ -51,6 +53,8 @@ includes = ["**gohugo.io**"]
 }
 
 func TestConfigDefault(t *testing.T) {
+       t.Parallel()
+
        files := `
 -- hugo.toml --
 `
@@ -62,3 +66,30 @@ func TestConfigDefault(t *testing.T) {
        b.Assert(compiled.For("https://gohugo.io/foo.jpg"), qt.IsFalse)
        b.Assert(compiled.PollConfigFor("https://gohugo.io/foo.jpg").Config.Disable, qt.IsTrue)
 }
+
+func TestConfigPollsOnly(t *testing.T) {
+       t.Parallel()
+       files := `
+-- hugo.toml --
+[httpcache]
+[[httpcache.polls]]
+low = "5s"
+high = "32s"
+[httpcache.polls.for]
+includes = ["**gohugo.io**"]
+               
+       
+`
+
+       b := hugolib.Test(t, files)
+
+       compiled := b.H.Configs.Base.C.HTTPCache
+
+       b.Assert(compiled.For("https://gohugo.io/posts.json"), qt.IsFalse)
+       b.Assert(compiled.For("https://gohugo.io/foo.jpg"), qt.IsFalse)
+
+       pc := compiled.PollConfigFor("https://gohugo.io/foo.jpg")
+       b.Assert(pc.Config.Low, qt.Equals, 5*time.Second)
+       b.Assert(pc.Config.High, qt.Equals, 32*time.Second)
+       b.Assert(compiled.PollConfigFor("https://example.com/foo.jpg").IsZero(), qt.IsTrue)
+}