-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_route.go
296 lines (261 loc) · 6.65 KB
/
calc_route.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package pgs
import (
"fmt"
"net/http"
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/picosh/pgs/storage"
"github.com/picosh/send/utils"
)
type HttpReply struct {
Filepath string
Query map[string]string
Status int
}
func expandRoute(projectName, fp string, status int) []*HttpReply {
if fp == "" {
fp = "/"
}
mimeType := storage.GetMimeType(fp)
fname := filepath.Base(fp)
fdir := filepath.Dir(fp)
fext := filepath.Ext(fp)
routes := []*HttpReply{}
if mimeType != "text/plain" {
return routes
}
if fext == ".txt" {
return routes
}
// we know it's a directory so send the index.html for it
if strings.HasSuffix(fp, "/") {
dirRoute := GetAssetFileName(&utils.FileEntry{
Filepath: filepath.Join(projectName, fp, "index.html"),
})
routes = append(
routes,
&HttpReply{Filepath: dirRoute, Status: status},
)
} else {
if fname == "." {
return routes
}
// pretty urls where we just append .html to base of fp
nameRoute := GetAssetFileName(&utils.FileEntry{
Filepath: filepath.Join(
projectName,
fdir,
fmt.Sprintf("%s.html", fname),
),
})
routes = append(
routes,
&HttpReply{Filepath: nameRoute, Status: status},
)
}
return routes
}
func checkIsRedirect(status int) bool {
return status >= 300 && status <= 399
}
func correlatePlaceholder(orig, pattern string) (string, string) {
origList := splitFp(orig)
patternList := splitFp(pattern)
nextList := []string{}
for idx, item := range patternList {
if len(origList) <= idx {
continue
}
if strings.HasPrefix(item, ":") {
nextList = append(nextList, origList[idx])
} else if strings.Contains(item, "*") {
nextList = append(nextList, strings.ReplaceAll(item, "*", "(.*)"))
} else if item == origList[idx] {
nextList = append(nextList, origList[idx])
} else {
nextList = append(nextList, item)
// if we are on the last pattern item then we need to ensure
// it matches the end of string so partial matches are not counted
if idx == len(patternList)-1 {
// regex end of string matcher
nextList = append(nextList, "$")
}
}
}
_type := "none"
if len(nextList) > 0 && len(nextList) == len(patternList) {
_type = "match"
} else if strings.Contains(pattern, "*") {
_type = "wildcard"
if pattern == "/*" {
nextList = append(nextList, ".*")
}
} else if strings.Contains(pattern, ":") {
_type = "variable"
}
return filepath.Join(nextList...), _type
}
func splitFp(str string) []string {
ls := strings.Split(str, "/")
fin := []string{}
for _, l := range ls {
if l == "" {
continue
}
fin = append(fin, l)
}
return fin
}
func genRedirectRoute(actual string, fromStr string, to string) string {
if to == "/" {
return to
}
actualList := splitFp(actual)
fromList := splitFp(fromStr)
prefix := ""
var toList []string
if hasProtocol(to) {
u, _ := url.Parse(to)
if u.Path == "" {
return to
}
toList = splitFp(u.Path)
prefix = u.Scheme + "://" + u.Host
} else {
toList = splitFp(to)
}
mapper := map[string]string{}
for idx, item := range fromList {
if len(actualList) < idx {
continue
}
if strings.HasPrefix(item, ":") {
mapper[item] = actualList[idx]
}
if strings.HasSuffix(item, "*") {
ls := actualList[idx:]
// if the * is part of other text in the segment (e.g. `/files*`)
// then we don't want to include "files" in the destination
if len(item) > 1 && len(actualList) > idx+1 {
ls = actualList[idx+1:]
}
// standalone splat
splat := strings.Join(ls, "/")
mapper[":splat"] = splat
// splat as a suffix to a string
place := strings.ReplaceAll(item, "*", ":splat")
mapper[place] = strings.Join(actualList[idx:], "/")
break
}
}
fin := []string{"/"}
for _, item := range toList {
if strings.HasSuffix(item, ":splat") {
fin = append(fin, mapper[item])
} else if mapper[item] != "" {
fin = append(fin, mapper[item])
} else {
fin = append(fin, item)
}
}
result := prefix + filepath.Join(fin...)
if !strings.HasSuffix(result, "/") && (strings.HasSuffix(to, "/") || strings.HasSuffix(actual, "/")) {
result += "/"
}
return result
}
func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpReply {
rts := []*HttpReply{}
if !strings.HasPrefix(fp, "/") {
fp = "/" + fp
}
// add route as-is without expansion
if !strings.HasSuffix(fp, "/") {
defRoute := GetAssetFileName(&utils.FileEntry{
Filepath: filepath.Join(projectName, fp),
})
rts = append(rts, &HttpReply{Filepath: defRoute, Status: http.StatusOK})
}
expts := expandRoute(projectName, fp, http.StatusOK)
rts = append(rts, expts...)
// user routes
for _, redirect := range userRedirects {
// this doesn't make sense so it is forbidden
if redirect.From == redirect.To {
continue
}
// hack: make suffix `/` optional when matching
from := filepath.Clean(redirect.From)
match := []string{}
fromMatcher, matcherType := correlatePlaceholder(fp, from)
switch matcherType {
case "match":
fallthrough
case "wildcard":
fallthrough
case "variable":
rr := regexp.MustCompile(fromMatcher)
match = rr.FindStringSubmatch(fp)
case "none":
fallthrough
default:
break
}
if len(match) > 0 && match[0] != "" {
isRedirect := checkIsRedirect(redirect.Status)
if !isRedirect && !hasProtocol(redirect.To) {
route := genRedirectRoute(fp, from, redirect.To)
// wipe redirect rules to prevent infinite loops
// as such we only support a single hop for user defined redirects
redirectRoutes := calcRoutes(projectName, route, []*RedirectRule{})
rts = append(rts, redirectRoutes...)
return rts
}
route := genRedirectRoute(fp, from, redirect.To)
userReply := []*HttpReply{}
var rule *HttpReply
if redirect.To != "" {
rule = &HttpReply{
Filepath: route,
Status: redirect.Status,
Query: redirect.Query,
}
userReply = append(userReply, rule)
}
if redirect.Force {
rts = userReply
} else {
rts = append(rts, userReply...)
}
if hasProtocol(redirect.To) {
// redirecting to another site so we should bail early
return rts
} else {
// quit after first match
break
}
}
}
// we might have a directory so add a trailing slash with a 301
// we can't check for file extention because route could have a dot
// and ext parsing gets confused
if !strings.HasSuffix(fp, "/") {
redirectRoute := GetAssetFileName(&utils.FileEntry{
Filepath: fp + "/",
})
rts = append(
rts,
&HttpReply{Filepath: redirectRoute, Status: http.StatusMovedPermanently},
)
}
notFound := &HttpReply{
Filepath: filepath.Join(projectName, "404.html"),
Status: http.StatusNotFound,
}
rts = append(rts,
notFound,
)
return rts
}