forked from samvaughton/caddy-netlify-redirects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
174 lines (142 loc) · 3.96 KB
/
matcher.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
170
171
172
173
174
package naddy
import (
"errors"
"fmt"
"github.com/tj/go-redirects"
"github.com/ucarion/urlpath"
"net/url"
"strings"
)
func ParseUrlWithContext(urlStr string, ctx *MatchContext) (*url.URL, error) {
original := urlStr
if !strings.Contains(urlStr, "http://") && !strings.Contains(urlStr, "https://") {
urlStr = fmt.Sprintf("%s%s", ctx.Scheme, urlStr)
// use url parse to test if it has a Host, if it doesn't revert back to original
testStr, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
if testStr.Host == "" {
urlStr = original
}
}
return url.Parse(urlStr)
}
func MatchUrlToRule(rule redirects.Rule, reqUrl *url.URL, ctx *MatchContext) MatchResult {
if reqUrl.Host == "" || reqUrl.Scheme == "" {
return MatchResult{
ResolvedTo: nil,
IsMatched: false,
IsHostRedirect: false,
Error: errors.New("request url must have both host and scheme"),
}
}
/*
* Perform the match as soon as possible on the path itself, as we may need the resolved To
*/
from, errFrom := ParseUrlWithContext(rule.From, ctx)
if errFrom != nil {
return MatchResult{
ResolvedTo: nil,
IsMatched: false,
IsHostRedirect: false,
Error: errFrom,
}
}
path := urlpath.New(strings.Trim(from.Path, "/"))
matched, ok := path.Match(strings.Trim(reqUrl.Path, "/"))
if !ok {
return MatchResult{
ResolvedTo: nil,
IsMatched: false,
IsHostRedirect: false,
}
}
toPath := rule.To
toPath = replaceParams(toPath, matched)
toPath = replaceSplat(toPath, matched)
to, errTo := ParseUrlWithContext(toPath, ctx)
if errTo != nil {
return MatchResult{
ResolvedTo: nil,
IsMatched: false,
IsHostRedirect: false,
Error: errTo,
}
}
skipMatch := MatchResult{
ResolvedTo: to,
Match: &matched,
IsMatched: false,
IsHostRedirect: false,
}
/*
* If this rule has a query string element to it, we need to perform an identical match ONLY after the initial match of the path
*/
if from.RawQuery != "" && from.RawQuery == reqUrl.RawQuery {
return MatchResult{
ResolvedTo: to,
Match: &matched,
IsMatched: true,
IsHostRedirect: false,
Source: rule,
}
} else if from.RawQuery != "" {
return skipMatch
}
hostToHost := from.Host != "" && to.Host != ""
hostToRelative := from.Host != "" && to.Host == ""
relativeToHost := from.Host == "" && to.Host != ""
// dont need to redirect if on the same host, or no host on rule.To
isHostRedirect := to.Host != "" && to.Host != reqUrl.Host
if (hostToHost || hostToRelative) && from.Host != reqUrl.Host {
return skipMatch
}
if relativeToHost && to.Host == reqUrl.Host {
return skipMatch
}
specialToRules := strings.Split(rule.To, "|")
for _, sItem := range specialToRules {
if sItem == "$ENFORCE_TRAILING_SLASH" {
// check to make sure this isn't a file request
parts := strings.Split(ctx.OriginalUrl.Path, ".")
if
// make sure parts is greater than two, and then verify that the final element is one of these
len(parts) >= 2 &&
len(parts[len(parts)-1]) >= 2 &&
len(parts[len(parts)-1]) <= 5 {
return skipMatch
}
if strings.HasSuffix(ctx.OriginalUrl.Path, "/") == false {
// redirect
prefixedTo := reqUrl
prefixedTo.Path = fmt.Sprintf("%s/", prefixedTo.Path)
return MatchResult{
ResolvedTo: prefixedTo,
Match: &matched,
IsMatched: true,
IsHostRedirect: isHostRedirect,
}
}
return skipMatch
}
}
return MatchResult{
ResolvedTo: to,
Match: &matched,
IsMatched: true,
IsHostRedirect: isHostRedirect,
Source: rule,
}
}
func replaceParams(to string, matched urlpath.Match) string {
if len(matched.Params) > 0 {
for key, value := range matched.Params {
to = strings.ReplaceAll(to, ":"+key, value)
}
}
return to
}
func replaceSplat(to string, matched urlpath.Match) string {
return strings.ReplaceAll(to, ":splat", matched.Trailing)
}