2022-11-12 19:43:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2024-04-18 21:19:45 +00:00
|
|
|
"fmt"
|
2022-11-12 19:43:44 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// tryUpstream forwards the target request to the Gitea API, and shows an error page on failure.
|
|
|
|
func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
|
|
|
|
mainDomainSuffix, trimmedHost string,
|
|
|
|
options *upstream.Options,
|
2024-02-15 16:08:29 +00:00
|
|
|
canonicalDomainCache cache.ICache,
|
|
|
|
redirectsCache cache.ICache,
|
2022-11-12 19:43:44 +00:00
|
|
|
) {
|
|
|
|
// check if a canonical domain exists on a request on MainDomain
|
2023-02-11 03:12:42 +00:00
|
|
|
if strings.HasSuffix(trimmedHost, mainDomainSuffix) && !options.ServeRaw {
|
2022-11-15 15:15:11 +00:00
|
|
|
canonicalDomain, _ := options.CheckCanonicalDomain(giteaClient, "", mainDomainSuffix, canonicalDomainCache)
|
|
|
|
if !strings.HasSuffix(strings.SplitN(canonicalDomain, "/", 2)[0], mainDomainSuffix) {
|
2022-11-12 19:43:44 +00:00
|
|
|
canonicalPath := ctx.Req.RequestURI
|
2022-11-12 20:16:11 +00:00
|
|
|
if options.TargetRepo != defaultPagesRepo {
|
2022-11-12 19:43:44 +00:00
|
|
|
path := strings.SplitN(canonicalPath, "/", 3)
|
|
|
|
if len(path) >= 3 {
|
|
|
|
canonicalPath = "/" + path[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Redirect("https://"+canonicalDomain+canonicalPath, http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 15:15:11 +00:00
|
|
|
// Add host for debugging.
|
|
|
|
options.Host = trimmedHost
|
2022-11-12 19:43:44 +00:00
|
|
|
|
|
|
|
// Try to request the file from the Gitea API
|
2023-03-30 21:36:31 +00:00
|
|
|
if !options.Upstream(ctx, giteaClient, redirectsCache) {
|
2024-04-18 21:19:45 +00:00
|
|
|
html.ReturnErrorPage(ctx, fmt.Sprintf("Forge returned %d %s", ctx.StatusCode, http.StatusText(ctx.StatusCode)), ctx.StatusCode)
|
2022-11-12 19:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tryBranch checks if a branch exists and populates the target variables. If canonicalLink is non-empty,
|
|
|
|
// it will also disallow search indexing and add a Link header to the canonical URL.
|
|
|
|
func tryBranch(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
|
|
|
|
targetOptions *upstream.Options, canonicalLink bool,
|
|
|
|
) (*upstream.Options, bool) {
|
|
|
|
if targetOptions.TargetOwner == "" || targetOptions.TargetRepo == "" {
|
|
|
|
log.Debug().Msg("tryBranch: owner or repo is empty")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "~" to "/" so we can access branch that contains slash character
|
|
|
|
// Branch name cannot contain "~" so doing this is okay
|
|
|
|
targetOptions.TargetBranch = strings.ReplaceAll(targetOptions.TargetBranch, "~", "/")
|
|
|
|
|
|
|
|
// Check if the branch exists, otherwise treat it as a file path
|
|
|
|
branchExist, _ := targetOptions.GetBranchTimestamp(giteaClient)
|
|
|
|
if !branchExist {
|
|
|
|
log.Debug().Msg("tryBranch: branch doesn't exist")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if canonicalLink {
|
|
|
|
// Hide from search machines & add canonical link
|
|
|
|
ctx.RespWriter.Header().Set("X-Robots-Tag", "noarchive, noindex")
|
|
|
|
ctx.RespWriter.Header().Set("Link", targetOptions.ContentWebLink(giteaClient)+"; rel=\"canonical\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("tryBranch: true")
|
|
|
|
return targetOptions, true
|
|
|
|
}
|