@marcel91200 / caddy Mirror · read-only / commits / 26fb8b3e
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

httpcaddyfile: remove certificate tags from global state (#3111)

* remove the certificate tag tracking from global state * refactored helper state, added log counter * moved state initialisation close to where it is used. * added helper state comment

Mark Sargent committed Mar 5, 2020 at 05:58 UTC 26fb8b3efd671a02a92ee55e93f547606285cde4
3 files changed +27 -11
caddyconfig/httpcaddyfile/builtins.go
+19 -9
@@ -152,6 +152,18 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
152 // policy that is looking for any tag but the last one to be
153 // loaded won't find it, and TLS handshakes will fail (see end)
154 // of issue #3004)
155 +
156 + // tlsCertTags maps certificate filenames to their tag.
157 + // This is used to remember which tag is used for each
158 + // certificate files, since we need to avoid loading
159 + // the same certificate files more than once, overwriting
160 + // previous tags
161 + tlsCertTags, ok := h.State["tlsCertTags"].(map[string]string)
162 + if !ok {
163 + tlsCertTags = make(map[string]string)
164 + h.State["tlsCertTags"] = tlsCertTags
165 + }
166 +
167 tag, ok := tlsCertTags[certFilename]
168 if !ok {
169 // haven't seen this cert file yet, let's give it a tag
@@ -521,10 +533,17 @@ func parseLog(h Helper) ([]ConfigValue, error) {
533
534 var val namedCustomLog
535 if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
536 +
537 + logCounter, ok := h.State["logCounter"].(int)
538 + if !ok {
539 + logCounter = 0
540 + }
541 +
542 cl.Include = []string{"http.log.access"}
543 val.name = fmt.Sprintf("log%d", logCounter)
544 val.log = cl
545 logCounter++
546 + h.State["logCounter"] = logCounter
547 }
548 configValues = append(configValues, ConfigValue{
549 Class: "custom_log",
@@ -533,12 +552,3 @@ func parseLog(h Helper) ([]ConfigValue, error) {
552 }
553 return configValues, nil
554 }
536 -
537 -// tlsCertTags maps certificate filenames to their tag.
538 -// This is used to remember which tag is used for each
539 -// certificate files, since we need to avoid loading
540 -// the same certificate files more than once, overwriting
541 -// previous tags
542 -var tlsCertTags = make(map[string]string)
543 -
544 -var logCounter int
caddyconfig/httpcaddyfile/directives.go
+2
@@ -114,6 +114,8 @@ func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
114 // Caddyfile tokens.
115 type Helper struct {
116 *caddyfile.Dispenser
117 + // State stores intermediate variables during caddyfile adaptation.
118 + State map[string]interface{}
119 options map[string]interface{}
120 warnings *[]caddyconfig.Warning
121 matcherDefs map[string]caddy.ModuleMap
caddyconfig/httpcaddyfile/httptype.go
+6 -2
@@ -42,6 +42,7 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
42 options map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error) {
43 var warnings []caddyconfig.Warning
44 gc := counter{new(int)}
45 + state := make(map[string]interface{})
46
47 // load all the server blocks and associate them with a "pile"
48 // of config values; also prohibit duplicate keys because they
@@ -133,14 +134,17 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
134 return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
135 }
136
136 - results, err := dirFunc(Helper{
137 + h := Helper{
138 Dispenser: caddyfile.NewDispenser(segment),
139 options: options,
140 warnings: &warnings,
141 matcherDefs: matcherDefs,
142 parentBlock: sb.block,
143 groupCounter: gc,
143 - })
144 + State: state,
145 + }
146 +
147 + results, err := dirFunc(h)
148 if err != nil {
149 return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
150 }