From 9c412966fcf5c78f7d54414d390a31bec4322840 Mon Sep 17 00:00:00 2001
From: Simon Vieille <simon@deblan.fr>
Date: Tue, 16 Aug 2022 15:58:22 +0200
Subject: [PATCH] try each default branch instead of 'pages' only

---
 cmd/main.go       |  9 ++++++++-
 server/handler.go | 47 +++++++++++++++++++++++++----------------------
 2 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go
index 41809cb..aa361a8 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -48,6 +48,7 @@ func Serve(ctx *cli.Context) error {
 	giteaAPIToken := ctx.String("gitea-api-token")
 	rawDomain := ctx.String("raw-domain")
 	mainDomainSuffix := []byte(ctx.String("pages-domain"))
+	defaultBranches := strings.Split(strings.ReplaceAll(ctx.String("pages-branches"), " ", ""), ",")
 	rawInfoPage := ctx.String("raw-info-page")
 	listeningAddress := fmt.Sprintf("%s:%s", ctx.String("host"), ctx.String("port"))
 	enableHTTPServer := ctx.Bool("enable-http-server")
@@ -73,6 +74,10 @@ func Serve(ctx *cli.Context) error {
 		mainDomainSuffix = append([]byte{'.'}, mainDomainSuffix...)
 	}
 
+	if len(defaultBranches) == 0 {
+		defaultBranches = []string{"pages"}
+	}
+
 	keyCache := cache.NewKeyValueCache()
 	challengeCache := cache.NewKeyValueCache()
 	// canonicalDomainCache stores canonical domains
@@ -95,7 +100,9 @@ func Serve(ctx *cli.Context) error {
 		giteaClient,
 		giteaRoot, rawInfoPage,
 		BlacklistedPaths, allowedCorsDomains,
-		dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache)
+		dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache,
+		defaultBranches,
+	)
 
 	fastServer := server.SetupServer(handler)
 	httpServer := server.SetupHTTPACMEChallengeServer(challengeCache)
diff --git a/server/handler.go b/server/handler.go
index cd67aa7..5f56e0f 100644
--- a/server/handler.go
+++ b/server/handler.go
@@ -23,6 +23,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
 	giteaRoot, rawInfoPage string,
 	blacklistedPaths, allowedCorsDomains [][]byte,
 	dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache cache.SetGetKey,
+	defaultBranches []string,
 ) func(ctx *fasthttp.RequestCtx) {
 	return func(ctx *fasthttp.RequestCtx) {
 		log := log.With().Strs("Handler", []string{string(ctx.Request.Host()), string(ctx.Request.Header.RequestURI())}).Logger()
@@ -227,29 +228,31 @@ func Handler(mainDomainSuffix, rawDomain []byte,
 				return
 			}
 
-			// Check if the first directory is a repo with a "pages" branch
-			// example.codeberg.page/myrepo/index.html
-			// example.codeberg.page/pages/... is not allowed here.
-			log.Debug().Msg("main domain preparations, now trying with specified repo")
-			if pathElements[0] != "pages" && tryBranch(log,
-				pathElements[0], "pages", pathElements[1:], "") {
-				log.Info().Msg("tryBranch, now trying upstream 5")
-				tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
-					targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
-					canonicalDomainCache, branchTimestampCache, fileResponseCache)
-				return
-			}
+			for _, branch := range defaultBranches {
+				// Check if the first directory is a repo with a default branch
+				// example.codeberg.page/myrepo/index.html
+				// example.codeberg.page/{PAGES_BRANCHE}/... is not allowed here.
+				log.Debug().Msg("main domain preparations, now trying with specified repo")
+				if pathElements[0] != branch && tryBranch(log,
+					pathElements[0], branch, pathElements[1:], "") {
+					log.Info().Msg("tryBranch, now trying upstream 5")
+					tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
+						targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
+						canonicalDomainCache, branchTimestampCache, fileResponseCache)
+					return
+				}
 
-			// Try to use the "pages" repo on its default branch
-			// example.codeberg.page/index.html
-			log.Debug().Msg("main domain preparations, now trying with default repo/branch")
-			if tryBranch(log,
-				"pages", "", pathElements, "") {
-				log.Info().Msg("tryBranch, now trying upstream 6")
-				tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
-					targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
-					canonicalDomainCache, branchTimestampCache, fileResponseCache)
-				return
+				// Try to use the "pages" repo on its default branch
+				// example.codeberg.page/index.html
+				log.Debug().Msg("main domain preparations, now trying with default repo/branch")
+				if tryBranch(log,
+					branch, "", pathElements, "") {
+					log.Info().Msg("tryBranch, now trying upstream 6")
+					tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
+						targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
+						canonicalDomainCache, branchTimestampCache, fileResponseCache)
+					return
+				}
 			}
 
 			// Couldn't find a valid repo/branch