summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/replacer.go
blob: 0b0547df3097f343229c9138352ebdd9428e867b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package caddyhttp

import (
	"fmt"
	"net"
	"net/http"
	"path"
	"strings"

	"bitbucket.org/lightcodelabs/caddy2"
)

// TODO: A simple way to format or escape or encode each value would be nice
// ... TODO: Should we just use templates? :-/ yeesh...

func addHTTPVarsToReplacer(repl caddy2.Replacer, req *http.Request, w http.ResponseWriter) {
	httpVars := func() map[string]string {
		m := make(map[string]string)
		if req != nil {
			m["http.request.host"] = func() string {
				host, _, err := net.SplitHostPort(req.Host)
				if err != nil {
					return req.Host // OK; there probably was no port
				}
				return host
			}()
			m["http.request.hostport"] = req.Host // may include both host and port
			m["http.request.method"] = req.Method
			m["http.request.port"] = func() string {
				// if there is no port, there will be an error; in
				// that case, port is the empty string anyway
				_, port, _ := net.SplitHostPort(req.Host)
				return port
			}()
			m["http.request.scheme"] = func() string {
				if req.TLS != nil {
					return "https"
				}
				return "http"
			}()
			m["http.request.uri"] = req.URL.RequestURI()
			m["http.request.uri.path"] = req.URL.Path
			m["http.request.uri.path.file"] = func() string {
				_, file := path.Split(req.URL.Path)
				return file
			}()
			m["http.request.uri.path.dir"] = func() string {
				dir, _ := path.Split(req.URL.Path)
				return dir
			}()
			m["http.request.uri.query"] = req.URL.RawQuery

			for param, vals := range req.URL.Query() {
				m["http.request.uri.query."+param] = strings.Join(vals, ",")
			}
			for field, vals := range req.Header {
				m["http.request.header."+strings.ToLower(field)] = strings.Join(vals, ",")
			}
			for _, cookie := range req.Cookies() {
				m["http.request.cookie."+cookie.Name] = cookie.Value
			}

			hostLabels := strings.Split(req.Host, ".")
			for i, label := range hostLabels {
				key := fmt.Sprintf("http.request.host.labels.%d", len(hostLabels)-i-1)
				m[key] = label
			}
		}

		if w != nil {
			for field, vals := range w.Header() {
				m["http.response.header."+strings.ToLower(field)] = strings.Join(vals, ",")
			}
		}

		return m
	}

	repl.Map(httpVars)
}