From 8e6c0c6c9f2f07ed526b0943ef123fdf1c15a24b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 12 Nov 2022 14:08:08 +0100 Subject: [PATCH] deduplicate --- server/handler.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/server/handler.go b/server/handler.go index 4006a96..a1a3c3c 100644 --- a/server/handler.go +++ b/server/handler.go @@ -88,6 +88,7 @@ func Handler(mainDomainSuffix, rawDomain string, } // Prepare request information to Gitea + pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/") targetOptions := &upstream.Options{ TryIndexPages: true, } @@ -100,25 +101,24 @@ func Handler(mainDomainSuffix, rawDomain string, targetOptions.TryIndexPages = false targetOptions.ServeRaw = true - pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/") if len(pathElements) < 2 { // https://{RawDomain}/{owner}/{repo}[/@{branch}]/{path} is required ctx.Redirect(rawInfoPage, http.StatusTemporaryRedirect) return } - targetOwner := pathElements[0] - targetRepo := pathElements[1] // raw.codeberg.org/example/myrepo/@main/index.html if len(pathElements) > 2 && strings.HasPrefix(pathElements[2], "@") { log.Debug().Msg("raw domain preparations, now trying with specified branch") newPath := path.Join(pathElements[3:]...) branch := pathElements[2][1:] - if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, branch, newPath, true); works { + repoOwner := pathElements[0] + repo := pathElements[1] + if timestampBranch, works := tryBranch(log, ctx, giteaClient, repoOwner, repo, branch, newPath, true); works { targetOptions.BranchTimestamp = timestampBranch.Timestamp log.Debug().Msg("tryBranch, now trying upstream 1") tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, - targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache) + targetOptions, repoOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache) return } log.Debug().Msg("missing branch info") @@ -127,22 +127,24 @@ func Handler(mainDomainSuffix, rawDomain string, } log.Debug().Msg("raw domain preparations, now trying with default branch") + repoOwner := pathElements[0] + repo := pathElements[1] newPath := path.Join(pathElements[2:]...) - if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, "", newPath, true); works { + if timestampBranch, works := tryBranch(log, ctx, giteaClient, repoOwner, repo, "", newPath, true); works { targetOptions.BranchTimestamp = timestampBranch.Timestamp log.Debug().Msg("tryBranch, now trying upstream 2") tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, - targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache) - } else { - log.Error().Msg("TODO: is this a bug?") + targetOptions, repoOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache) + return } + + html.ReturnErrorPage(ctx, fmt.Sprintf("raw domain could not find repo '%s/%s' or repo is empty", repoOwner, repo), http.StatusNotFound) return } else if strings.HasSuffix(trimmedHost, mainDomainSuffix) { // Serve pages from subdomains of MainDomainSuffix log.Debug().Msg("main domain suffix") - pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/") targetOwner := strings.TrimSuffix(trimmedHost, mainDomainSuffix) targetRepo := pathElements[0] @@ -245,16 +247,16 @@ func Handler(mainDomainSuffix, rawDomain string, return } - pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/") + pathParts := pathElements canonicalLink := false if strings.HasPrefix(pathElements[0], "@") { targetBranch = pathElements[0][1:] - pathElements = pathElements[1:] + pathParts = pathElements[1:] canonicalLink = true } // Try to use the given repo on the given branch or the default branch - newPath := path.Join(pathElements...) + newPath := path.Join(pathParts...) log.Debug().Msg("custom domain preparations, now trying with details from DNS") if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, targetBranch, newPath, canonicalLink); works { targetOptions.BranchTimestamp = timestampBranch.Timestamp