deduplicate

This commit is contained in:
6543 2022-11-12 14:08:08 +01:00
parent 5f2f073a73
commit 8e6c0c6c9f
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE

View file

@ -88,6 +88,7 @@ func Handler(mainDomainSuffix, rawDomain string,
} }
// Prepare request information to Gitea // Prepare request information to Gitea
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
targetOptions := &upstream.Options{ targetOptions := &upstream.Options{
TryIndexPages: true, TryIndexPages: true,
} }
@ -100,25 +101,24 @@ func Handler(mainDomainSuffix, rawDomain string,
targetOptions.TryIndexPages = false targetOptions.TryIndexPages = false
targetOptions.ServeRaw = true targetOptions.ServeRaw = true
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
if len(pathElements) < 2 { if len(pathElements) < 2 {
// https://{RawDomain}/{owner}/{repo}[/@{branch}]/{path} is required // https://{RawDomain}/{owner}/{repo}[/@{branch}]/{path} is required
ctx.Redirect(rawInfoPage, http.StatusTemporaryRedirect) ctx.Redirect(rawInfoPage, http.StatusTemporaryRedirect)
return return
} }
targetOwner := pathElements[0]
targetRepo := pathElements[1]
// raw.codeberg.org/example/myrepo/@main/index.html // raw.codeberg.org/example/myrepo/@main/index.html
if len(pathElements) > 2 && strings.HasPrefix(pathElements[2], "@") { if len(pathElements) > 2 && strings.HasPrefix(pathElements[2], "@") {
log.Debug().Msg("raw domain preparations, now trying with specified branch") log.Debug().Msg("raw domain preparations, now trying with specified branch")
newPath := path.Join(pathElements[3:]...) newPath := path.Join(pathElements[3:]...)
branch := pathElements[2][1:] 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 targetOptions.BranchTimestamp = timestampBranch.Timestamp
log.Debug().Msg("tryBranch, now trying upstream 1") log.Debug().Msg("tryBranch, now trying upstream 1")
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache) targetOptions, repoOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
return return
} }
log.Debug().Msg("missing branch info") 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") log.Debug().Msg("raw domain preparations, now trying with default branch")
repoOwner := pathElements[0]
repo := pathElements[1]
newPath := path.Join(pathElements[2:]...) 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 targetOptions.BranchTimestamp = timestampBranch.Timestamp
log.Debug().Msg("tryBranch, now trying upstream 2") log.Debug().Msg("tryBranch, now trying upstream 2")
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache) targetOptions, repoOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
} else { return
log.Error().Msg("TODO: is this a bug?")
} }
html.ReturnErrorPage(ctx, fmt.Sprintf("raw domain could not find repo '%s/%s' or repo is empty", repoOwner, repo), http.StatusNotFound)
return return
} else if strings.HasSuffix(trimmedHost, mainDomainSuffix) { } else if strings.HasSuffix(trimmedHost, mainDomainSuffix) {
// Serve pages from subdomains of MainDomainSuffix // Serve pages from subdomains of MainDomainSuffix
log.Debug().Msg("main domain suffix") log.Debug().Msg("main domain suffix")
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
targetOwner := strings.TrimSuffix(trimmedHost, mainDomainSuffix) targetOwner := strings.TrimSuffix(trimmedHost, mainDomainSuffix)
targetRepo := pathElements[0] targetRepo := pathElements[0]
@ -245,16 +247,16 @@ func Handler(mainDomainSuffix, rawDomain string,
return return
} }
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/") pathParts := pathElements
canonicalLink := false canonicalLink := false
if strings.HasPrefix(pathElements[0], "@") { if strings.HasPrefix(pathElements[0], "@") {
targetBranch = pathElements[0][1:] targetBranch = pathElements[0][1:]
pathElements = pathElements[1:] pathParts = pathElements[1:]
canonicalLink = true canonicalLink = true
} }
// Try to use the given repo on the given branch or the default branch // 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") 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 { if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, targetBranch, newPath, canonicalLink); works {
targetOptions.BranchTimestamp = timestampBranch.Timestamp targetOptions.BranchTimestamp = timestampBranch.Timestamp