mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
improve & refactor & return specific error pages
This commit is contained in:
parent
60aefb4bf5
commit
70871e77be
4 changed files with 45 additions and 24 deletions
|
@ -1,7 +1,9 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
|
@ -58,7 +60,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
// Block blacklisted paths (like ACME challenges)
|
||||
for _, blacklistedPath := range blacklistedPaths {
|
||||
if strings.HasPrefix(ctx.Path(), blacklistedPath) {
|
||||
html.ReturnErrorPage(ctx, "", http.StatusForbidden)
|
||||
html.ReturnErrorPage(ctx, "requested blacklisted path", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +92,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
|
||||
// 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.
|
||||
tryBranch := func(log zerolog.Logger, repo, branch string, path []string, canonicalLink string) bool {
|
||||
tryBranch := func(log zerolog.Logger, repo, branch string, _path []string, canonicalLink string) bool {
|
||||
if repo == "" {
|
||||
log.Debug().Msg("tryBranch: repo == ''")
|
||||
return false
|
||||
|
@ -109,7 +111,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
|
||||
// Branch exists, use it
|
||||
targetRepo = repo
|
||||
targetPath = strings.Trim(strings.Join(path, "/"), "/")
|
||||
targetPath = path.Join(_path...)
|
||||
targetBranch = branchTimestampResult.Branch
|
||||
|
||||
targetOptions.BranchTimestamp = branchTimestampResult.Timestamp
|
||||
|
@ -157,8 +159,8 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
canonicalDomainCache)
|
||||
return
|
||||
}
|
||||
log.Debug().Msg("missing branch")
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
log.Debug().Msg("missing branch info")
|
||||
html.ReturnErrorPage(ctx, "missing branch info", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -198,8 +200,9 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
}
|
||||
|
||||
log.Debug().Msg("main domain preparations, now trying with specified repo & branch")
|
||||
branch := pathElements[1][1:]
|
||||
if tryBranch(log,
|
||||
pathElements[0], pathElements[1][1:], pathElements[2:],
|
||||
pathElements[0], branch, pathElements[2:],
|
||||
"/"+pathElements[0]+"/%p",
|
||||
) {
|
||||
log.Debug().Msg("tryBranch, now trying upstream 3")
|
||||
|
@ -207,7 +210,9 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
|
||||
canonicalDomainCache)
|
||||
} else {
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx,
|
||||
fmt.Sprintf("explizite set branch '%s' do not exist at '%s/%s'", branch, targetOwner, targetRepo),
|
||||
http.StatusFailedDependency)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -216,14 +221,17 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
// example.codeberg.page/@main/index.html
|
||||
if strings.HasPrefix(pathElements[0], "@") {
|
||||
log.Debug().Msg("main domain preparations, now trying with specified branch")
|
||||
branch := pathElements[0][1:]
|
||||
if tryBranch(log,
|
||||
"pages", pathElements[0][1:], pathElements[1:], "/%p") {
|
||||
"pages", branch, pathElements[1:], "/%p") {
|
||||
log.Debug().Msg("tryBranch, now trying upstream 4")
|
||||
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
|
||||
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
|
||||
targetOptions, targetOwner, "pages", targetBranch, targetPath,
|
||||
canonicalDomainCache)
|
||||
} else {
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx,
|
||||
fmt.Sprintf("explizite set branch '%s' do not exist at '%s/%s'", branch, targetOwner, "pages"),
|
||||
http.StatusFailedDependency)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -254,15 +262,19 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
}
|
||||
|
||||
// Couldn't find a valid repo/branch
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx,
|
||||
fmt.Sprintf("couldn't find a valid repo[%s]/branch[%s]", targetRepo, targetBranch),
|
||||
http.StatusFailedDependency)
|
||||
return
|
||||
} else {
|
||||
trimmedHostStr := string(trimmedHost)
|
||||
|
||||
// Serve pages from external domains
|
||||
// Serve pages from custom domains
|
||||
targetOwner, targetRepo, targetBranch = dns.GetTargetFromDNS(trimmedHostStr, string(mainDomainSuffix), dnsLookupCache)
|
||||
if targetOwner == "" {
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx,
|
||||
"could not obtain repo owner from custom domain",
|
||||
http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -280,7 +292,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
targetRepo, targetBranch, pathElements, canonicalLink) {
|
||||
canonicalDomain, valid := upstream.CheckCanonicalDomain(giteaClient, targetOwner, targetRepo, targetBranch, trimmedHostStr, string(mainDomainSuffix), canonicalDomainCache)
|
||||
if !valid {
|
||||
html.ReturnErrorPage(ctx, "", http.StatusMisdirectedRequest)
|
||||
html.ReturnErrorPage(ctx, "domain not specified in <code>.domains</code> file", http.StatusMisdirectedRequest)
|
||||
return
|
||||
} else if canonicalDomain != trimmedHostStr {
|
||||
// only redirect if the target is also a codeberg page!
|
||||
|
@ -290,7 +302,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
return
|
||||
}
|
||||
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "target is no codeberg page", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -301,7 +313,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
|||
return
|
||||
}
|
||||
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "could not find target for custom domain", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue