forked from caddyserver/caddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
169 lines (142 loc) · 3.62 KB
/
setup.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package redirect
import (
"net/http"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func init() {
caddy.RegisterPlugin("redir", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup configures a new Redirect middleware instance.
func setup(c *caddy.Controller) error {
rules, err := redirParse(c)
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return Redirect{Next: next, Rules: rules}
})
return nil
}
func redirParse(c *caddy.Controller) ([]Rule, error) {
var redirects []Rule
cfg := httpserver.GetConfig(c)
initRule := func(rule *Rule, defaultCode string, args []string) error {
if cfg.TLS.Enabled {
rule.FromScheme = "https"
} else {
rule.FromScheme = "http"
}
var (
from = "/"
to string
code = defaultCode
)
switch len(args) {
case 1:
// To specified (catch-all redirect)
// Not sure why user is doing this in a table, as it causes all other redirects to be ignored.
// As such, this feature remains undocumented.
to = args[0]
case 2:
// From and To specified
from = args[0]
to = args[1]
case 3:
// From, To, and Code specified
from = args[0]
to = args[1]
code = args[2]
default:
return c.ArgErr()
}
rule.FromPath = from
rule.To = to
if code == "meta" {
rule.Meta = true
code = defaultCode
}
if codeNumber, ok := httpRedirs[code]; ok {
rule.Code = codeNumber
} else {
return c.Errf("Invalid redirect code '%v'", code)
}
return nil
}
// checkAndSaveRule checks the rule for validity (except the redir code)
// and saves it if it's valid, or returns an error.
checkAndSaveRule := func(rule Rule) error {
if rule.FromPath == rule.To {
return c.Err("'from' and 'to' values of redirect rule cannot be the same")
}
for _, otherRule := range redirects {
if otherRule.FromPath == rule.FromPath {
return c.Errf("rule with duplicate 'from' value: %s -> %s", otherRule.FromPath, otherRule.To)
}
}
redirects = append(redirects, rule)
return nil
}
const initDefaultCode = "301"
for c.Next() {
args := c.RemainingArgs()
matcher, err := httpserver.SetupIfMatcher(c)
if err != nil {
return nil, err
}
var hadOptionalBlock bool
for c.NextBlock() {
if httpserver.IfMatcherKeyword(c) {
continue
}
hadOptionalBlock = true
rule := Rule{
RequestMatcher: matcher,
}
defaultCode := initDefaultCode
// Set initial redirect code
if len(args) == 1 {
defaultCode = args[0]
}
// RemainingArgs only gets the values after the current token, but in our
// case we want to include the current token to get an accurate count.
insideArgs := append([]string{c.Val()}, c.RemainingArgs()...)
err := initRule(&rule, defaultCode, insideArgs)
if err != nil {
return redirects, err
}
err = checkAndSaveRule(rule)
if err != nil {
return redirects, err
}
}
if !hadOptionalBlock {
rule := Rule{
RequestMatcher: matcher,
}
err := initRule(&rule, initDefaultCode, args)
if err != nil {
return redirects, err
}
err = checkAndSaveRule(rule)
if err != nil {
return redirects, err
}
}
}
return redirects, nil
}
// httpRedirs is a list of supported HTTP redirect codes.
var httpRedirs = map[string]int{
"300": http.StatusMultipleChoices,
"301": http.StatusMovedPermanently,
"302": http.StatusFound, // (NOT CORRECT for "Temporary Redirect", see 307)
"303": http.StatusSeeOther,
"304": http.StatusNotModified,
"305": http.StatusUseProxy,
"307": http.StatusTemporaryRedirect,
"308": 308, // Permanent Redirect (RFC 7238)
}