Don't panic when regex doesn't compile
Instead, print a warning and discard the invalid rule.
This commit is contained in:
parent
adfa6d398e
commit
d0e5eca1f4
4 changed files with 81 additions and 9 deletions
|
@ -26,10 +26,20 @@ type HeaderConfig struct {
|
||||||
CompiledRegex *regexp.Regexp
|
CompiledRegex *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *HeaderConfig) Init() {
|
func (config *HeaderConfig) Init() (ok bool) {
|
||||||
if len(config.Regex) > 0 {
|
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 {
|
func (config *HeaderConfig) UsesRegex() bool {
|
||||||
|
@ -42,7 +52,7 @@ type HeaderDefiniton struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var headerConfigs *HeaderConfigArray
|
var headerConfigs HeaderConfigArray
|
||||||
|
|
||||||
func fileExists(filename string) bool {
|
func fileExists(filename string) bool {
|
||||||
info, err := os.Stat(filename)
|
info, err := os.Stat(filename)
|
||||||
|
@ -52,7 +62,7 @@ func fileExists(filename string) bool {
|
||||||
return !info.IsDir()
|
return !info.IsDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
func logHeaderConfig(config *HeaderConfig) {
|
func logHeaderConfig(config HeaderConfig) {
|
||||||
if config.UsesRegex() {
|
if config.UsesRegex() {
|
||||||
fmt.Println("Regex: " + config.Regex)
|
fmt.Println("Regex: " + config.Regex)
|
||||||
} else {
|
} else {
|
||||||
|
@ -74,7 +84,7 @@ func initHeaderConfig(headerConfigPath string) bool {
|
||||||
if fileExists(headerConfigPath) {
|
if fileExists(headerConfigPath) {
|
||||||
jsonFile, err := os.Open(headerConfigPath)
|
jsonFile, err := os.Open(headerConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Cant't read header config file. Error:")
|
fmt.Println("Can't read header config file. Error:")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
} else {
|
} else {
|
||||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||||
|
@ -83,12 +93,22 @@ func initHeaderConfig(headerConfigPath string) bool {
|
||||||
|
|
||||||
if len(headerConfigs.Configs) > 0 {
|
if len(headerConfigs.Configs) > 0 {
|
||||||
headerConfigValid = true
|
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("Found header config file. Rules:")
|
||||||
fmt.Println("------------------------------")
|
fmt.Println("------------------------------")
|
||||||
|
for _, configEntry := range headerConfigs.Configs {
|
||||||
for i := 0; i < len(headerConfigs.Configs); i++ {
|
|
||||||
configEntry := &headerConfigs.Configs[i]
|
|
||||||
configEntry.Init()
|
|
||||||
logHeaderConfig(configEntry)
|
logHeaderConfig(configEntry)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
39
customHeaders_test.go
Normal file
39
customHeaders_test.go
Normal file
|
@ -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())
|
||||||
|
}
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module github.com/PierreZ/goStatic
|
module github.com/PierreZ/goStatic
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
|
require github.com/stretchr/testify v1.7.0
|
||||||
|
|
11
go.sum
Normal file
11
go.sum
Normal file
|
@ -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=
|
Loading…
Reference in a new issue