2022-11-12 19:43:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2023-02-14 03:03:00 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2022-11-12 19:43:44 +00:00
|
|
|
|
|
|
|
"codeberg.org/codeberg/pages/html"
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
|
|
|
"codeberg.org/codeberg/pages/server/context"
|
|
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
|
|
|
"codeberg.org/codeberg/pages/server/upstream"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSubDomain(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
|
|
|
|
mainDomainSuffix string,
|
2023-02-14 03:03:00 +00:00
|
|
|
defaultPagesBranches []string,
|
2022-11-12 19:43:44 +00:00
|
|
|
trimmedHost string,
|
|
|
|
pathElements []string,
|
2023-03-30 21:36:31 +00:00
|
|
|
canonicalDomainCache, redirectsCache cache.SetGetKey,
|
2022-11-12 19:43:44 +00:00
|
|
|
) {
|
|
|
|
// Serve pages from subdomains of MainDomainSuffix
|
|
|
|
log.Debug().Msg("main domain suffix")
|
|
|
|
|
|
|
|
targetOwner := strings.TrimSuffix(trimmedHost, mainDomainSuffix)
|
|
|
|
targetRepo := pathElements[0]
|
|
|
|
|
|
|
|
if targetOwner == "www" {
|
|
|
|
// www.codeberg.page redirects to codeberg.page // TODO: rm hardcoded - use cname?
|
2022-11-15 15:15:11 +00:00
|
|
|
ctx.Redirect("https://"+mainDomainSuffix[1:]+ctx.Path(), http.StatusPermanentRedirect)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the first directory is a repo with the second directory as a branch
|
|
|
|
// example.codeberg.page/myrepo/@main/index.html
|
|
|
|
if len(pathElements) > 1 && strings.HasPrefix(pathElements[1], "@") {
|
2022-11-12 20:10:16 +00:00
|
|
|
if targetRepo == defaultPagesRepo {
|
2022-11-12 19:43:44 +00:00
|
|
|
// example.codeberg.org/pages/@... redirects to example.codeberg.org/@...
|
|
|
|
ctx.Redirect("/"+strings.Join(pathElements[1:], "/"), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("main domain preparations, now trying with specified repo & branch")
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: true,
|
|
|
|
TargetOwner: targetOwner,
|
|
|
|
TargetRepo: pathElements[0],
|
|
|
|
TargetBranch: pathElements[1][1:],
|
|
|
|
TargetPath: path.Join(pathElements[2:]...),
|
|
|
|
}, true); works {
|
|
|
|
log.Trace().Msg("tryUpstream: serve with specified repo and branch")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
} else {
|
2023-11-16 17:11:35 +00:00
|
|
|
html.ReturnErrorPage(
|
|
|
|
ctx,
|
|
|
|
formatSetBranchNotFoundMessage(targetOpt.TargetBranch, targetOpt.TargetOwner, targetOpt.TargetRepo),
|
|
|
|
http.StatusFailedDependency,
|
|
|
|
)
|
2022-11-12 19:43:44 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:10:16 +00:00
|
|
|
// Check if the first directory is a branch for the defaultPagesRepo
|
2022-11-12 19:43:44 +00:00
|
|
|
// example.codeberg.page/@main/index.html
|
|
|
|
if strings.HasPrefix(pathElements[0], "@") {
|
2023-02-14 03:03:00 +00:00
|
|
|
targetBranch := pathElements[0][1:]
|
|
|
|
|
|
|
|
// if the default pages branch can be determined exactly, it does not need to be set
|
|
|
|
if len(defaultPagesBranches) == 1 && slices.Contains(defaultPagesBranches, targetBranch) {
|
|
|
|
// example.codeberg.org/@pages/... redirects to example.codeberg.org/...
|
|
|
|
ctx.Redirect("/"+strings.Join(pathElements[1:], "/"), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 19:43:44 +00:00
|
|
|
log.Debug().Msg("main domain preparations, now trying with specified branch")
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: true,
|
|
|
|
TargetOwner: targetOwner,
|
2022-11-12 20:10:16 +00:00
|
|
|
TargetRepo: defaultPagesRepo,
|
2023-02-14 03:03:00 +00:00
|
|
|
TargetBranch: targetBranch,
|
2022-11-12 19:43:44 +00:00
|
|
|
TargetPath: path.Join(pathElements[1:]...),
|
|
|
|
}, true); works {
|
|
|
|
log.Trace().Msg("tryUpstream: serve default pages repo with specified branch")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
} else {
|
2023-11-16 17:11:35 +00:00
|
|
|
html.ReturnErrorPage(
|
|
|
|
ctx,
|
|
|
|
formatSetBranchNotFoundMessage(targetOpt.TargetBranch, targetOpt.TargetOwner, targetOpt.TargetRepo),
|
|
|
|
http.StatusFailedDependency,
|
|
|
|
)
|
2022-11-12 19:43:44 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-14 03:03:00 +00:00
|
|
|
for _, defaultPagesBranch := range defaultPagesBranches {
|
|
|
|
// Check if the first directory is a repo with a default pages branch
|
|
|
|
// example.codeberg.page/myrepo/index.html
|
|
|
|
// example.codeberg.page/{PAGES_BRANCHE}/... is not allowed here.
|
|
|
|
log.Debug().Msg("main domain preparations, now trying with specified repo")
|
|
|
|
if pathElements[0] != defaultPagesBranch {
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: true,
|
|
|
|
TargetOwner: targetOwner,
|
|
|
|
TargetRepo: pathElements[0],
|
|
|
|
TargetBranch: defaultPagesBranch,
|
|
|
|
TargetPath: path.Join(pathElements[1:]...),
|
|
|
|
}, false); works {
|
|
|
|
log.Debug().Msg("tryBranch, now trying upstream 5")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2023-02-14 03:03:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to use the defaultPagesRepo on an default pages branch
|
|
|
|
// example.codeberg.page/index.html
|
|
|
|
log.Debug().Msg("main domain preparations, now trying with default repo")
|
2022-11-12 19:43:44 +00:00
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: true,
|
|
|
|
TargetOwner: targetOwner,
|
2023-02-14 03:03:00 +00:00
|
|
|
TargetRepo: defaultPagesRepo,
|
2022-11-12 20:13:13 +00:00
|
|
|
TargetBranch: defaultPagesBranch,
|
2023-02-14 03:03:00 +00:00
|
|
|
TargetPath: path.Join(pathElements...),
|
2022-11-12 19:43:44 +00:00
|
|
|
}, false); works {
|
2023-02-14 03:03:00 +00:00
|
|
|
log.Debug().Msg("tryBranch, now trying upstream 6")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:10:16 +00:00
|
|
|
// Try to use the defaultPagesRepo on its default branch
|
2022-11-12 19:43:44 +00:00
|
|
|
// example.codeberg.page/index.html
|
|
|
|
log.Debug().Msg("main domain preparations, now trying with default repo/branch")
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: true,
|
|
|
|
TargetOwner: targetOwner,
|
2022-11-12 20:10:16 +00:00
|
|
|
TargetRepo: defaultPagesRepo,
|
2022-11-12 19:43:44 +00:00
|
|
|
TargetPath: path.Join(pathElements...),
|
|
|
|
}, false); works {
|
|
|
|
log.Debug().Msg("tryBranch, now trying upstream 6")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Couldn't find a valid repo/branch
|
|
|
|
html.ReturnErrorPage(ctx,
|
2023-11-16 17:11:35 +00:00
|
|
|
fmt.Sprintf("could not find a valid repository or branch for repository: <code>%s</code>", targetRepo),
|
2022-11-22 21:26:10 +00:00
|
|
|
http.StatusNotFound)
|
2022-11-12 19:43:44 +00:00
|
|
|
}
|
2023-11-16 17:11:35 +00:00
|
|
|
|
|
|
|
func formatSetBranchNotFoundMessage(branch, owner, repo string) string {
|
|
|
|
return fmt.Sprintf("explicitly set branch <code>%q</code> does not exist at <code>%s/%s</code>", branch, owner, repo)
|
|
|
|
}
|