mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-18 10:29:43 +00:00
974229681f
Adds basic support for `_redirects` files. It supports a subset of what IPFS supports: https://docs.ipfs.tech/how-to/websites-on-ipfs/redirects-and-custom-404s/ Example: ``` /redirect https://example.com/ 301 /another-redirect /page 301 /302 https://example.com/ 302 /app/* /index.html 200 /articles/* /posts/:splat 301 ``` 301 redirect: https://video-prize-ranch.localhost.mock.directory:4430/redirect SPA rewrite: https://video-prize-ranch.localhost.mock.directory:4430/app/path/path Catch-all with splat: https://video-prize-ranch.localhost.mock.directory:4430/articles/path/path Closes #46 Co-authored-by: video-prize-ranch <cb.8a3w5@simplelogin.co> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/148 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: video-prize-ranch <video-prize-ranch@noreply.codeberg.org> Co-committed-by: video-prize-ranch <video-prize-ranch@noreply.codeberg.org>
72 lines
2.5 KiB
Go
72 lines
2.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"codeberg.org/codeberg/pages/html"
|
|
"codeberg.org/codeberg/pages/server/cache"
|
|
"codeberg.org/codeberg/pages/server/context"
|
|
"codeberg.org/codeberg/pages/server/dns"
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
|
"codeberg.org/codeberg/pages/server/upstream"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func handleCustomDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
|
|
mainDomainSuffix string,
|
|
trimmedHost string,
|
|
pathElements []string,
|
|
firstDefaultBranch string,
|
|
dnsLookupCache, canonicalDomainCache, redirectsCache cache.SetGetKey,
|
|
) {
|
|
// Serve pages from custom domains
|
|
targetOwner, targetRepo, targetBranch := dns.GetTargetFromDNS(trimmedHost, mainDomainSuffix, firstDefaultBranch, dnsLookupCache)
|
|
if targetOwner == "" {
|
|
html.ReturnErrorPage(ctx,
|
|
"could not obtain repo owner from custom domain",
|
|
http.StatusFailedDependency)
|
|
return
|
|
}
|
|
|
|
pathParts := pathElements
|
|
canonicalLink := false
|
|
if strings.HasPrefix(pathElements[0], "@") {
|
|
targetBranch = pathElements[0][1:]
|
|
pathParts = pathElements[1:]
|
|
canonicalLink = true
|
|
}
|
|
|
|
// Try to use the given repo on the given branch or the default branch
|
|
log.Debug().Msg("custom domain preparations, now trying with details from DNS")
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
TryIndexPages: true,
|
|
TargetOwner: targetOwner,
|
|
TargetRepo: targetRepo,
|
|
TargetBranch: targetBranch,
|
|
TargetPath: path.Join(pathParts...),
|
|
}, canonicalLink); works {
|
|
canonicalDomain, valid := targetOpt.CheckCanonicalDomain(giteaClient, trimmedHost, mainDomainSuffix, canonicalDomainCache)
|
|
if !valid {
|
|
html.ReturnErrorPage(ctx, "domain not specified in <code>.domains</code> file", http.StatusMisdirectedRequest)
|
|
return
|
|
} else if canonicalDomain != trimmedHost {
|
|
// only redirect if the target is also a codeberg page!
|
|
targetOwner, _, _ = dns.GetTargetFromDNS(strings.SplitN(canonicalDomain, "/", 2)[0], mainDomainSuffix, firstDefaultBranch, dnsLookupCache)
|
|
if targetOwner != "" {
|
|
ctx.Redirect("https://"+canonicalDomain+"/"+targetOpt.TargetPath, http.StatusTemporaryRedirect)
|
|
return
|
|
}
|
|
|
|
html.ReturnErrorPage(ctx, "target is no codeberg page", http.StatusFailedDependency)
|
|
return
|
|
}
|
|
|
|
log.Debug().Msg("tryBranch, now trying upstream 7")
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
|
return
|
|
}
|
|
|
|
html.ReturnErrorPage(ctx, "could not find target for custom domain", http.StatusFailedDependency)
|
|
}
|