fix: Fix username extraction in the TLS handler
This commit is contained in:
@@ -24,26 +24,14 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Extract the username from the domain name @domain that we're processing
|
||||
// at the moment.
|
||||
func extractUsername(pagesDomain, domain string) string {
|
||||
suffixlessDomain := strings.TrimSuffix(domain, "."+pagesDomain)
|
||||
usernameParts := strings.Split(suffixlessDomain, ".")
|
||||
if len(usernameParts) == 1 {
|
||||
return usernameParts[0]
|
||||
}
|
||||
|
||||
return strings.Join(usernameParts, ".")
|
||||
}
|
||||
|
||||
func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
|
||||
username := ""
|
||||
if cname != "" {
|
||||
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
|
||||
username = extractUsername(pagesDomain, cname)
|
||||
username = dns.ExtractUsername(pagesDomain, cname)
|
||||
} else {
|
||||
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
|
||||
username = extractUsername(pagesDomain, domain)
|
||||
username = dns.ExtractUsername(pagesDomain, domain)
|
||||
}
|
||||
|
||||
// Strip the leading /
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExtractUsernameSimple(t *testing.T) {
|
||||
username := extractUsername(
|
||||
"pages.local",
|
||||
"papatutuwawa.pages.local",
|
||||
)
|
||||
if username != "papatutuwawa" {
|
||||
t.Fatalf("Unexpected username: '%s'", username)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractUsernameDot(t *testing.T) {
|
||||
username := extractUsername(
|
||||
"pages.local",
|
||||
"polynom.me.pages.local",
|
||||
)
|
||||
if username != "polynom.me" {
|
||||
t.Fatalf("Unexpected username: '%s'", username)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user