mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +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.
|
// Add host for debugging.
|
||||||
options.Host = trimmedHost
|
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
|
// 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)
|
html.ReturnErrorPage(ctx, "", ctx.StatusCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,9 +25,11 @@ const redirectsConfig = "_redirects"
|
||||||
func (o *Options) GetRedirects(giteaClient *gitea.Client, redirectsCache cache.SetGetKey) []Redirect {
|
func (o *Options) GetRedirects(giteaClient *gitea.Client, redirectsCache cache.SetGetKey) []Redirect {
|
||||||
var redirects []Redirect
|
var redirects []Redirect
|
||||||
|
|
||||||
|
// Check for cached redirects
|
||||||
if cachedValue, ok := redirectsCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
|
if cachedValue, ok := redirectsCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
|
||||||
redirects = cachedValue.([]Redirect)
|
redirects = cachedValue.([]Redirect)
|
||||||
} else {
|
} else {
|
||||||
|
// Get _redirects file and parse
|
||||||
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, redirectsConfig)
|
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, redirectsConfig)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, line := range strings.Split(string(body), "\n") {
|
for _, line := range strings.Split(string(body), "\n") {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"codeberg.org/codeberg/pages/html"
|
"codeberg.org/codeberg/pages/html"
|
||||||
|
"codeberg.org/codeberg/pages/server/cache"
|
||||||
"codeberg.org/codeberg/pages/server/context"
|
"codeberg.org/codeberg/pages/server/context"
|
||||||
"codeberg.org/codeberg/pages/server/gitea"
|
"codeberg.org/codeberg/pages/server/gitea"
|
||||||
)
|
)
|
||||||
|
@ -52,7 +53,7 @@ type Options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
||||||
func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (final bool) {
|
func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redirectsCache cache.SetGetKey) (final bool) {
|
||||||
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
||||||
|
|
||||||
if o.TargetOwner == "" || o.TargetRepo == "" {
|
if o.TargetOwner == "" || o.TargetRepo == "" {
|
||||||
|
@ -110,7 +111,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
optionsForIndexPages.appendTrailingSlash = true
|
optionsForIndexPages.appendTrailingSlash = true
|
||||||
for _, indexPage := range upstreamIndexPages {
|
for _, indexPage := range upstreamIndexPages {
|
||||||
optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage
|
optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage
|
||||||
if optionsForIndexPages.Upstream(ctx, giteaClient) {
|
if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,7 +119,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
optionsForIndexPages.appendTrailingSlash = false
|
optionsForIndexPages.appendTrailingSlash = false
|
||||||
optionsForIndexPages.redirectIfExists = strings.TrimSuffix(ctx.Path(), "/") + ".html"
|
optionsForIndexPages.redirectIfExists = strings.TrimSuffix(ctx.Path(), "/") + ".html"
|
||||||
optionsForIndexPages.TargetPath = o.TargetPath + ".html"
|
optionsForIndexPages.TargetPath = o.TargetPath + ".html"
|
||||||
if optionsForIndexPages.Upstream(ctx, giteaClient) {
|
if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,11 +132,60 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
optionsForNotFoundPages.appendTrailingSlash = false
|
optionsForNotFoundPages.appendTrailingSlash = false
|
||||||
for _, notFoundPage := range upstreamNotFoundPages {
|
for _, notFoundPage := range upstreamNotFoundPages {
|
||||||
optionsForNotFoundPages.TargetPath = "/" + notFoundPage
|
optionsForNotFoundPages.TargetPath = "/" + notFoundPage
|
||||||
if optionsForNotFoundPages.Upstream(ctx, giteaClient) {
|
if optionsForNotFoundPages.Upstream(ctx, giteaClient, redirectsCache) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for redirects
|
||||||
|
redirects := o.GetRedirects(giteaClient, redirectsCache)
|
||||||
|
if len(redirects) > 0 {
|
||||||
|
for _, redirect := range redirects {
|
||||||
|
reqUrl := ctx.Req.RequestURI
|
||||||
|
|
||||||
|
// check if from url matches request url
|
||||||
|
if strings.TrimSuffix(redirect.From, "/") == strings.TrimSuffix(reqUrl, "/") {
|
||||||
|
// do rewrite if status code is 200
|
||||||
|
if redirect.StatusCode == 200 {
|
||||||
|
o.TargetPath = redirect.To
|
||||||
|
o.Upstream(ctx, giteaClient, redirectsCache)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
ctx.Redirect(redirect.To, redirect.StatusCode)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle wildcard redirects
|
||||||
|
trimmedFromUrl := strings.TrimSuffix(redirect.From, "/*")
|
||||||
|
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))
|
||||||
|
// do rewrite if status code is 200
|
||||||
|
if redirect.StatusCode == 200 {
|
||||||
|
o.TargetPath = splatUrl
|
||||||
|
o.Upstream(ctx, giteaClient, redirectsCache)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
ctx.Redirect(splatUrl, redirect.StatusCode)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// do rewrite if status code is 200
|
||||||
|
if redirect.StatusCode == 200 {
|
||||||
|
o.TargetPath = redirect.To
|
||||||
|
o.Upstream(ctx, giteaClient, redirectsCache)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
ctx.Redirect(redirect.To, redirect.StatusCode)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue