2022-11-12 19:43:44 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleRaw(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
|
2023-11-16 17:33:39 +00:00
|
|
|
mainDomainSuffix string,
|
2022-11-12 19:43:44 +00:00
|
|
|
trimmedHost string,
|
|
|
|
pathElements []string,
|
2024-02-15 16:08:29 +00:00
|
|
|
canonicalDomainCache, redirectsCache cache.ICache,
|
2022-11-12 19:43:44 +00:00
|
|
|
) {
|
|
|
|
// Serve raw content from RawDomain
|
|
|
|
log.Debug().Msg("raw domain")
|
|
|
|
|
|
|
|
if len(pathElements) < 2 {
|
2023-11-16 17:33:39 +00:00
|
|
|
html.ReturnErrorPage(
|
|
|
|
ctx,
|
|
|
|
"a url in the form of <code>https://{domain}/{owner}/{repo}[/@{branch}]/{path}</code> is required",
|
|
|
|
http.StatusBadRequest,
|
|
|
|
)
|
|
|
|
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
ServeRaw: true,
|
|
|
|
TargetOwner: pathElements[0],
|
|
|
|
TargetRepo: pathElements[1],
|
|
|
|
TargetBranch: pathElements[2][1:],
|
|
|
|
TargetPath: path.Join(pathElements[3:]...),
|
|
|
|
}, true); works {
|
|
|
|
log.Trace().Msg("tryUpstream: serve raw domain with specified branch")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug().Msg("missing branch info")
|
|
|
|
html.ReturnErrorPage(ctx, "missing branch info", http.StatusFailedDependency)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msg("raw domain preparations, now trying with default branch")
|
|
|
|
if targetOpt, works := tryBranch(log, ctx, giteaClient, &upstream.Options{
|
|
|
|
TryIndexPages: false,
|
|
|
|
ServeRaw: true,
|
|
|
|
TargetOwner: pathElements[0],
|
|
|
|
TargetRepo: pathElements[1],
|
|
|
|
TargetPath: path.Join(pathElements[2:]...),
|
|
|
|
}, true); works {
|
|
|
|
log.Trace().Msg("tryUpstream: serve raw domain with default branch")
|
2023-03-30 21:36:31 +00:00
|
|
|
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost, targetOpt, canonicalDomainCache, redirectsCache)
|
2022-11-12 19:43:44 +00:00
|
|
|
} else {
|
|
|
|
html.ReturnErrorPage(ctx,
|
2023-11-16 17:11:35 +00:00
|
|
|
fmt.Sprintf("raw domain could not find repo <code>%s/%s</code> or repo is empty", targetOpt.TargetOwner, targetOpt.TargetRepo),
|
2022-11-12 19:43:44 +00:00
|
|
|
http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|