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
file_server: Automatically hide all involved Caddyfiles
Matthew Holt committed
Aug 21, 2019 at 15:50 UTC
c0da7d487abcdcd9761220d75400f75584482b5f
3 files changed
+30
caddyconfig/httpcaddyfile/directives.go
+20
@@ -81,6 +81,26 @@ type Helper struct {
81
*caddyfile.Dispenser
82
warnings *[]caddyconfig.Warning
83
matcherDefs map[string]map[string]json.RawMessage
84
+ parentBlock caddyfile.ServerBlock
85
+}
86
+
87
+// Caddyfiles returns the list of config files from
88
+// which tokens in the current server block were loaded.
89
+func (h Helper) Caddyfiles() []string {
90
+ // first obtain set of names of files involved
91
+ // in this server block, without duplicates
92
+ files := make(map[string]struct{})
93
+ for _, segment := range h.parentBlock.Segments {
94
+ for _, token := range segment {
95
+ files[token.File] = struct{}{}
96
+ }
97
+ }
98
+ // then convert the set into a slice
99
+ filesSlice := make([]string, 0, len(files))
100
+ for file := range files {
101
+ filesSlice = append(filesSlice, file)
102
+ }
103
+ return filesSlice
104
}
105
106
// JSON converts val into JSON. Any errors are added to warnings.
caddyconfig/httpcaddyfile/httptype.go
+1
@@ -95,6 +95,7 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
95
Dispenser: caddyfile.NewDispenser(segment),
96
warnings: &warnings,
97
matcherDefs: matcherDefs,
98
+ parentBlock: sb.block,
99
})
100
if err != nil {
101
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
modules/caddyhttp/fileserver/caddyfile.go
+9
@@ -76,6 +76,15 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
76
fsrv.Root = "{http.var.root}"
77
}
78
79
+ // hide the Caddyfile (and any imported Caddyfiles)
80
+ if configFiles := h.Caddyfiles(); len(configFiles) > 0 {
81
+ for _, file := range configFiles {
82
+ if !fileHidden(file, fsrv.Hide) {
83
+ fsrv.Hide = append(fsrv.Hide, file)
84
+ }
85
+ }
86
+ }
87
+
88
return &fsrv, nil
89
}
90