2022-11-12 18:44:04 +01:00
|
|
|
package handler
|
2021-12-05 18:17:28 +01:00
|
|
|
|
|
|
|
import (
|
2022-07-27 17:25:08 +02:00
|
|
|
"net/http"
|
2021-12-05 18:17:28 +01:00
|
|
|
"strings"
|
|
|
|
|
2022-11-12 14:10:04 +01:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2021-12-05 18:17:28 +01:00
|
|
|
"codeberg.org/codeberg/pages/html"
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
2022-07-27 17:25:08 +02:00
|
|
|
"codeberg.org/codeberg/pages/server/context"
|
2022-06-11 23:02:06 +02:00
|
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
2021-12-05 18:17:28 +01:00
|
|
|
"codeberg.org/codeberg/pages/server/upstream"
|
|
|
|
)
|
|
|
|
|
|
|
|
// tryUpstream forwards the target request to the Gitea API, and shows an error page on failure.
|
2022-07-27 17:25:08 +02:00
|
|
|
func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
|
2022-08-28 16:21:37 +02:00
|
|
|
mainDomainSuffix, trimmedHost string,
|
2022-11-12 17:50:08 +01:00
|
|
|
options *upstream.Options,
|
2022-07-27 15:39:46 +02:00
|
|
|
canonicalDomainCache cache.SetGetKey,
|
2022-03-27 21:54:06 +02:00
|
|
|
) {
|
2021-12-05 18:17:28 +01:00
|
|
|
// check if a canonical domain exists on a request on MainDomain
|
2022-08-28 16:21:37 +02:00
|
|
|
if strings.HasSuffix(trimmedHost, mainDomainSuffix) {
|
2022-11-12 19:00:03 +01:00
|
|
|
canonicalDomain, _ := options.CheckCanonicalDomain(giteaClient, "", string(mainDomainSuffix), canonicalDomainCache)
|
2021-12-05 18:17:28 +01:00
|
|
|
if !strings.HasSuffix(strings.SplitN(canonicalDomain, "/", 2)[0], string(mainDomainSuffix)) {
|
2022-07-27 17:25:08 +02:00
|
|
|
canonicalPath := ctx.Req.RequestURI
|
2022-11-12 17:50:08 +01:00
|
|
|
if options.TargetRepo != "pages" {
|
2021-12-09 19:32:30 +01:00
|
|
|
path := strings.SplitN(canonicalPath, "/", 3)
|
|
|
|
if len(path) >= 3 {
|
2021-12-09 20:16:43 +01:00
|
|
|
canonicalPath = "/" + path[2]
|
2021-12-09 19:32:30 +01:00
|
|
|
}
|
2021-12-05 18:17:28 +01:00
|
|
|
}
|
2022-07-27 17:25:08 +02:00
|
|
|
ctx.Redirect("https://"+canonicalDomain+canonicalPath, http.StatusTemporaryRedirect)
|
2021-12-05 18:17:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 17:50:08 +01:00
|
|
|
// add host for debugging
|
|
|
|
options.Host = string(trimmedHost)
|
2021-12-05 19:53:23 +01:00
|
|
|
|
2021-12-05 18:17:28 +01:00
|
|
|
// Try to request the file from the Gitea API
|
2022-11-12 17:50:08 +01:00
|
|
|
if !options.Upstream(ctx, giteaClient) {
|
2022-11-11 06:38:09 +01:00
|
|
|
html.ReturnErrorPage(ctx, "", ctx.StatusCode)
|
2021-12-05 18:17:28 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-11 19:03:17 +01:00
|
|
|
|
2022-11-12 02:54:56 +01: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,
|
2022-11-12 17:50:08 +01:00
|
|
|
targetOptions *upstream.Options, canonicalLink bool,
|
|
|
|
) (*upstream.Options, bool) {
|
|
|
|
if targetOptions.TargetOwner == "" || targetOptions.TargetRepo == "" {
|
|
|
|
log.Debug().Msg("tryBranch: owner or repo is empty")
|
2022-11-12 02:54:56 +01:00
|
|
|
return nil, false
|
2022-11-11 19:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "~" to "/" so we can access branch that contains slash character
|
|
|
|
// Branch name cannot contain "~" so doing this is okay
|
2022-11-12 17:50:08 +01:00
|
|
|
targetOptions.TargetBranch = strings.ReplaceAll(targetOptions.TargetBranch, "~", "/")
|
2022-11-11 19:03:17 +01:00
|
|
|
|
|
|
|
// Check if the branch exists, otherwise treat it as a file path
|
2022-11-12 17:50:08 +01:00
|
|
|
branchExist, _ := targetOptions.GetBranchTimestamp(giteaClient)
|
|
|
|
if !branchExist {
|
2022-11-11 19:03:17 +01:00
|
|
|
log.Debug().Msg("tryBranch: branch doesn't exist")
|
2022-11-12 02:54:56 +01:00
|
|
|
return nil, false
|
2022-11-11 19:03:17 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 02:54:56 +01:00
|
|
|
if canonicalLink {
|
2022-11-11 19:03:17 +01:00
|
|
|
// Hide from search machines & add canonical link
|
|
|
|
ctx.RespWriter.Header().Set("X-Robots-Tag", "noarchive, noindex")
|
2022-11-12 17:50:08 +01:00
|
|
|
ctx.RespWriter.Header().Set("Link", targetOptions.ContentWebLink(giteaClient)+"; rel=\"canonical\"")
|
2022-11-11 19:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("tryBranch: true")
|
2022-11-12 17:50:08 +01:00
|
|
|
return targetOptions, true
|
2022-11-11 19:03:17 +01:00
|
|
|
}
|