2023-03-30 21:36:31 +00:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
|
|
|
"codeberg.org/codeberg/pages/server/context"
|
|
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Redirect struct {
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
2024-04-18 21:03:16 +00:00
|
|
|
// rewriteURL returns the destination URL and true if r matches reqURL.
|
|
|
|
func (r *Redirect) rewriteURL(reqURL string) (dstURL string, ok bool) {
|
|
|
|
// check if from url matches request url
|
|
|
|
if strings.TrimSuffix(r.From, "/") == strings.TrimSuffix(reqURL, "/") {
|
|
|
|
return r.To, true
|
|
|
|
}
|
|
|
|
// handle wildcard redirects
|
2024-04-20 11:00:15 +00:00
|
|
|
if strings.HasSuffix(r.From, "/*") {
|
|
|
|
trimmedFromURL := strings.TrimSuffix(r.From, "/*")
|
|
|
|
if reqURL == trimmedFromURL || strings.HasPrefix(reqURL, trimmedFromURL+"/") {
|
|
|
|
if strings.Contains(r.To, ":splat") {
|
|
|
|
matched := strings.TrimPrefix(reqURL, trimmedFromURL)
|
|
|
|
matched = strings.TrimPrefix(matched, "/")
|
|
|
|
return strings.ReplaceAll(r.To, ":splat", matched), true
|
|
|
|
}
|
|
|
|
return r.To, true
|
2024-04-18 21:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2023-03-30 21:36:31 +00:00
|
|
|
// redirectsCacheTimeout specifies the timeout for the redirects cache.
|
|
|
|
var redirectsCacheTimeout = 10 * time.Minute
|
|
|
|
|
|
|
|
const redirectsConfig = "_redirects"
|
|
|
|
|
|
|
|
// getRedirects returns redirects specified in the _redirects file.
|
2024-02-15 16:08:29 +00:00
|
|
|
func (o *Options) getRedirects(giteaClient *gitea.Client, redirectsCache cache.ICache) []Redirect {
|
2023-03-30 21:36:31 +00:00
|
|
|
var redirects []Redirect
|
|
|
|
cacheKey := o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch
|
|
|
|
|
|
|
|
// Check for cached redirects
|
|
|
|
if cachedValue, ok := redirectsCache.Get(cacheKey); ok {
|
|
|
|
redirects = cachedValue.([]Redirect)
|
|
|
|
} else {
|
|
|
|
// Get _redirects file and parse
|
|
|
|
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, redirectsConfig)
|
|
|
|
if err == nil {
|
|
|
|
for _, line := range strings.Split(string(body), "\n") {
|
|
|
|
redirectArr := strings.Fields(line)
|
|
|
|
|
|
|
|
// Ignore comments and invalid lines
|
|
|
|
if strings.HasPrefix(line, "#") || len(redirectArr) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get redirect status code
|
|
|
|
statusCode := 301
|
|
|
|
if len(redirectArr) == 3 {
|
|
|
|
statusCode, err = strconv.Atoi(redirectArr[2])
|
|
|
|
if err != nil {
|
|
|
|
log.Info().Err(err).Msgf("could not read %s of %s/%s", redirectsConfig, o.TargetOwner, o.TargetRepo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
redirects = append(redirects, Redirect{
|
|
|
|
From: redirectArr[0],
|
|
|
|
To: redirectArr[1],
|
|
|
|
StatusCode: statusCode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = redirectsCache.Set(cacheKey, redirects, redirectsCacheTimeout)
|
|
|
|
}
|
|
|
|
return redirects
|
|
|
|
}
|
|
|
|
|
2024-02-15 16:08:29 +00:00
|
|
|
func (o *Options) matchRedirects(ctx *context.Context, giteaClient *gitea.Client, redirects []Redirect, redirectsCache cache.ICache) (final bool) {
|
2024-04-18 21:03:16 +00:00
|
|
|
reqURL := ctx.Req.RequestURI
|
|
|
|
// remove repo and branch from request url
|
|
|
|
reqURL = strings.TrimPrefix(reqURL, "/"+o.TargetRepo)
|
|
|
|
reqURL = strings.TrimPrefix(reqURL, "/@"+o.TargetBranch)
|
2023-03-30 21:36:31 +00:00
|
|
|
|
2024-04-18 21:03:16 +00:00
|
|
|
for _, redirect := range redirects {
|
|
|
|
if dstURL, ok := redirect.rewriteURL(reqURL); ok {
|
|
|
|
// do rewrite if status code is 200
|
|
|
|
if redirect.StatusCode == 200 {
|
|
|
|
o.TargetPath = dstURL
|
|
|
|
o.Upstream(ctx, giteaClient, redirectsCache)
|
|
|
|
} else {
|
|
|
|
ctx.Redirect(dstURL, redirect.StatusCode)
|
2023-03-30 21:36:31 +00:00
|
|
|
}
|
2024-04-18 21:03:16 +00:00
|
|
|
return true
|
2023-03-30 21:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|