mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
Move redirects to upstream
This commit is contained in:
parent
ac5d6e38fa
commit
0169816854
3 changed files with 57 additions and 40 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue