mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-18 10:29:43 +00:00
move more args of Upstream() to upstream Options
This commit is contained in:
parent
2f6b280fce
commit
a7bb3448a4
4 changed files with 56 additions and 30 deletions
|
@ -144,7 +144,8 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
log.Debug().Msg("missing branch")
|
log.Debug().Msg("missing branch")
|
||||||
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
|
|
||||||
log.Debug().Msg("raw domain preparations, now trying with default branch")
|
log.Debug().Msg("raw domain preparations, now trying with default branch")
|
||||||
tryBranch(targetRepo, "", pathElements[2:],
|
tryBranch(targetRepo, "", pathElements[2:],
|
||||||
giteaRoot+"/"+targetOwner+"/"+targetRepo+"/src/branch/%b/%p",
|
giteaRoot+"/"+targetOwner+"/"+targetRepo+"/src/branch/%b/%p",
|
||||||
|
@ -155,7 +156,6 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
giteaRoot, giteaAPIToken,
|
giteaRoot, giteaAPIToken,
|
||||||
canonicalDomainCache, branchTimestampCache, fileResponseCache)
|
canonicalDomainCache, branchTimestampCache, fileResponseCache)
|
||||||
return
|
return
|
||||||
}
|
|
||||||
|
|
||||||
} else if bytes.HasSuffix(trimmedHost, mainDomainSuffix) {
|
} else if bytes.HasSuffix(trimmedHost, mainDomainSuffix) {
|
||||||
// Serve pages from subdomains of MainDomainSuffix
|
// Serve pages from subdomains of MainDomainSuffix
|
||||||
|
@ -167,7 +167,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
targetPath = strings.Trim(strings.Join(pathElements[1:], "/"), "/")
|
targetPath = strings.Trim(strings.Join(pathElements[1:], "/"), "/")
|
||||||
|
|
||||||
if targetOwner == "www" {
|
if targetOwner == "www" {
|
||||||
// www.codeberg.page redirects to codeberg.page
|
// www.codeberg.page redirects to codeberg.page // TODO: rm hardcoded - use cname?
|
||||||
ctx.Redirect("https://"+string(mainDomainSuffix[1:])+string(ctx.Path()), fasthttp.StatusPermanentRedirect)
|
ctx.Redirect("https://"+string(mainDomainSuffix[1:])+string(ctx.Path()), fasthttp.StatusPermanentRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -271,11 +271,11 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
if targetOwner != "" {
|
if targetOwner != "" {
|
||||||
ctx.Redirect("https://"+canonicalDomain+string(ctx.RequestURI()), fasthttp.StatusTemporaryRedirect)
|
ctx.Redirect("https://"+canonicalDomain+string(ctx.RequestURI()), fasthttp.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
|
|
||||||
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug().Msg("tryBranch, now trying upstream")
|
log.Debug().Msg("tryBranch, now trying upstream")
|
||||||
tryUpstream(ctx, mainDomainSuffix, trimmedHost,
|
tryUpstream(ctx, mainDomainSuffix, trimmedHost,
|
||||||
|
@ -283,10 +283,10 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
giteaRoot, giteaAPIToken,
|
giteaRoot, giteaAPIToken,
|
||||||
canonicalDomainCache, branchTimestampCache, fileResponseCache)
|
canonicalDomainCache, branchTimestampCache, fileResponseCache)
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
|
|
||||||
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ func tryUpstream(ctx *fasthttp.RequestCtx,
|
||||||
|
|
||||||
giteaRoot, giteaAPIToken string,
|
giteaRoot, giteaAPIToken string,
|
||||||
canonicalDomainCache, branchTimestampCache, fileResponseCache cache.SetGetKey) {
|
canonicalDomainCache, branchTimestampCache, fileResponseCache cache.SetGetKey) {
|
||||||
|
|
||||||
// check if a canonical domain exists on a request on MainDomain
|
// check if a canonical domain exists on a request on MainDomain
|
||||||
if bytes.HasSuffix(trimmedHost, mainDomainSuffix) {
|
if bytes.HasSuffix(trimmedHost, mainDomainSuffix) {
|
||||||
canonicalDomain, _ := upstream.CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, "", string(mainDomainSuffix), giteaRoot, giteaAPIToken, canonicalDomainCache)
|
canonicalDomain, _ := upstream.CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, "", string(mainDomainSuffix), giteaRoot, giteaAPIToken, canonicalDomainCache)
|
||||||
|
@ -33,8 +34,13 @@ func tryUpstream(ctx *fasthttp.RequestCtx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
targetOptions.TargetOwner = targetOwner
|
||||||
|
targetOptions.TargetRepo = targetRepo
|
||||||
|
targetOptions.TargetBranch = targetBranch
|
||||||
|
targetOptions.TargetPath = targetPath
|
||||||
|
|
||||||
// Try to request the file from the Gitea API
|
// Try to request the file from the Gitea API
|
||||||
if !targetOptions.Upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath, giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
if !targetOptions.Upstream(ctx, giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
||||||
html.ReturnErrorPage(ctx, ctx.Response.StatusCode())
|
html.ReturnErrorPage(ctx, ctx.Response.StatusCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,11 @@ var upstreamIndexPages = []string{
|
||||||
|
|
||||||
// Options provides various options for the upstream request.
|
// Options provides various options for the upstream request.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
|
TargetOwner,
|
||||||
|
TargetRepo,
|
||||||
|
TargetBranch,
|
||||||
|
TargetPath,
|
||||||
|
|
||||||
DefaultMimeType string
|
DefaultMimeType string
|
||||||
ForbiddenMimeTypes map[string]struct{}
|
ForbiddenMimeTypes map[string]struct{}
|
||||||
TryIndexPages bool
|
TryIndexPages bool
|
||||||
|
@ -41,26 +46,26 @@ var client = fasthttp.Client{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
||||||
func (o *Options) Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, targetBranch, targetPath, giteaRoot, giteaAPIToken string, branchTimestampCache, fileResponseCache cache.SetGetKey) (final bool) {
|
func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken string, branchTimestampCache, fileResponseCache cache.SetGetKey) (final bool) {
|
||||||
log := log.With().Strs("upstream", []string{targetOwner, targetRepo, targetBranch, targetPath}).Logger()
|
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
||||||
|
|
||||||
if o.ForbiddenMimeTypes == nil {
|
if o.ForbiddenMimeTypes == nil {
|
||||||
o.ForbiddenMimeTypes = map[string]struct{}{}
|
o.ForbiddenMimeTypes = map[string]struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the branch exists and when it was modified
|
// Check if the branch exists and when it was modified
|
||||||
if o.BranchTimestamp == (time.Time{}) {
|
if o.BranchTimestamp.IsZero() {
|
||||||
branch := GetBranchTimestamp(targetOwner, targetRepo, targetBranch, giteaRoot, giteaAPIToken, branchTimestampCache)
|
branch := GetBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch, giteaRoot, giteaAPIToken, branchTimestampCache)
|
||||||
|
|
||||||
if branch == nil {
|
if branch == nil {
|
||||||
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
targetBranch = branch.Branch
|
o.TargetBranch = branch.Branch
|
||||||
o.BranchTimestamp = branch.Timestamp
|
o.BranchTimestamp = branch.Timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
if targetOwner == "" || targetRepo == "" || targetBranch == "" {
|
if o.TargetOwner == "" || o.TargetRepo == "" || o.TargetBranch == "" {
|
||||||
html.ReturnErrorPage(ctx, fasthttp.StatusBadRequest)
|
html.ReturnErrorPage(ctx, fasthttp.StatusBadRequest)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -75,7 +80,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, ta
|
||||||
log.Debug().Msg("preparations")
|
log.Debug().Msg("preparations")
|
||||||
|
|
||||||
// Make a GET request to the upstream URL
|
// Make a GET request to the upstream URL
|
||||||
uri := targetOwner + "/" + targetRepo + "/raw/" + targetBranch + "/" + targetPath
|
uri := o.TargetOwner + "/" + o.TargetRepo + "/raw/" + o.TargetBranch + "/" + o.TargetPath
|
||||||
var req *fasthttp.Request
|
var req *fasthttp.Request
|
||||||
var res *fasthttp.Response
|
var res *fasthttp.Response
|
||||||
var cachedResponse fileResponse
|
var cachedResponse fileResponse
|
||||||
|
@ -99,7 +104,8 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, ta
|
||||||
optionsForIndexPages.TryIndexPages = false
|
optionsForIndexPages.TryIndexPages = false
|
||||||
optionsForIndexPages.appendTrailingSlash = true
|
optionsForIndexPages.appendTrailingSlash = true
|
||||||
for _, indexPage := range upstreamIndexPages {
|
for _, indexPage := range upstreamIndexPages {
|
||||||
if optionsForIndexPages.Upstream(ctx, targetOwner, targetRepo, targetBranch, strings.TrimSuffix(targetPath, "/")+"/"+indexPage, giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage
|
||||||
|
if optionsForIndexPages.Upstream(ctx, giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
||||||
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
||||||
exists: false,
|
exists: false,
|
||||||
}, fileCacheTimeout)
|
}, fileCacheTimeout)
|
||||||
|
@ -109,7 +115,8 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, ta
|
||||||
// compatibility fix for GitHub Pages (/example → /example.html)
|
// compatibility fix for GitHub Pages (/example → /example.html)
|
||||||
optionsForIndexPages.appendTrailingSlash = false
|
optionsForIndexPages.appendTrailingSlash = false
|
||||||
optionsForIndexPages.redirectIfExists = string(ctx.Request.URI().Path()) + ".html"
|
optionsForIndexPages.redirectIfExists = string(ctx.Request.URI().Path()) + ".html"
|
||||||
if optionsForIndexPages.Upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath+".html", giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
optionsForIndexPages.TargetPath = o.TargetPath + ".html"
|
||||||
|
if optionsForIndexPages.Upstream(ctx, giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
||||||
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
||||||
exists: false,
|
exists: false,
|
||||||
}, fileCacheTimeout)
|
}, fileCacheTimeout)
|
||||||
|
@ -148,7 +155,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, ta
|
||||||
log.Debug().Msg("error handling")
|
log.Debug().Msg("error handling")
|
||||||
|
|
||||||
// Set the MIME type
|
// Set the MIME type
|
||||||
mimeType := mime.TypeByExtension(path.Ext(targetPath))
|
mimeType := mime.TypeByExtension(path.Ext(o.TargetPath))
|
||||||
mimeTypeSplit := strings.SplitN(mimeType, ";", 2)
|
mimeTypeSplit := strings.SplitN(mimeType, ";", 2)
|
||||||
if _, ok := o.ForbiddenMimeTypes[mimeTypeSplit[0]]; ok || mimeType == "" {
|
if _, ok := o.ForbiddenMimeTypes[mimeTypeSplit[0]]; ok || mimeType == "" {
|
||||||
if o.DefaultMimeType != "" {
|
if o.DefaultMimeType != "" {
|
||||||
|
|
13
server/utils/utils_test.go
Normal file
13
server/utils/utils_test.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTrimHostPort(t *testing.T) {
|
||||||
|
assert.EqualValues(t, "aa", TrimHostPort([]byte("aa")))
|
||||||
|
assert.EqualValues(t, "", TrimHostPort([]byte(":")))
|
||||||
|
assert.EqualValues(t, "example.com", TrimHostPort([]byte("example.com:80")))
|
||||||
|
}
|
Loading…
Reference in a new issue