mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
more string
This commit is contained in:
parent
662d76386c
commit
51ca74fc11
16 changed files with 121 additions and 123 deletions
|
@ -3,7 +3,6 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
@ -27,7 +26,7 @@ const (
|
|||
)
|
||||
|
||||
// Handler handles a single HTTP request to the web server.
|
||||
func Handler(mainDomainSuffix, rawDomain []byte,
|
||||
func Handler(mainDomainSuffix, rawDomain string,
|
||||
giteaClient *gitea.Client,
|
||||
giteaRoot, rawInfoPage string,
|
||||
blacklistedPaths, allowedCorsDomains []string,
|
||||
|
@ -45,7 +44,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
// Enable browser caching for up to 10 minutes
|
||||
ctx.RespWriter.Header().Set("Cache-Control", "public, max-age=600")
|
||||
|
||||
trimmedHost := utils.TrimHostPort([]byte(req.Host))
|
||||
trimmedHost := utils.TrimHostPort(req.Host)
|
||||
|
||||
// Add HSTS for RawDomain and MainDomainSuffix
|
||||
if hsts := GetHSTSHeader(trimmedHost, mainDomainSuffix, rawDomain); hsts != "" {
|
||||
|
@ -53,16 +52,16 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
}
|
||||
|
||||
// Block all methods not required for static pages
|
||||
if !ctx.IsGet() && !ctx.IsHead() && !ctx.IsOptions() {
|
||||
ctx.Response.Header.Set("Allow", "GET, HEAD, OPTIONS")
|
||||
ctx.Error("Method not allowed", fasthttp.StatusMethodNotAllowed)
|
||||
if !ctx.IsMethod(http.MethodGet) && !ctx.IsMethod(http.MethodHead) && !ctx.IsMethod(http.MethodOptions) {
|
||||
ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions) // duplic 1
|
||||
ctx.String("Method not allowed", fasthttp.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Block blacklisted paths (like ACME challenges)
|
||||
for _, blacklistedPath := range blacklistedPaths {
|
||||
if strings.HasPrefix(ctx.Path(), blacklistedPath) {
|
||||
html.ReturnErrorPage(ctx, fasthttp.StatusForbidden)
|
||||
html.ReturnErrorPage(ctx, "", fasthttp.StatusForbidden)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +69,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
// Allow CORS for specified domains
|
||||
allowCors := false
|
||||
for _, allowedCorsDomain := range allowedCorsDomains {
|
||||
if bytes.Equal(trimmedHost, allowedCorsDomain) {
|
||||
if strings.EqualFold(trimmedHost, allowedCorsDomain) {
|
||||
allowCors = true
|
||||
break
|
||||
}
|
||||
|
@ -79,9 +78,9 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
ctx.RespWriter.Header().Set(headerAccessControlAllowOrigin, "*")
|
||||
ctx.RespWriter.Header().Set(headerAccessControlAllowMethods, http.MethodGet+", "+http.MethodHead)
|
||||
}
|
||||
ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions)
|
||||
if ctx.IsOptions() {
|
||||
|
||||
ctx.RespWriter.Header().Set("Allow", http.MethodGet+", "+http.MethodHead+", "+http.MethodOptions) // duplic 1
|
||||
if ctx.IsMethod(http.MethodOptions) {
|
||||
ctx.Response().StatusCode = http.StatusNoContent
|
||||
return
|
||||
}
|
||||
|
@ -132,7 +131,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
}
|
||||
|
||||
log.Debug().Msg("preparations")
|
||||
if rawDomain != nil && bytes.Equal(trimmedHost, rawDomain) {
|
||||
if rawDomain != "" && strings.EqualFold(trimmedHost, rawDomain) {
|
||||
// Serve raw content from RawDomain
|
||||
log.Debug().Msg("raw domain")
|
||||
|
||||
|
@ -166,7 +165,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
return
|
||||
}
|
||||
log.Debug().Msg("missing branch")
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -181,12 +180,12 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
canonicalDomainCache)
|
||||
return
|
||||
|
||||
} else if bytes.HasSuffix(trimmedHost, mainDomainSuffix) {
|
||||
} else if strings.HasSuffix(trimmedHost, mainDomainSuffix) {
|
||||
// Serve pages from subdomains of MainDomainSuffix
|
||||
log.Debug().Msg("main domain suffix")
|
||||
|
||||
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
|
||||
targetOwner = string(bytes.TrimSuffix(trimmedHost, mainDomainSuffix))
|
||||
targetOwner = strings.TrimSuffix(trimmedHost, mainDomainSuffix)
|
||||
targetRepo = pathElements[0]
|
||||
targetPath = strings.Trim(strings.Join(pathElements[1:], "/"), "/")
|
||||
|
||||
|
@ -215,7 +214,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
|
||||
canonicalDomainCache)
|
||||
} else {
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -231,7 +230,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
|
||||
canonicalDomainCache)
|
||||
} else {
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -262,7 +261,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
}
|
||||
|
||||
// Couldn't find a valid repo/branch
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
return
|
||||
} else {
|
||||
trimmedHostStr := string(trimmedHost)
|
||||
|
@ -270,7 +269,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
// Serve pages from external domains
|
||||
targetOwner, targetRepo, targetBranch = dns.GetTargetFromDNS(trimmedHostStr, string(mainDomainSuffix), dnsLookupCache)
|
||||
if targetOwner == "" {
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -288,7 +287,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
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, "", http.StatusMisdirectedRequest)
|
||||
return
|
||||
} else if canonicalDomain != trimmedHostStr {
|
||||
// only redirect if the target is also a codeberg page!
|
||||
|
@ -298,7 +297,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
return
|
||||
}
|
||||
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -309,7 +308,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||
return
|
||||
}
|
||||
|
||||
html.ReturnErrorPage(ctx, http.StatusFailedDependency)
|
||||
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue