2019-06-30 17:07:58 -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.
|
|
|
|
|
2019-06-14 12:58:28 -05:00
|
|
|
package caddy
|
2019-05-20 11:59:20 -05:00
|
|
|
|
|
|
|
import (
|
2019-10-28 15:39:37 -05:00
|
|
|
"fmt"
|
2023-03-27 15:51:13 -05:00
|
|
|
"net/http"
|
2019-05-20 11:59:20 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2020-03-13 12:06:08 -05:00
|
|
|
"strconv"
|
2019-05-20 11:59:20 -05:00
|
|
|
"strings"
|
2024-03-21 13:15:18 -05:00
|
|
|
"sync"
|
2019-10-10 16:38:30 -05:00
|
|
|
"time"
|
2019-05-20 11:59:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewReplacer returns a new Replacer.
|
2019-12-29 15:12:52 -05:00
|
|
|
func NewReplacer() *Replacer {
|
|
|
|
rep := &Replacer{
|
2024-03-21 13:15:18 -05:00
|
|
|
static: make(map[string]any),
|
|
|
|
mapMutex: &sync.RWMutex{},
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
2019-12-17 18:29:09 -05:00
|
|
|
rep.providers = []ReplacerFunc{
|
2019-07-16 13:27:11 -05:00
|
|
|
globalDefaultReplacements,
|
|
|
|
rep.fromStatic,
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
return rep
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:29:04 -05:00
|
|
|
// NewEmptyReplacer returns a new Replacer,
|
|
|
|
// without the global default replacements.
|
|
|
|
func NewEmptyReplacer() *Replacer {
|
|
|
|
rep := &Replacer{
|
2024-03-21 13:15:18 -05:00
|
|
|
static: make(map[string]any),
|
|
|
|
mapMutex: &sync.RWMutex{},
|
2021-04-22 19:29:04 -05:00
|
|
|
}
|
|
|
|
rep.providers = []ReplacerFunc{
|
|
|
|
rep.fromStatic,
|
|
|
|
}
|
|
|
|
return rep
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:12:52 -05:00
|
|
|
// Replacer can replace values in strings.
|
|
|
|
// A default/empty Replacer is not valid;
|
|
|
|
// use NewReplacer to make one.
|
|
|
|
type Replacer struct {
|
2019-12-17 18:29:09 -05:00
|
|
|
providers []ReplacerFunc
|
2024-03-21 13:15:18 -05:00
|
|
|
|
|
|
|
static map[string]any
|
|
|
|
mapMutex *sync.RWMutex
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
// Map adds mapFunc to the list of value providers.
|
|
|
|
// mapFunc will be executed only at replace-time.
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) Map(mapFunc ReplacerFunc) {
|
2019-07-16 13:27:11 -05:00
|
|
|
r.providers = append(r.providers, mapFunc)
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets a custom variable to a static value.
|
2022-08-02 15:39:09 -05:00
|
|
|
func (r *Replacer) Set(variable string, value any) {
|
2024-03-21 13:15:18 -05:00
|
|
|
r.mapMutex.Lock()
|
2019-05-20 11:59:20 -05:00
|
|
|
r.static[variable] = value
|
2024-03-21 13:15:18 -05:00
|
|
|
r.mapMutex.Unlock()
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:12:52 -05:00
|
|
|
// Get gets a value from the replacer. It returns
|
|
|
|
// the value and whether the variable was known.
|
2022-08-02 15:39:09 -05:00
|
|
|
func (r *Replacer) Get(variable string) (any, bool) {
|
2019-12-29 15:12:52 -05:00
|
|
|
for _, mapFunc := range r.providers {
|
|
|
|
if val, ok := mapFunc(variable); ok {
|
|
|
|
return val, true
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 12:49:53 -05:00
|
|
|
return nil, false
|
2019-12-29 15:12:52 -05:00
|
|
|
}
|
|
|
|
|
2022-09-01 22:15:20 -05:00
|
|
|
// GetString is the same as Get, but coerces the value to a
|
|
|
|
// string representation as efficiently as possible.
|
2020-06-26 16:12:37 -05:00
|
|
|
func (r *Replacer) GetString(variable string) (string, bool) {
|
|
|
|
s, found := r.Get(variable)
|
2022-09-01 22:15:20 -05:00
|
|
|
return ToString(s), found
|
2020-06-26 16:12:37 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:59:20 -05:00
|
|
|
// Delete removes a variable with a static value
|
|
|
|
// that was created using Set.
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) Delete(variable string) {
|
2024-03-21 13:15:18 -05:00
|
|
|
r.mapMutex.Lock()
|
2019-05-20 11:59:20 -05:00
|
|
|
delete(r.static, variable)
|
2024-03-21 13:15:18 -05:00
|
|
|
r.mapMutex.Unlock()
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
// fromStatic provides values from r.static.
|
2022-08-02 15:39:09 -05:00
|
|
|
func (r *Replacer) fromStatic(key string) (any, bool) {
|
2024-03-21 13:15:18 -05:00
|
|
|
r.mapMutex.RLock()
|
|
|
|
defer r.mapMutex.RUnlock()
|
2020-03-30 12:49:53 -05:00
|
|
|
val, ok := r.static[key]
|
|
|
|
return val, ok
|
2019-07-16 13:27:11 -05:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:39:37 -05:00
|
|
|
// ReplaceOrErr is like ReplaceAll, but any placeholders
|
|
|
|
// that are empty or not recognized will cause an error to
|
|
|
|
// be returned.
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) ReplaceOrErr(input string, errOnEmpty, errOnUnknown bool) (string, error) {
|
2019-12-17 18:29:09 -05:00
|
|
|
return r.replace(input, "", false, errOnEmpty, errOnUnknown, nil)
|
2019-10-28 15:39:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceKnown is like ReplaceAll but only replaces
|
|
|
|
// placeholders that are known (recognized). Unrecognized
|
|
|
|
// placeholders will remain in the output.
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) ReplaceKnown(input, empty string) string {
|
2019-12-17 18:29:09 -05:00
|
|
|
out, _ := r.replace(input, empty, false, false, false, nil)
|
2019-10-28 15:39:37 -05:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
// ReplaceAll efficiently replaces placeholders in input with
|
2019-10-28 15:39:37 -05:00
|
|
|
// 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.
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) ReplaceAll(input, empty string) string {
|
2019-12-17 18:29:09 -05:00
|
|
|
out, _ := r.replace(input, empty, true, false, false, nil)
|
2019-10-28 15:39:37 -05:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-03-30 12:49:53 -05:00
|
|
|
// ReplaceFunc is the same as ReplaceAll, but calls f for every
|
|
|
|
// replacement to be made, in case f wants to change or inspect
|
|
|
|
// the replacement.
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) ReplaceFunc(input string, f ReplacementFunc) (string, error) {
|
2019-12-17 18:29:09 -05:00
|
|
|
return r.replace(input, "", true, false, false, f)
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:12:52 -05:00
|
|
|
func (r *Replacer) replace(input, empty string,
|
2019-12-17 18:29:09 -05:00
|
|
|
treatUnknownAsEmpty, errOnEmpty, errOnUnknown bool,
|
2023-08-07 14:40:31 -05:00
|
|
|
f ReplacementFunc,
|
|
|
|
) (string, error) {
|
2024-01-13 15:24:03 -05:00
|
|
|
if !strings.Contains(input, string(phOpen)) && !strings.Contains(input, string(phClose)) {
|
2019-10-28 15:39:37 -05:00
|
|
|
return input, nil
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
2019-07-16 13:27:11 -05:00
|
|
|
|
|
|
|
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
|
2020-03-30 12:49:53 -05:00
|
|
|
|
2022-09-15 14:36:08 -05:00
|
|
|
// fail fast if too many placeholders are unclosed
|
|
|
|
var unclosedCount int
|
|
|
|
|
2020-03-11 17:12:00 -05:00
|
|
|
scan:
|
2019-07-16 13:27:11 -05:00
|
|
|
for i := 0; i < len(input); i++ {
|
2020-03-08 16:36:59 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
if input[i] != phOpen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:36:08 -05:00
|
|
|
// our iterator is now on an unescaped open brace (start of placeholder)
|
|
|
|
|
|
|
|
// too many unclosed placeholders in absolutely ridiculous input can be extremely slow (issue #4170)
|
|
|
|
if unclosedCount > 100 {
|
|
|
|
return "", fmt.Errorf("too many unclosed placeholders")
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
// find the end of the placeholder
|
|
|
|
end := strings.Index(input[i:], string(phClose)) + i
|
2019-11-11 21:29:31 -05:00
|
|
|
if end < i {
|
2022-09-15 14:36:08 -05:00
|
|
|
unclosedCount++
|
2019-11-11 21:29:31 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:36:59 -05:00
|
|
|
// if necessary look for the first closing brace that is not escaped
|
|
|
|
for end > 0 && end < len(input)-1 && input[end-1] == phEscape {
|
2020-03-11 17:12:00 -05:00
|
|
|
nextEnd := strings.Index(input[end+1:], string(phClose))
|
|
|
|
if nextEnd < 0 {
|
2022-09-15 14:36:08 -05:00
|
|
|
unclosedCount++
|
2020-03-11 17:12:00 -05:00
|
|
|
continue scan
|
|
|
|
}
|
|
|
|
end += nextEnd + 1
|
2020-03-08 16:36:59 -05:00
|
|
|
}
|
|
|
|
|
2019-11-11 21:29:31 -05:00
|
|
|
// write the substring from the last cursor to this point
|
|
|
|
sb.WriteString(input[lastWriteCursor:i])
|
2019-07-16 13:27:11 -05:00
|
|
|
|
|
|
|
// trim opening bracket
|
|
|
|
key := input[i+1 : end]
|
|
|
|
|
2019-12-29 15:12:52 -05:00
|
|
|
// try to get a value for this key, handle empty values accordingly
|
|
|
|
val, found := r.Get(key)
|
2019-07-16 13:27:11 -05:00
|
|
|
if !found {
|
2019-12-29 15:12:52 -05:00
|
|
|
// placeholder is unknown (unrecognized); handle accordingly
|
|
|
|
if errOnUnknown {
|
2019-10-28 15:39:37 -05:00
|
|
|
return "", fmt.Errorf("unrecognized placeholder %s%s%s",
|
|
|
|
string(phOpen), key, string(phClose))
|
2020-02-26 00:00:33 -05:00
|
|
|
} else if !treatUnknownAsEmpty {
|
2020-03-30 12:49:53 -05:00
|
|
|
// if treatUnknownAsEmpty is true, we'll handle an empty
|
|
|
|
// val later; so only continue otherwise
|
2019-10-28 15:39:37 -05:00
|
|
|
lastWriteCursor = i
|
|
|
|
continue
|
|
|
|
}
|
2019-07-16 13:27:11 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:12:52 -05:00
|
|
|
// apply any transformations
|
|
|
|
if f != nil {
|
|
|
|
var err error
|
|
|
|
val, err = f(key, val)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 12:49:53 -05:00
|
|
|
// convert val to a string as efficiently as possible
|
2022-09-01 22:15:20 -05:00
|
|
|
valStr := ToString(val)
|
2020-03-30 12:49:53 -05:00
|
|
|
|
2019-12-29 15:12:52 -05:00
|
|
|
// write the value; if it's empty, either return
|
|
|
|
// an error or write a default value
|
2020-03-30 12:49:53 -05:00
|
|
|
if valStr == "" {
|
2019-12-29 15:12:52 -05:00
|
|
|
if errOnEmpty {
|
|
|
|
return "", fmt.Errorf("evaluated placeholder %s%s%s is empty",
|
|
|
|
string(phOpen), key, string(phClose))
|
|
|
|
} else if empty != "" {
|
|
|
|
sb.WriteString(empty)
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-30 12:49:53 -05:00
|
|
|
sb.WriteString(valStr)
|
2019-12-29 15:12:52 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
// advance cursor to end of placeholder
|
2019-07-21 10:57:34 -05:00
|
|
|
i = end
|
|
|
|
lastWriteCursor = i + 1
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
2019-07-16 13:27:11 -05:00
|
|
|
|
|
|
|
// flush any unwritten remainder
|
|
|
|
sb.WriteString(input[lastWriteCursor:])
|
|
|
|
|
2019-10-28 15:39:37 -05:00
|
|
|
return sb.String(), nil
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2022-09-01 22:15:20 -05:00
|
|
|
// ToString returns val as a string, as efficiently as possible.
|
|
|
|
// EXPERIMENTAL: may be changed or removed later.
|
|
|
|
func ToString(val any) string {
|
2020-03-30 12:49:53 -05:00
|
|
|
switch v := val.(type) {
|
|
|
|
case nil:
|
|
|
|
return ""
|
|
|
|
case string:
|
|
|
|
return v
|
|
|
|
case fmt.Stringer:
|
|
|
|
return v.String()
|
2022-09-01 17:49:18 -05:00
|
|
|
case error:
|
|
|
|
return v.Error()
|
2020-03-30 12:49:53 -05:00
|
|
|
case byte:
|
|
|
|
return string(v)
|
|
|
|
case []byte:
|
|
|
|
return string(v)
|
|
|
|
case []rune:
|
|
|
|
return string(v)
|
|
|
|
case int:
|
|
|
|
return strconv.Itoa(v)
|
|
|
|
case int32:
|
|
|
|
return strconv.Itoa(int(v))
|
|
|
|
case int64:
|
|
|
|
return strconv.Itoa(int(v))
|
|
|
|
case uint:
|
|
|
|
return strconv.Itoa(int(v))
|
|
|
|
case uint32:
|
|
|
|
return strconv.Itoa(int(v))
|
|
|
|
case uint64:
|
|
|
|
return strconv.Itoa(int(v))
|
|
|
|
case float32:
|
|
|
|
return strconv.FormatFloat(float64(v), 'f', -1, 32)
|
|
|
|
case float64:
|
|
|
|
return strconv.FormatFloat(v, 'f', -1, 64)
|
|
|
|
case bool:
|
|
|
|
if v {
|
|
|
|
return "true"
|
|
|
|
}
|
|
|
|
return "false"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%+v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:29:09 -05:00
|
|
|
// ReplacerFunc is a function that returns a replacement
|
2019-07-16 13:27:11 -05:00
|
|
|
// 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.
|
2022-08-02 15:39:09 -05:00
|
|
|
type ReplacerFunc func(key string) (any, bool)
|
2019-07-16 13:27:11 -05:00
|
|
|
|
2022-08-02 15:39:09 -05:00
|
|
|
func globalDefaultReplacements(key string) (any, bool) {
|
2019-07-16 13:27:11 -05:00
|
|
|
// check environment variable
|
|
|
|
const envPrefix = "env."
|
|
|
|
if strings.HasPrefix(key, envPrefix) {
|
2019-10-28 15:39:37 -05:00
|
|
|
return os.Getenv(key[len(envPrefix):]), true
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:27:11 -05:00
|
|
|
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
|
2022-10-07 04:54:41 -05:00
|
|
|
case "system.wd":
|
|
|
|
// OK if there is an error; just return empty string
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
return wd, true
|
2019-07-16 13:27:11 -05:00
|
|
|
case "system.arch":
|
|
|
|
return runtime.GOARCH, true
|
2020-05-21 19:19:01 -05:00
|
|
|
case "time.now":
|
|
|
|
return nowFunc(), true
|
2023-03-27 15:51:13 -05:00
|
|
|
case "time.now.http":
|
2023-08-22 01:41:25 -05:00
|
|
|
// According to the comment for http.TimeFormat, the timezone must be in UTC
|
|
|
|
// to generate the correct format.
|
|
|
|
// https://github.com/caddyserver/caddy/issues/5773
|
|
|
|
return nowFunc().UTC().Format(http.TimeFormat), true
|
2019-10-10 16:38:30 -05:00
|
|
|
case "time.now.common_log":
|
|
|
|
return nowFunc().Format("02/Jan/2006:15:04:05 -0700"), true
|
2020-03-13 12:06:08 -05:00
|
|
|
case "time.now.year":
|
|
|
|
return strconv.Itoa(nowFunc().Year()), true
|
2021-08-16 16:06:44 -05:00
|
|
|
case "time.now.unix":
|
|
|
|
return strconv.FormatInt(nowFunc().Unix(), 10), true
|
|
|
|
case "time.now.unix_ms":
|
|
|
|
return strconv.FormatInt(nowFunc().UnixNano()/int64(time.Millisecond), 10), true
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2020-03-30 12:49:53 -05:00
|
|
|
return nil, false
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 18:29:09 -05:00
|
|
|
// 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.
|
2022-08-02 15:39:09 -05:00
|
|
|
type ReplacementFunc func(variable string, val any) (any, error)
|
2019-12-17 18:29:09 -05:00
|
|
|
|
2019-10-10 16:38:30 -05:00
|
|
|
// nowFunc is a variable so tests can change it
|
|
|
|
// in order to obtain a deterministic time.
|
|
|
|
var nowFunc = time.Now
|
|
|
|
|
2019-05-20 11:59:20 -05:00
|
|
|
// ReplacerCtxKey is the context key for a replacer.
|
|
|
|
const ReplacerCtxKey CtxKey = "replacer"
|
|
|
|
|
2020-03-08 16:36:59 -05:00
|
|
|
const phOpen, phClose, phEscape = '{', '}', '\\'
|