2020-06-26 16:12:37 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package maphandler
|
|
|
|
|
|
|
|
import (
|
2020-10-02 15:23:40 -05:00
|
|
|
"strings"
|
|
|
|
|
2020-06-26 16:12:37 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
httpcaddyfile.RegisterHandlerDirective("map", parseCaddyfile)
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:23:40 -05:00
|
|
|
// parseCaddyfile sets up the map handler from Caddyfile tokens. Syntax:
|
2020-06-26 16:12:37 -05:00
|
|
|
//
|
2020-10-02 15:23:40 -05:00
|
|
|
// map [<matcher>] <source> <destinations...> {
|
|
|
|
// [~]<input> <outputs...>
|
|
|
|
// default <defaults...>
|
2020-06-26 16:12:37 -05:00
|
|
|
// }
|
|
|
|
//
|
2020-10-02 15:23:40 -05:00
|
|
|
// If the input value is prefixed with a tilde (~), then the input will be parsed as a
|
|
|
|
// regular expression.
|
2020-06-26 16:12:37 -05:00
|
|
|
//
|
2020-10-02 17:08:28 -05:00
|
|
|
// The Caddyfile adapter treats outputs that are a literal hyphen (-) as a null/nil
|
|
|
|
// value. This is useful if you want to fall back to default for that particular output.
|
|
|
|
//
|
2020-10-02 15:23:40 -05:00
|
|
|
// The number of outputs for each mapping must not be more than the number of destinations.
|
|
|
|
// However, for convenience, there may be fewer outputs than destinations and any missing
|
|
|
|
// outputs will be filled in implicitly.
|
2020-06-26 16:12:37 -05:00
|
|
|
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
2020-10-02 15:23:40 -05:00
|
|
|
var handler Handler
|
2020-06-26 16:12:37 -05:00
|
|
|
|
|
|
|
for h.Next() {
|
2020-10-02 15:23:40 -05:00
|
|
|
// source
|
|
|
|
if !h.NextArg() {
|
|
|
|
return nil, h.ArgErr()
|
|
|
|
}
|
|
|
|
handler.Source = h.Val()
|
|
|
|
|
|
|
|
// destinations
|
|
|
|
handler.Destinations = h.RemainingArgs()
|
|
|
|
if len(handler.Destinations) == 0 {
|
|
|
|
return nil, h.Err("missing destination argument(s)")
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:23:40 -05:00
|
|
|
// mappings
|
2020-06-26 16:12:37 -05:00
|
|
|
for h.NextBlock(0) {
|
2020-10-02 15:23:40 -05:00
|
|
|
// defaults are a special case
|
|
|
|
if h.Val() == "default" {
|
|
|
|
if len(handler.Defaults) > 0 {
|
|
|
|
return nil, h.Err("defaults already defined")
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|
2020-10-02 15:23:40 -05:00
|
|
|
handler.Defaults = h.RemainingArgs()
|
|
|
|
for len(handler.Defaults) < len(handler.Destinations) {
|
|
|
|
handler.Defaults = append(handler.Defaults, "")
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|
2020-10-02 15:23:40 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// every other line maps one input to one or more outputs
|
|
|
|
in := h.Val()
|
2020-10-02 16:23:52 -05:00
|
|
|
var outs []interface{}
|
|
|
|
for _, out := range h.RemainingArgs() {
|
2020-10-02 17:08:28 -05:00
|
|
|
if out == "-" {
|
|
|
|
outs = append(outs, nil)
|
|
|
|
} else {
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
2020-10-02 16:23:52 -05:00
|
|
|
}
|
2020-10-02 15:23:40 -05:00
|
|
|
|
|
|
|
// cannot have more outputs than destinations
|
|
|
|
if len(outs) > len(handler.Destinations) {
|
|
|
|
return nil, h.Err("too many outputs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// for convenience, can have fewer outputs than destinations, but the
|
2020-10-02 16:23:52 -05:00
|
|
|
// underlying handler won't accept that, so we fill in nil values
|
2020-10-02 15:23:40 -05:00
|
|
|
for len(outs) < len(handler.Destinations) {
|
2020-10-02 16:23:52 -05:00
|
|
|
outs = append(outs, nil)
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|
2020-10-02 15:23:40 -05:00
|
|
|
|
|
|
|
// create the mapping
|
|
|
|
mapping := Mapping{Outputs: outs}
|
|
|
|
if strings.HasPrefix(in, "~") {
|
|
|
|
mapping.InputRegexp = in[1:]
|
|
|
|
} else {
|
|
|
|
mapping.Input = in
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.Mappings = append(handler.Mappings, mapping)
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:23:40 -05:00
|
|
|
return handler, nil
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|