mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
modernize ForbiddenMimeTypes
This commit is contained in:
parent
6234889495
commit
d35e41fa9f
3 changed files with 10 additions and 9 deletions
|
@ -76,8 +76,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
// Prepare request information to Gitea
|
// Prepare request information to Gitea
|
||||||
var targetOwner, targetRepo, targetBranch, targetPath string
|
var targetOwner, targetRepo, targetBranch, targetPath string
|
||||||
targetOptions := &upstream.Options{
|
targetOptions := &upstream.Options{
|
||||||
ForbiddenMimeTypes: map[string]struct{}{},
|
TryIndexPages: true,
|
||||||
TryIndexPages: true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryBranch checks if a branch exists and populates the target variables. If canonicalLink is non-empty, it will
|
// tryBranch checks if a branch exists and populates the target variables. If canonicalLink is non-empty, it will
|
||||||
|
@ -119,7 +118,10 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
||||||
log.Debug().Msg("raw domain")
|
log.Debug().Msg("raw domain")
|
||||||
|
|
||||||
targetOptions.TryIndexPages = false
|
targetOptions.TryIndexPages = false
|
||||||
targetOptions.ForbiddenMimeTypes["text/html"] = struct{}{}
|
if targetOptions.ForbiddenMimeTypes == nil {
|
||||||
|
targetOptions.ForbiddenMimeTypes = make(map[string]bool)
|
||||||
|
}
|
||||||
|
targetOptions.ForbiddenMimeTypes["text/html"] = true
|
||||||
targetOptions.DefaultMimeType = "text/plain; charset=utf-8"
|
targetOptions.DefaultMimeType = "text/plain; charset=utf-8"
|
||||||
|
|
||||||
pathElements := strings.Split(string(bytes.Trim(ctx.Request.URI().Path(), "/")), "/")
|
pathElements := strings.Split(string(bytes.Trim(ctx.Request.URI().Path(), "/")), "/")
|
||||||
|
|
|
@ -48,9 +48,12 @@ func GetBranchTimestamp(giteaClient *gitea.Client, owner, repo, branch string, b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Options) getMimeTypeByExtension() string {
|
func (o *Options) getMimeTypeByExtension() string {
|
||||||
|
if o.ForbiddenMimeTypes == nil {
|
||||||
|
o.ForbiddenMimeTypes = make(map[string]bool)
|
||||||
|
}
|
||||||
mimeType := mime.TypeByExtension(path.Ext(o.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 forbidden := o.ForbiddenMimeTypes[mimeTypeSplit[0]]; forbidden || mimeType == "" {
|
||||||
if o.DefaultMimeType != "" {
|
if o.DefaultMimeType != "" {
|
||||||
mimeType = o.DefaultMimeType
|
mimeType = o.DefaultMimeType
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -28,7 +28,7 @@ type Options struct {
|
||||||
TargetPath,
|
TargetPath,
|
||||||
|
|
||||||
DefaultMimeType string
|
DefaultMimeType string
|
||||||
ForbiddenMimeTypes map[string]struct{}
|
ForbiddenMimeTypes map[string]bool
|
||||||
TryIndexPages bool
|
TryIndexPages bool
|
||||||
BranchTimestamp time.Time
|
BranchTimestamp time.Time
|
||||||
// internal
|
// internal
|
||||||
|
@ -40,10 +40,6 @@ type Options struct {
|
||||||
func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client, branchTimestampCache, fileResponseCache cache.SetGetKey) (final bool) {
|
func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client, branchTimestampCache, fileResponseCache cache.SetGetKey) (final bool) {
|
||||||
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
||||||
|
|
||||||
if o.ForbiddenMimeTypes == nil {
|
|
||||||
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.IsZero() {
|
if o.BranchTimestamp.IsZero() {
|
||||||
branch := GetBranchTimestamp(giteaClient, o.TargetOwner, o.TargetRepo, o.TargetBranch, branchTimestampCache)
|
branch := GetBranchTimestamp(giteaClient, o.TargetOwner, o.TargetRepo, o.TargetBranch, branchTimestampCache)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue