-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.go
More file actions
113 lines (100 loc) · 2.8 KB
/
Copy pathgenerate.go
File metadata and controls
113 lines (100 loc) · 2.8 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package routebuilder
import (
"bytes"
"fmt"
"regexp"
"strings"
"text/template"
)
type routingVCLData struct {
Routes []routingRouteData
}
type routingRouteData struct {
Hostnames []string
LabelName string
}
func hostnameToVCL(h string) string {
if !strings.Contains(h, "*") {
return fmt.Sprintf(`req.http.host == %q`, h)
}
segs := strings.Split(h, ".")
for i, seg := range segs {
if seg == "*" {
segs[i] = `[^.]+`
} else {
segs[i] = regexp.QuoteMeta(seg)
}
}
return fmt.Sprintf(`req.http.host ~ "^%s$"`, strings.Join(segs, `\.`))
}
func hostCondition(hosts []string) string {
parts := make([]string, len(hosts))
for i, h := range hosts {
parts[i] = hostnameToVCL(h)
}
return strings.Join(parts, " || ")
}
var routingTmpl = template.Must(template.New("routing").Funcs(template.FuncMap{
"hostCond": hostCondition,
}).Parse(`vcl 4.1;
import tls;
backend default none;
sub vcl_recv {
if (tls.is_tls()) {
set req.http.host = tls.authority();
} else if (req.http.host ~ "^\[") {
set req.http.host = regsub(req.http.host, "^\[([^\]]+)\](:\d+)?$", "[\1]");
} else {
set req.http.host = regsub(req.http.host, ":\d+$", "");
}
{{- range .Routes}}
if ({{hostCond .Hostnames}}) {
return(vcl({{.LabelName}}));
}
{{- end}}
return(synth(404, "No route matched"));
}
`))
// BuildRoutingVCL generates the routing VCL content for the given configurations.
func (b *Builder) BuildRoutingVCL(configs []VCLConfig) (string, error) {
for i, cfg := range configs {
if err := validateRoutingConfig(cfg); err != nil {
return "", fmt.Errorf("route %d: %w", i, err)
}
}
targets, err := b.vclProgramTargets(configs)
if err != nil {
return "", err
}
resolved, err := b.ResolveVCLTargets(targets)
if err != nil {
return "", err
}
entryVCL, err := b.entryRenderer(resolved)
return string(entryVCL), err
}
func defaultEntryRenderer(targets []ResolvedVCLTarget) ([]byte, error) {
routes := make([]routingRouteData, len(targets))
for i, target := range targets {
if len(target.Hostnames) == 0 {
return nil, fmt.Errorf("target %q: hostnames are required for default entry renderer", target.VCLPath)
}
routes[i] = routingRouteData{Hostnames: target.Hostnames, LabelName: target.LabelName}
}
return renderRoutingVCL(routes)
}
func renderRoutingVCL(routes []routingRouteData) ([]byte, error) {
var buf bytes.Buffer
data := routingVCLData{Routes: routes}
if err := routingTmpl.Execute(&buf, data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// quoteCLIArg wraps a Varnish CLI argument in double quotes, escaping backslash
// and double-quote characters. Prevents malformed cmdfiles when paths contain spaces.
func quoteCLIArg(s string) string {
s = strings.ReplaceAll(s, `\`, `\\`)
s = strings.ReplaceAll(s, `"`, `\"`)
return `"` + s + `"`
}