2019-05-04 14:21:20 -05:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
2019-05-07 10:56:13 -05:00
|
|
|
"net"
|
2019-05-04 14:21:20 -05:00
|
|
|
"net/http"
|
2019-05-07 10:56:13 -05:00
|
|
|
"os"
|
2019-05-04 14:21:20 -05:00
|
|
|
"strings"
|
2019-05-14 15:14:05 -05:00
|
|
|
|
|
|
|
"bitbucket.org/lightcodelabs/caddy2"
|
2019-05-04 14:21:20 -05:00
|
|
|
)
|
|
|
|
|
2019-05-07 10:56:13 -05:00
|
|
|
// Replacer can replace values in strings based
|
2019-05-10 22:07:02 -05:00
|
|
|
// on a request and/or response writer. The zero
|
2019-05-14 15:14:05 -05:00
|
|
|
// Replacer is not valid; use NewReplacer() to
|
|
|
|
// initialize one.
|
2019-05-04 14:21:20 -05:00
|
|
|
type Replacer struct {
|
|
|
|
req *http.Request
|
|
|
|
resp http.ResponseWriter
|
|
|
|
custom map[string]string
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:14:05 -05:00
|
|
|
// NewReplacer makes a new Replacer, initializing all necessary
|
|
|
|
// fields. The request and response writer are optional, but
|
|
|
|
// necessary for most replacements to work.
|
|
|
|
func NewReplacer(req *http.Request, rw http.ResponseWriter) *Replacer {
|
|
|
|
return &Replacer{
|
|
|
|
req: req,
|
|
|
|
resp: rw,
|
|
|
|
custom: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 14:21:20 -05:00
|
|
|
// Map sets a custom variable mapping to a value.
|
|
|
|
func (r *Replacer) Map(variable, value string) {
|
|
|
|
r.custom[variable] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace replaces placeholders in input with the value. If
|
|
|
|
// the value is empty string, the placeholder is substituted
|
|
|
|
// with the value empty.
|
|
|
|
func (r *Replacer) Replace(input, empty string) string {
|
|
|
|
if !strings.Contains(input, phOpen) {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
input = r.replaceAll(input, empty, r.defaults())
|
|
|
|
input = r.replaceAll(input, empty, r.custom)
|
|
|
|
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Replacer) replaceAll(input, empty string, mapping map[string]string) string {
|
|
|
|
for key, val := range mapping {
|
|
|
|
if val == "" {
|
|
|
|
val = empty
|
|
|
|
}
|
|
|
|
input = strings.ReplaceAll(input, phOpen+key+phClose, val)
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Replacer) defaults() map[string]string {
|
|
|
|
m := map[string]string{
|
2019-05-14 15:14:05 -05:00
|
|
|
"system.hostname": func() string {
|
|
|
|
// OK if there is an error; just return empty string
|
|
|
|
name, _ := os.Hostname()
|
|
|
|
return name
|
|
|
|
}(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.req != nil {
|
|
|
|
m["request.host"] = func() string {
|
2019-05-07 10:56:13 -05:00
|
|
|
host, _, err := net.SplitHostPort(r.req.Host)
|
|
|
|
if err != nil {
|
|
|
|
return r.req.Host // OK; there probably was no port
|
|
|
|
}
|
|
|
|
return host
|
2019-05-14 15:14:05 -05:00
|
|
|
}()
|
|
|
|
m["request.hostport"] = r.req.Host // may include both host and port
|
|
|
|
m["request.method"] = r.req.Method
|
|
|
|
m["request.port"] = func() string {
|
2019-05-07 10:56:13 -05:00
|
|
|
// if there is no port, there will be an error; in
|
|
|
|
// that case, port is the empty string anyway
|
|
|
|
_, port, _ := net.SplitHostPort(r.req.Host)
|
|
|
|
return port
|
2019-05-14 15:14:05 -05:00
|
|
|
}()
|
|
|
|
m["request.scheme"] = func() string {
|
2019-05-04 14:21:20 -05:00
|
|
|
if r.req.TLS != nil {
|
|
|
|
return "https"
|
|
|
|
}
|
|
|
|
return "http"
|
2019-05-14 15:14:05 -05:00
|
|
|
}()
|
|
|
|
m["request.uri"] = r.req.URL.RequestURI()
|
|
|
|
m["request.uri.path"] = r.req.URL.Path
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-05-14 15:14:05 -05:00
|
|
|
for field, vals := range r.req.Header {
|
2019-05-16 12:46:17 -05:00
|
|
|
m["request.header."+strings.ToLower(field)] = strings.Join(vals, ",")
|
2019-05-14 15:14:05 -05:00
|
|
|
}
|
|
|
|
for _, cookie := range r.req.Cookies() {
|
2019-05-16 12:46:17 -05:00
|
|
|
m["request.cookie."+cookie.Name] = cookie.Value
|
2019-05-14 15:14:05 -05:00
|
|
|
}
|
|
|
|
for param, vals := range r.req.URL.Query() {
|
2019-05-16 12:46:17 -05:00
|
|
|
m["request.uri.query."+param] = strings.Join(vals, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.resp != nil {
|
|
|
|
for field, vals := range r.resp.Header() {
|
|
|
|
m["response.header."+strings.ToLower(field)] = strings.Join(vals, ",")
|
2019-05-14 15:14:05 -05:00
|
|
|
}
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
const phOpen, phClose = "{", "}"
|
2019-05-14 15:14:05 -05:00
|
|
|
|
|
|
|
// ReplacerCtxKey is the context key for the request's replacer.
|
|
|
|
const ReplacerCtxKey caddy2.CtxKey = "replacer"
|