summaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-03-08 16:33:15 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-03-08 19:57:30 +0100
commit108314444b510bfc330ccac745dce7beccd52c91 (patch)
treed689af9dd1c838dd23e3a3b2cb78a65a601f4d9c /config
parent51e178a6a28a3f305d89ebb489675743f80862ee (diff)
Add HTTP header support for the dev server
Fixes #7031
Diffstat (limited to 'config')
-rw-r--r--config/commonConfig.go59
-rw-r--r--config/commonConfig_test.go24
2 files changed, 83 insertions, 0 deletions
diff --git a/config/commonConfig.go b/config/commonConfig.go
index ab2cfe80b..17d5619bb 100644
--- a/config/commonConfig.go
+++ b/config/commonConfig.go
@@ -14,8 +14,13 @@
package config
import (
+ "sort"
"strings"
+ "sync"
+ "github.com/gohugoio/hugo/common/types"
+
+ "github.com/gobwas/glob"
"github.com/gohugoio/hugo/common/herrors"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
@@ -88,3 +93,57 @@ func DecodeSitemap(prototype Sitemap, input map[string]interface{}) Sitemap {
return prototype
}
+
+// Config for the dev server.
+type Server struct {
+ Headers []Headers
+
+ compiledInit sync.Once
+ compiled []glob.Glob
+}
+
+func (s *Server) Match(pattern string) []types.KeyValueStr {
+ s.compiledInit.Do(func() {
+ for _, h := range s.Headers {
+ s.compiled = append(s.compiled, glob.MustCompile(h.For))
+ }
+ })
+
+ if s.compiled == nil {
+ return nil
+ }
+
+ var matches []types.KeyValueStr
+
+ for i, g := range s.compiled {
+ if g.Match(pattern) {
+ h := s.Headers[i]
+ for k, v := range h.Values {
+ matches = append(matches, types.KeyValueStr{Key: k, Value: cast.ToString(v)})
+ }
+ }
+ }
+
+ sort.Slice(matches, func(i, j int) bool {
+ return matches[i].Key < matches[j].Key
+ })
+
+ return matches
+
+}
+
+type Headers struct {
+ For string
+ Values map[string]interface{}
+}
+
+func DecodeServer(cfg Provider) *Server {
+ m := cfg.GetStringMap("server")
+ s := &Server{}
+ if m == nil {
+ return s
+ }
+
+ _ = mapstructure.WeakDecode(m, s)
+ return s
+}
diff --git a/config/commonConfig_test.go b/config/commonConfig_test.go
index 281d2b0b6..41b2721bc 100644
--- a/config/commonConfig_test.go
+++ b/config/commonConfig_test.go
@@ -18,6 +18,7 @@ import (
"testing"
"github.com/gohugoio/hugo/common/herrors"
+ "github.com/gohugoio/hugo/common/types"
qt "github.com/frankban/quicktest"
@@ -58,3 +59,26 @@ func TestBuild(t *testing.T) {
c.Assert(b.UseResourceCache(nil), qt.Equals, false)
}
+
+func TestServer(t *testing.T) {
+ c := qt.New(t)
+
+ cfg, err := FromConfigString(`[[server.headers]]
+for = "/*.jpg"
+
+[server.headers.values]
+X-Frame-Options = "DENY"
+X-XSS-Protection = "1; mode=block"
+X-Content-Type-Options = "nosniff"
+`, "toml")
+
+ c.Assert(err, qt.IsNil)
+
+ s := DecodeServer(cfg)
+
+ c.Assert(s.Match("/foo.jpg"), qt.DeepEquals, []types.KeyValueStr{
+ {Key: "X-Content-Type-Options", Value: "nosniff"},
+ {Key: "X-Frame-Options", Value: "DENY"},
+ {Key: "X-XSS-Protection", Value: "1; mode=block"}})
+
+}