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

Refactor ExtractMatcherSet()

Matthew Holt committed Feb 27, 2020 at 21:03 UTC cef6e098bb278d50cb2f45f57ed40b3af47d6a74
2 files changed +21 -6
caddyconfig/httpcaddyfile/directives.go
+17
@@ -161,6 +161,23 @@ func (h Helper) MatcherToken() (caddy.ModuleMap, bool, error) {
161 return matcherSetFromMatcherToken(h.Dispenser.Token(), h.matcherDefs, h.warnings)
162 }
163
164 +// ExtractMatcherSet is like MatcherToken, except this is a higher-level
165 +// method that returns the matcher set described by the matcher token,
166 +// or nil if there is none, and deletes the matcher token from the
167 +// dispenser and resets it as if this look-ahead never happened. Useful
168 +// when wrapping a route (one or more handlers) in a user-defined matcher.
169 +func (h Helper) ExtractMatcherSet() (caddy.ModuleMap, error) {
170 + matcherSet, hasMatcher, err := h.MatcherToken()
171 + if err != nil {
172 + return nil, err
173 + }
174 + if hasMatcher {
175 + h.Dispenser.Delete() // strip matcher token
176 + }
177 + h.Dispenser.Reset() // pretend this lookahead never happened
178 + return matcherSet, nil
179 +}
180 +
181 // NewRoute returns config values relevant to creating a new HTTP route.
182 func (h Helper) NewRoute(matcherSet caddy.ModuleMap,
183 handler caddyhttp.MiddlewareHandler) []ConfigValue {
modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go
+4 -6
@@ -167,14 +167,10 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
167 // either way, strip the matcher token and pass
168 // the remaining tokens to the unmarshaler so that
169 // we can gain the rest of the reverse_proxy syntax
170 - userMatcherSet, hasUserMatcher, err := h.MatcherToken()
170 + userMatcherSet, err := h.ExtractMatcherSet()
171 if err != nil {
172 return nil, err
173 }
174 - if hasUserMatcher {
175 - h.Dispenser.Delete() // strip matcher token
176 - }
177 - h.Dispenser.Reset() // pretend this lookahead never happened
174
175 // set up the transport for FastCGI, and specifically PHP
176 fcgiTransport := Transport{SplitPath: ".php"}
@@ -186,6 +182,8 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
182
183 // the rest of the config is specified by the user
184 // using the reverse_proxy directive syntax
185 + // TODO: this can overwrite our fcgiTransport that we encoded and
186 + // set on the rpHandler... even with a non-fastcgi transport!
187 err = rpHandler.UnmarshalCaddyfile(h.Dispenser)
188 if err != nil {
189 return nil, err
@@ -204,7 +202,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
202
203 // the user's matcher is a prerequisite for ours, so
204 // wrap ours in a subroute and return that
207 - if hasUserMatcher {
205 + if userMatcherSet != nil {
206 return []httpcaddyfile.ConfigValue{
207 {
208 Class: "route",