This commit is contained in:
6543 2022-11-11 19:03:17 +01:00
parent 9a44e26da7
commit 6bc79e5ebf
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
2 changed files with 49 additions and 5 deletions

View file

@ -2,13 +2,16 @@ package server
import (
"net/http"
"path"
"strings"
"time"
"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"
"github.com/rs/zerolog"
)
// tryUpstream forwards the target request to the Gitea API, and shows an error page on failure.
@ -47,3 +50,43 @@ func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
html.ReturnErrorPage(ctx, "", ctx.StatusCode)
}
}
func tryBranch2(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
repoOwner, repoName, branch string, _path []string, canonicalLink string) (
targetRepo, targetPath, targetBranch string, branchTimestamp *time.Time,
works bool) {
if repoName == "" {
log.Debug().Msg("tryBranch: repo == ''")
return "", "", "", nil, false
}
// Replace "~" to "/" so we can access branch that contains slash character
// Branch name cannot contain "~" so doing this is okay
branch = strings.ReplaceAll(branch, "~", "/")
// Check if the branch exists, otherwise treat it as a file path
branchTimestampResult := upstream.GetBranchTimestamp(giteaClient, repoOwner, repoName, branch)
if branchTimestampResult == nil {
log.Debug().Msg("tryBranch: branch doesn't exist")
return "", "", "", nil, false
}
// Branch exists, use it
targetRepo = repoName
targetPath = path.Join(_path...)
targetBranch = branchTimestampResult.Branch
branchTimestamp = &branchTimestampResult.Timestamp
if canonicalLink != "" {
// Hide from search machines & add canonical link
ctx.RespWriter.Header().Set("X-Robots-Tag", "noarchive, noindex")
ctx.RespWriter.Header().Set("Link",
strings.NewReplacer("%b", targetBranch, "%p", targetPath).Replace(canonicalLink)+
"; rel=\"canonical\"",
)
}
log.Debug().Msg("tryBranch: true")
return
}