@marcel91200 / caddy Mirror · read-only / commits / f935458e
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

cmd: Fix validate command when JSON contains "@id" fields

Also, don't run admin server when validating...

Matthew Holt committed Dec 12, 2019 at 14:30 UTC f935458e3e30a8d9f47f37ad35fe9c546b841fa3
3 files changed +22 -13
admin.go
+14
@@ -734,6 +734,20 @@ traverseLoop:
734 return nil
735 }
736
737 +// RemoveMetaFields removes meta fields like "@id" from a JSON message.
738 +func RemoveMetaFields(rawJSON []byte) []byte {
739 + return idRegexp.ReplaceAllFunc(rawJSON, func(in []byte) []byte {
740 + // matches with a comma on both sides (when "@id" property is
741 + // not the first or last in the object) need to keep exactly
742 + // one comma for correct JSON syntax
743 + comma := []byte{','}
744 + if bytes.HasPrefix(in, comma) && bytes.HasSuffix(in, comma) {
745 + return comma
746 + }
747 + return []byte{}
748 + })
749 +}
750 +
751 // AdminHandler is like http.Handler except ServeHTTP may return an error.
752 //
753 // If any handler encounters an error, it should be returned for proper
caddy.go
+6 -13
@@ -153,16 +153,7 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
153 // (an alternate way to do this would be to delete them from
154 // rawCfg as they are indexed, then iterate the index we made
155 // and add them back after encoding as JSON)
156 - newCfg = idRegexp.ReplaceAllFunc(newCfg, func(in []byte) []byte {
157 - // matches with a comma on both sides (when "@id" property is
158 - // not the first or last in the object) need to keep exactly
159 - // one comma for correct JSON syntax
160 - comma := []byte{','}
161 - if bytes.HasPrefix(in, comma) && bytes.HasSuffix(in, comma) {
162 - return comma
163 - }
164 - return []byte{}
165 - })
156 + newCfg = RemoveMetaFields(newCfg)
157
158 // load this new config; if it fails, we need to revert to
159 // our old representation of caddy's actual config
@@ -295,9 +286,11 @@ func run(newCfg *Config, start bool) error {
286 var err error
287
288 // start the admin endpoint (and stop any prior one)
298 - err = replaceAdmin(newCfg)
299 - if err != nil {
300 - return fmt.Errorf("starting caddy administration endpoint: %v", err)
289 + if start {
290 + err = replaceAdmin(newCfg)
291 + if err != nil {
292 + return fmt.Errorf("starting caddy administration endpoint: %v", err)
293 + }
294 }
295
296 if newCfg == nil {
cmd/commandfuncs.go
+2
@@ -465,6 +465,8 @@ func cmdValidateConfig(fl Flags) (int, error) {
465 input = adaptedConfig
466 }
467
468 + input = caddy.RemoveMetaFields(input)
469 +
470 var cfg *caddy.Config
471 err = json.Unmarshal(input, &cfg)
472 if err != nil {