Wildcards #2
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,5 @@
|
|||||||
# Artificats
|
# Artificats
|
||||||
rio
|
/rio
|
||||||
|
|
||||||
# Testing stuff
|
# Testing stuff
|
||||||
*.json
|
*.json
|
||||||
|
@ -24,14 +24,26 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleSubdomain(domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
|
// 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 := ""
|
username := ""
|
||||||
if cname != "" {
|
if cname != "" {
|
||||||
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
|
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
|
||||||
username = strings.Split(cname, ".")[0]
|
username = extractUsername(pagesDomain, cname)
|
||||||
} else {
|
} else {
|
||||||
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
|
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
|
||||||
username = strings.Split(domain, ".")[0]
|
username = extractUsername(pagesDomain, domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip the leading /
|
// Strip the leading /
|
||||||
@ -79,7 +91,7 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
|
|||||||
// Is a direct subdomain requested?
|
// Is a direct subdomain requested?
|
||||||
if strings.HasSuffix(req.Host, pagesDomain) {
|
if strings.HasSuffix(req.Host, pagesDomain) {
|
||||||
log.Debug("Domain can be directly handled")
|
log.Debug("Domain can be directly handled")
|
||||||
handleSubdomain(req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
handleSubdomain(pagesDomain, req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +108,7 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
|
|||||||
// pages domain makes no sense.
|
// pages domain makes no sense.
|
||||||
if strings.HasSuffix(cname, "."+pagesDomain) {
|
if strings.HasSuffix(cname, "."+pagesDomain) {
|
||||||
log.Debugf("%s is alias of %s and can be handled after a CNAME query", req.Host, cname)
|
log.Debugf("%s is alias of %s and can be handled after a CNAME query", req.Host, cname)
|
||||||
handleSubdomain(req.Host, cname, req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
handleSubdomain(pagesDomain, req.Host, cname, req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
cmd/rio/main_test.go
Normal file
23
cmd/rio/main_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user