Compare commits
No commits in common. "25eb0de1e7dec06e4c72b418feb8382aead3cdd3" and "2cbe46dc1a546de63f54e3259f84ea87694ed48c" have entirely different histories.
25eb0de1e7
...
2cbe46dc1a
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,5 @@
|
||||
# Artificats
|
||||
/rio
|
||||
rio
|
||||
|
||||
# Testing stuff
|
||||
*.json
|
||||
|
@ -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) {
|
||||
func handleSubdomain(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 = strings.Split(cname, ".")[0]
|
||||
} else {
|
||||
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
|
||||
username = extractUsername(pagesDomain, domain)
|
||||
username = strings.Split(domain, ".")[0]
|
||||
}
|
||||
|
||||
// Strip the leading /
|
||||
@ -79,19 +67,9 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Server", "rio")
|
||||
|
||||
// Is the direct domain requested?
|
||||
if req.Host == pagesDomain {
|
||||
log.Debug("Direct pages domain is requested.")
|
||||
|
||||
// TODO: Handle
|
||||
w.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
|
||||
// Is a direct subdomain requested?
|
||||
if strings.HasSuffix(req.Host, pagesDomain) {
|
||||
log.Debug("Domain can be directly handled")
|
||||
handleSubdomain(pagesDomain, req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||
handleSubdomain(req.Host, "", req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||
return
|
||||
}
|
||||
|
||||
@ -103,12 +81,9 @@ func Handler(pagesDomain, giteaUrl, defaultCsp string, giteaClient *repo.GiteaCl
|
||||
}
|
||||
log.Debugf("Got CNAME %s", cname)
|
||||
|
||||
// Is a direct subdomain requested after CNAME lookup?
|
||||
// NOTE: We now require the leading dot because a CNAME to the direct
|
||||
// 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)
|
||||
handleSubdomain(pagesDomain, req.Host, cname, req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||
handleSubdomain(req.Host, cname, req.URL.Path, giteaUrl, defaultCsp, giteaClient, w)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user