Move redirects to upstream

This commit is contained in:
video-prize-ranch 2023-02-25 21:30:05 -05:00
parent ac5d6e38fa
commit 0169816854
No known key found for this signature in database
3 changed files with 57 additions and 40 deletions

View file

@ -39,43 +39,8 @@ func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
// Add host for debugging.
options.Host = trimmedHost
// Check for redirects
redirects := options.GetRedirects(giteaClient, redirectsCache)
if len(redirects) > 0 {
for _, redirect := range redirects {
reqUrl := ctx.Req.RequestURI
trimmedFromUrl := strings.TrimSuffix(redirect.From, "/*")
if strings.TrimSuffix(redirect.From, "/") == strings.TrimSuffix(reqUrl, "/") {
if redirect.StatusCode == 200 {
options.TargetPath = redirect.To
} else {
ctx.Redirect(redirect.To, redirect.StatusCode)
return
}
}
if strings.HasSuffix(redirect.From, "/*") && strings.HasPrefix(reqUrl, trimmedFromUrl) {
if strings.Contains(redirect.To, ":splat") {
splatUrl := strings.ReplaceAll(redirect.To, ":splat", strings.TrimPrefix(reqUrl, trimmedFromUrl))
if redirect.StatusCode == 200 {
options.TargetPath = splatUrl
} else {
ctx.Redirect(splatUrl, redirect.StatusCode)
return
}
} else {
if redirect.StatusCode == 200 {
options.TargetPath = redirect.To
} else {
ctx.Redirect(redirect.To, redirect.StatusCode)
return
}
}
}
}
}
// Try to request the file from the Gitea API
if !options.Upstream(ctx, giteaClient) {
if !options.Upstream(ctx, giteaClient, redirectsCache) {
html.ReturnErrorPage(ctx, "", ctx.StatusCode)
}
}