From ece4a91ec3a285a5f41ddfae176b4c917d357900 Mon Sep 17 00:00:00 2001 From: Alex Tan Date: Fri, 9 Jul 2021 22:52:01 -0500 Subject: [PATCH 1/5] Add `Regex` option for HeaderConfig --- .gitignore | 1 + customHeaders.go | 43 +++++++++++++++++++++++++++++++++++-------- docs/header-config.md | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index bd4655a..d4930fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ goStatic +.DS_Store diff --git a/customHeaders.go b/customHeaders.go index 1e819bf..7e9a2e3 100644 --- a/customHeaders.go +++ b/customHeaders.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "strings" ) @@ -17,9 +18,22 @@ type HeaderConfigArray struct { // HeaderConfig is a single header rule specification type HeaderConfig struct { + Regex string `json:"regex"` Path string `json:"path"` FileExtension string `json:"fileExtension"` Headers []HeaderDefiniton `json:"headers"` + + CompiledRegex *regexp.Regexp +} + +func (config *HeaderConfig) Init() { + if config.UsesRegex() { + config.CompiledRegex = regexp.MustCompile(config.Regex) + } +} + +func (config *HeaderConfig) UsesRegex() bool { + return len(config.Regex) > 0 } // HeaderDefiniton is a key value pair of a specified header rule @@ -28,7 +42,7 @@ type HeaderDefiniton struct { Value string `json:"value"` } -var headerConfigs HeaderConfigArray +var headerConfigs *HeaderConfigArray func fileExists(filename string) bool { info, err := os.Stat(filename) @@ -38,9 +52,13 @@ func fileExists(filename string) bool { return !info.IsDir() } -func logHeaderConfig(config HeaderConfig) { - fmt.Println("Path: " + config.Path) - fmt.Println("FileExtension: " + config.FileExtension) +func logHeaderConfig(config *HeaderConfig) { + if config.UsesRegex() { + fmt.Println("Regex: " + config.Regex) + } else { + fmt.Println("Path: " + config.Path) + fmt.Println("FileExtension: " + config.FileExtension) + } for j := 0; j < len(config.Headers); j++ { headerRule := config.Headers[j] @@ -69,7 +87,8 @@ func initHeaderConfig(headerConfigPath string) bool { fmt.Println("------------------------------") for i := 0; i < len(headerConfigs.Configs); i++ { - configEntry := headerConfigs.Configs[i] + configEntry := &headerConfigs.Configs[i] + configEntry.Init() logHeaderConfig(configEntry) } } else { @@ -89,11 +108,19 @@ func customHeadersMiddleware(next http.Handler) http.Handler { for i := 0; i < len(headerConfigs.Configs); i++ { configEntry := headerConfigs.Configs[i] + var matches bool - fileMatch := configEntry.FileExtension == "*" || reqFileExtension == "."+configEntry.FileExtension - pathMatch := configEntry.Path == "*" || strings.HasPrefix(r.URL.Path, configEntry.Path) + if configEntry.UsesRegex() { + if configEntry.CompiledRegex.MatchString(r.URL.Path) { + matches = true + } + } else { + fileMatch := configEntry.FileExtension == "*" || reqFileExtension == "."+configEntry.FileExtension + pathMatch := configEntry.Path == "*" || strings.HasPrefix(r.URL.Path, configEntry.Path) + matches = fileMatch && pathMatch + } - if fileMatch && pathMatch { + if matches { for j := 0; j < len(configEntry.Headers); j++ { headerEntry := configEntry.Headers[j] w.Header().Set(headerEntry.Key, headerEntry.Value) diff --git a/docs/header-config.md b/docs/header-config.md index b97255d..f498759 100644 --- a/docs/header-config.md +++ b/docs/header-config.md @@ -4,7 +4,44 @@ With the header config, you can specify custom [HTTP Header](https://developer.m ## Config -You have to create a JSON file that serves as a config. The JSON must contain a `configs` array. For every entry, you can specify a certain path that must be matched as well as a file extension. You can use the `*` symbol to use the config entry for any path or filename. Note that the path option only matches the requested path from the start. Thatswhy you have to start with a `/` and can use paths like `/files/static/css`. The `headers` array includes a key-value pair of the actual header rule. The headers are not parsed so double check your spelling and test your site. +You have to create a JSON file that serves as a config. The JSON must contain a `configs` array. For every entry, you can specify the rule in one of two ways: + +1. A regular expression that the path must meet, e.g.: + +```json +{ + "regex": "/$", + "headers": [ + { + "key": "cache-control", + "value": "no-cache, no-store, must-revalidate" + } + ] +} +``` + +This rule would match any path ending in `/` which is useful if you never want to cache the `index.html` that a directory leads to. + +2. You may specify a combination of `path` and `fileExtension`: + +```json +{ + "path": "*", + "fileExtension": "html", + "headers": [ + { + "key": "cache-control", + "value": "public, max-age=0, must-revalidate" + }, + { + "key": "Strict-Transport-Security", + "value": "max-age=31536000; includeSubDomains;" + } + ] +} +``` + +You can use the `*` symbol to use the config entry for any path or filename. Note that the path option only matches the requested path from the start. That's why you have to start with a `/` and can use paths like `/files/static/css`. The `headers` array includes a key-value pair of the actual header rule. The headers are not parsed so double check your spelling and test your site. The created JSON config has to be mounted into the container via a volume into `/config/headerConfig.json` per default. When this file does not exist inside the container, the header middleware will not be active. From 06466faa87a3f93373476acce08d69c20a755a3e Mon Sep 17 00:00:00 2001 From: Alex Tan Date: Sat, 10 Jul 2021 00:54:07 -0500 Subject: [PATCH 2/5] Change how UsesRegex is determined --- customHeaders.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/customHeaders.go b/customHeaders.go index 7e9a2e3..d10b540 100644 --- a/customHeaders.go +++ b/customHeaders.go @@ -27,13 +27,13 @@ type HeaderConfig struct { } func (config *HeaderConfig) Init() { - if config.UsesRegex() { + if len(config.Regex) > 0 { config.CompiledRegex = regexp.MustCompile(config.Regex) } } func (config *HeaderConfig) UsesRegex() bool { - return len(config.Regex) > 0 + return config.CompiledRegex != nil } // HeaderDefiniton is a key value pair of a specified header rule From d5b3eace4aa7fea2ee3fad32e2bb7e6c0d3d4236 Mon Sep 17 00:00:00 2001 From: Alex Tan Date: Sat, 10 Jul 2021 00:56:08 -0500 Subject: [PATCH 3/5] Don't check if path matches if it's not necessary --- customHeaders.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/customHeaders.go b/customHeaders.go index d10b540..f7c78e8 100644 --- a/customHeaders.go +++ b/customHeaders.go @@ -115,9 +115,11 @@ func customHeadersMiddleware(next http.Handler) http.Handler { matches = true } } else { - fileMatch := configEntry.FileExtension == "*" || reqFileExtension == "."+configEntry.FileExtension - pathMatch := configEntry.Path == "*" || strings.HasPrefix(r.URL.Path, configEntry.Path) - matches = fileMatch && pathMatch + matches = + // Check if the file extension matches. + (configEntry.FileExtension == "*" || reqFileExtension == "."+configEntry.FileExtension) && + // Check if the path matches. + (configEntry.Path == "*" || strings.HasPrefix(r.URL.Path, configEntry.Path)) } if matches { From adfa6d398e44d68a4a4eb09bdbd5adfd539b4e09 Mon Sep 17 00:00:00 2001 From: Alex Tan Date: Sat, 10 Jul 2021 12:19:43 -0500 Subject: [PATCH 4/5] Simplify conditional --- customHeaders.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/customHeaders.go b/customHeaders.go index f7c78e8..f001a32 100644 --- a/customHeaders.go +++ b/customHeaders.go @@ -111,9 +111,7 @@ func customHeadersMiddleware(next http.Handler) http.Handler { var matches bool if configEntry.UsesRegex() { - if configEntry.CompiledRegex.MatchString(r.URL.Path) { - matches = true - } + matches = configEntry.CompiledRegex.MatchString(r.URL.Path) } else { matches = // Check if the file extension matches. From d0e5eca1f4ec5495265f51fd8ce8296853ba1e45 Mon Sep 17 00:00:00 2001 From: Alex Tan Date: Sun, 11 Jul 2021 16:09:55 -0500 Subject: [PATCH 5/5] Don't panic when regex doesn't compile Instead, print a warning and discard the invalid rule. --- customHeaders.go | 38 +++++++++++++++++++++++++++++--------- customHeaders_test.go | 39 +++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 11 +++++++++++ 4 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 customHeaders_test.go create mode 100644 go.sum diff --git a/customHeaders.go b/customHeaders.go index f001a32..29aff43 100644 --- a/customHeaders.go +++ b/customHeaders.go @@ -26,10 +26,20 @@ type HeaderConfig struct { CompiledRegex *regexp.Regexp } -func (config *HeaderConfig) Init() { +func (config *HeaderConfig) Init() (ok bool) { if len(config.Regex) > 0 { - config.CompiledRegex = regexp.MustCompile(config.Regex) + regex, err := regexp.Compile(config.Regex) + + if err != nil { + fmt.Println("WARNING: Ignoring rule with regex error:", err) + fmt.Println("") + return + } + config.CompiledRegex = regex } + + ok = true + return } func (config *HeaderConfig) UsesRegex() bool { @@ -42,7 +52,7 @@ type HeaderDefiniton struct { Value string `json:"value"` } -var headerConfigs *HeaderConfigArray +var headerConfigs HeaderConfigArray func fileExists(filename string) bool { info, err := os.Stat(filename) @@ -52,7 +62,7 @@ func fileExists(filename string) bool { return !info.IsDir() } -func logHeaderConfig(config *HeaderConfig) { +func logHeaderConfig(config HeaderConfig) { if config.UsesRegex() { fmt.Println("Regex: " + config.Regex) } else { @@ -74,7 +84,7 @@ func initHeaderConfig(headerConfigPath string) bool { if fileExists(headerConfigPath) { jsonFile, err := os.Open(headerConfigPath) if err != nil { - fmt.Println("Cant't read header config file. Error:") + fmt.Println("Can't read header config file. Error:") fmt.Println(err) } else { byteValue, _ := ioutil.ReadAll(jsonFile) @@ -83,12 +93,22 @@ func initHeaderConfig(headerConfigPath string) bool { if len(headerConfigs.Configs) > 0 { headerConfigValid = true + + // Only keep valid config entries. + keepers := make([]HeaderConfig, 0) + for _, configEntry := range headerConfigs.Configs { + ok := configEntry.Init() + if ok { + keepers = append(keepers, configEntry) + } + } + + headerConfigs.Configs = keepers + + // Print the config entries that are kept. fmt.Println("Found header config file. Rules:") fmt.Println("------------------------------") - - for i := 0; i < len(headerConfigs.Configs); i++ { - configEntry := &headerConfigs.Configs[i] - configEntry.Init() + for _, configEntry := range headerConfigs.Configs { logHeaderConfig(configEntry) } } else { diff --git a/customHeaders_test.go b/customHeaders_test.go new file mode 100644 index 0000000..81a478b --- /dev/null +++ b/customHeaders_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHeaderConfigWithValidRegex(t *testing.T) { + assert := assert.New(t) + config := HeaderConfig{Regex: "/$"} + + ok := config.Init() + assert.True(ok) + assert.NotNil(config.CompiledRegex) + assert.True(config.UsesRegex()) +} + +func TestHeaderConfigWithInvalidRegex(t *testing.T) { + assert := assert.New(t) + config := HeaderConfig{Regex: "["} + + ok := config.Init() + assert.False(ok) + assert.Nil(config.CompiledRegex) +} + +func TestHeaderConfigWithoutRegex(t *testing.T) { + assert := assert.New(t) + config := HeaderConfig{ + Path: "/page-data", + FileExtension: "json", + } + + ok := config.Init() + assert.True(ok) + assert.Nil(config.CompiledRegex) + assert.False(config.UsesRegex()) +} diff --git a/go.mod b/go.mod index 0d42a78..af73224 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/PierreZ/goStatic go 1.16 + +require github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..acb88a4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=