mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-22 13:59:32 +08:00
36a6c7daf0
Fixes #3116 * Rework Replacer loop to ignore escaped braces * Add benchmark tests for replacer * Optimise handling of escaped braces * Handle escaped closing braces * Remove additional check for closing brace This commit removes the additional check for input in which the closing brace appears before the opening brace. This check has been removed for performance reasons as it is deemed an unlikely edge case. * Check for escaped closing braces in placeholder name
254 lines
7.1 KiB
Go
254 lines
7.1 KiB
Go
// 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 caddy
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// NewReplacer returns a new Replacer.
|
|
func NewReplacer() *Replacer {
|
|
rep := &Replacer{
|
|
static: make(map[string]string),
|
|
}
|
|
rep.providers = []ReplacerFunc{
|
|
globalDefaultReplacements,
|
|
rep.fromStatic,
|
|
}
|
|
return rep
|
|
}
|
|
|
|
// Replacer can replace values in strings.
|
|
// A default/empty Replacer is not valid;
|
|
// use NewReplacer to make one.
|
|
type Replacer struct {
|
|
providers []ReplacerFunc
|
|
static map[string]string
|
|
}
|
|
|
|
// Map adds mapFunc to the list of value providers.
|
|
// mapFunc will be executed only at replace-time.
|
|
func (r *Replacer) Map(mapFunc ReplacerFunc) {
|
|
r.providers = append(r.providers, mapFunc)
|
|
}
|
|
|
|
// Set sets a custom variable to a static value.
|
|
func (r *Replacer) Set(variable, value string) {
|
|
r.static[variable] = value
|
|
}
|
|
|
|
// Get gets a value from the replacer. It returns
|
|
// the value and whether the variable was known.
|
|
func (r *Replacer) Get(variable string) (string, bool) {
|
|
for _, mapFunc := range r.providers {
|
|
if val, ok := mapFunc(variable); ok {
|
|
return val, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// Delete removes a variable with a static value
|
|
// that was created using Set.
|
|
func (r *Replacer) Delete(variable string) {
|
|
delete(r.static, variable)
|
|
}
|
|
|
|
// fromStatic provides values from r.static.
|
|
func (r *Replacer) fromStatic(key string) (val string, ok bool) {
|
|
val, ok = r.static[key]
|
|
return
|
|
}
|
|
|
|
// ReplaceOrErr is like ReplaceAll, but any placeholders
|
|
// that are empty or not recognized will cause an error to
|
|
// be returned.
|
|
func (r *Replacer) ReplaceOrErr(input string, errOnEmpty, errOnUnknown bool) (string, error) {
|
|
return r.replace(input, "", false, errOnEmpty, errOnUnknown, nil)
|
|
}
|
|
|
|
// ReplaceKnown is like ReplaceAll but only replaces
|
|
// placeholders that are known (recognized). Unrecognized
|
|
// placeholders will remain in the output.
|
|
func (r *Replacer) ReplaceKnown(input, empty string) string {
|
|
out, _ := r.replace(input, empty, false, false, false, nil)
|
|
return out
|
|
}
|
|
|
|
// ReplaceAll efficiently replaces placeholders in input with
|
|
// their values. All placeholders are replaced in the output
|
|
// whether they are recognized or not. Values that are empty
|
|
// string will be substituted with empty.
|
|
func (r *Replacer) ReplaceAll(input, empty string) string {
|
|
out, _ := r.replace(input, empty, true, false, false, nil)
|
|
return out
|
|
}
|
|
|
|
// ReplaceFunc calls ReplaceAll efficiently replaces placeholders in input with
|
|
// their values. All placeholders are replaced in the output
|
|
// whether they are recognized or not. Values that are empty
|
|
// string will be substituted with empty.
|
|
func (r *Replacer) ReplaceFunc(input string, f ReplacementFunc) (string, error) {
|
|
return r.replace(input, "", true, false, false, f)
|
|
}
|
|
|
|
func (r *Replacer) replace(input, empty string,
|
|
treatUnknownAsEmpty, errOnEmpty, errOnUnknown bool,
|
|
f ReplacementFunc) (string, error) {
|
|
if !strings.Contains(input, string(phOpen)) {
|
|
return input, nil
|
|
}
|
|
|
|
var sb strings.Builder
|
|
|
|
// it is reasonable to assume that the output
|
|
// will be approximately as long as the input
|
|
sb.Grow(len(input))
|
|
|
|
// iterate the input to find each placeholder
|
|
var lastWriteCursor int
|
|
for i := 0; i < len(input); i++ {
|
|
|
|
// check for escaped braces
|
|
if i > 0 && input[i-1] == phEscape && (input[i] == phClose || input[i] == phOpen) {
|
|
sb.WriteString(input[lastWriteCursor : i-1])
|
|
lastWriteCursor = i
|
|
continue
|
|
}
|
|
|
|
if input[i] != phOpen {
|
|
continue
|
|
}
|
|
|
|
// find the end of the placeholder
|
|
end := strings.Index(input[i:], string(phClose)) + i
|
|
if end < i {
|
|
continue
|
|
}
|
|
|
|
// if necessary look for the first closing brace that is not escaped
|
|
for end > 0 && end < len(input)-1 && input[end-1] == phEscape {
|
|
end = strings.Index(input[end+1:], string(phClose)) + end + 1
|
|
}
|
|
|
|
// write the substring from the last cursor to this point
|
|
sb.WriteString(input[lastWriteCursor:i])
|
|
|
|
// trim opening bracket
|
|
key := input[i+1 : end]
|
|
|
|
// try to get a value for this key, handle empty values accordingly
|
|
val, found := r.Get(key)
|
|
if !found {
|
|
// placeholder is unknown (unrecognized); handle accordingly
|
|
if errOnUnknown {
|
|
return "", fmt.Errorf("unrecognized placeholder %s%s%s",
|
|
string(phOpen), key, string(phClose))
|
|
} else if !treatUnknownAsEmpty {
|
|
// if treatUnknownAsEmpty is true, we'll
|
|
// handle an empty val later; so only
|
|
// continue otherwise
|
|
lastWriteCursor = i
|
|
continue
|
|
}
|
|
}
|
|
|
|
// apply any transformations
|
|
if f != nil {
|
|
var err error
|
|
val, err = f(key, val)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
// write the value; if it's empty, either return
|
|
// an error or write a default value
|
|
if val == "" {
|
|
if errOnEmpty {
|
|
return "", fmt.Errorf("evaluated placeholder %s%s%s is empty",
|
|
string(phOpen), key, string(phClose))
|
|
} else if empty != "" {
|
|
sb.WriteString(empty)
|
|
}
|
|
} else {
|
|
sb.WriteString(val)
|
|
}
|
|
|
|
// advance cursor to end of placeholder
|
|
i = end
|
|
lastWriteCursor = i + 1
|
|
}
|
|
|
|
// flush any unwritten remainder
|
|
sb.WriteString(input[lastWriteCursor:])
|
|
|
|
return sb.String(), nil
|
|
}
|
|
|
|
// ReplacerFunc is a function that returns a replacement
|
|
// for the given key along with true if the function is able
|
|
// to service that key (even if the value is blank). If the
|
|
// function does not recognize the key, false should be
|
|
// returned.
|
|
type ReplacerFunc func(key string) (val string, ok bool)
|
|
|
|
func globalDefaultReplacements(key string) (string, bool) {
|
|
// check environment variable
|
|
const envPrefix = "env."
|
|
if strings.HasPrefix(key, envPrefix) {
|
|
return os.Getenv(key[len(envPrefix):]), true
|
|
}
|
|
|
|
switch key {
|
|
case "system.hostname":
|
|
// OK if there is an error; just return empty string
|
|
name, _ := os.Hostname()
|
|
return name, true
|
|
case "system.slash":
|
|
return string(filepath.Separator), true
|
|
case "system.os":
|
|
return runtime.GOOS, true
|
|
case "system.arch":
|
|
return runtime.GOARCH, true
|
|
case "time.now.common_log":
|
|
return nowFunc().Format("02/Jan/2006:15:04:05 -0700"), true
|
|
}
|
|
|
|
return "", false
|
|
}
|
|
|
|
// ReplacementFunc is a function that is called when a
|
|
// replacement is being performed. It receives the
|
|
// variable (i.e. placeholder name) and the value that
|
|
// will be the replacement, and returns the value that
|
|
// will actually be the replacement, or an error. Note
|
|
// that errors are sometimes ignored by replacers.
|
|
type ReplacementFunc func(variable, val string) (string, error)
|
|
|
|
// nowFunc is a variable so tests can change it
|
|
// in order to obtain a deterministic time.
|
|
var nowFunc = time.Now
|
|
|
|
// ReplacerCtxKey is the context key for a replacer.
|
|
const ReplacerCtxKey CtxKey = "replacer"
|
|
|
|
const phOpen, phClose, phEscape = '{', '}', '\\'
|