Compare commits
No commits in common. "40ce4e81a84b2f3b3497640afb1d14aefdc89874" and "6f9f92e68ae2a45157db619a1abbe7d041254358" have entirely different histories.
40ce4e81a8
...
6f9f92e68a
@ -26,10 +26,8 @@ import (
|
||||
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 = strings.Split(cname, ".")[0]
|
||||
} else {
|
||||
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
|
||||
username = strings.Split(domain, ".")[0]
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,10 @@ func makeCSPCacheKey(username, repositoryName string) string {
|
||||
return username + ":" + repositoryName
|
||||
}
|
||||
|
||||
// / Try to find the repository with name @reponame of the user @username. If @cname
|
||||
// / is not "", then it also verifies that the repository contains a "CNAME" with
|
||||
// / the value of @cname as its content. @host, @domain, and @path are passed for
|
||||
// / caching on success.
|
||||
func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path, cname string, giteaClient *GiteaClient) (*Repository, error) {
|
||||
log.Debugf("CNAME: %s", cname)
|
||||
log.Debugf("Looking up repository %s/%s", username, reponame)
|
||||
@ -81,8 +85,8 @@ func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path
|
||||
)
|
||||
|
||||
log.Debugf("CNAME Content: %s", cnameContent)
|
||||
if cnameContent != host {
|
||||
log.Warnf("CNAME mismatch: Repo '%s', Host '%s'", cnameContent, host)
|
||||
if cnameContent != cname {
|
||||
log.Warnf("CNAME mismatch: Repo '%s', CNAME '%s'", cnameContent, cname)
|
||||
return nil, errors.New("CNAME mismatch")
|
||||
}
|
||||
}
|
||||
@ -99,8 +103,6 @@ func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path
|
||||
return &repo, nil
|
||||
}
|
||||
|
||||
// host is the domain name we're accessed from. cname is the domain that host is pointing
|
||||
// if, if we're accessed via a CNAME. If not, then cname is "".
|
||||
func RepoFromPath(username, host, cname, path string, giteaClient *GiteaClient) (*Repository, string, error) {
|
||||
domain := host
|
||||
|
||||
@ -115,11 +117,11 @@ func RepoFromPath(username, host, cname, path string, giteaClient *GiteaClient)
|
||||
// Allow specifying the repository name in the TXT record
|
||||
reponame := ""
|
||||
if cname != "" {
|
||||
repoLookup, err := giteaClient.lookupRepoTXT(host)
|
||||
repoLookup, err := giteaClient.lookupRepoTXT(cname)
|
||||
if err == nil && repoLookup != "" {
|
||||
log.Infof(
|
||||
"TXT lookup for %s resulted in choosing repository %s",
|
||||
host,
|
||||
cname,
|
||||
repoLookup,
|
||||
)
|
||||
reponame = repoLookup
|
||||
@ -127,7 +129,6 @@ func RepoFromPath(username, host, cname, path string, giteaClient *GiteaClient)
|
||||
}
|
||||
|
||||
pathParts := strings.Split(path, "/")
|
||||
log.Debugf("reponame='%s' len(pathParts)='%d'", reponame, len(pathParts))
|
||||
if reponame == "" && len(pathParts) > 1 {
|
||||
log.Debugf("Trying repository %s", pathParts[0])
|
||||
modifiedPath := strings.Join(pathParts[1:], "/")
|
||||
|
@ -229,9 +229,9 @@ func TestPickingRepositoryValidCNAME(t *testing.T) {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
client := GiteaClient{
|
||||
getRepository: func(username, repositoryName string) (Repository, error) {
|
||||
if username == "example-user" && repositoryName == "example-user.local" {
|
||||
if username == "example-user" && repositoryName == "example-user.pages.example.org" {
|
||||
return Repository{
|
||||
Name: "example-user.local",
|
||||
Name: "example-user.pages.example.org",
|
||||
}, nil
|
||||
} else {
|
||||
t.Fatalf("Called with unknown repository %s", repositoryName)
|
||||
@ -239,14 +239,14 @@ func TestPickingRepositoryValidCNAME(t *testing.T) {
|
||||
}
|
||||
},
|
||||
hasBranch: func(username, repositoryName, branchName string) bool {
|
||||
if username == "example-user" && repositoryName == "example-user.local" && branchName == "pages" {
|
||||
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branchName == "pages" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
},
|
||||
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
||||
if username == "example-user" && repositoryName == "example-user.local" && branch == "pages" && path == "CNAME" {
|
||||
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branch == "pages" && path == "CNAME" {
|
||||
return []byte("example-user.local"), true, nil
|
||||
}
|
||||
|
||||
@ -261,11 +261,11 @@ func TestPickingRepositoryValidCNAME(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", &client)
|
||||
repo, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", &client)
|
||||
if err != nil {
|
||||
t.Fatalf("Error returned: %v", err)
|
||||
}
|
||||
if repo.Name != "example-user.local" {
|
||||
if repo.Name != "example-user.pages.example.org" {
|
||||
t.Fatalf("Invalid repository name returned: %s", repo.Name)
|
||||
}
|
||||
}
|
||||
@ -313,7 +313,7 @@ func TestPickingRepositoryValidCNAMEWithTXTLookup(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", &client)
|
||||
repo, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", &client)
|
||||
if err != nil {
|
||||
t.Fatalf("Error returned: %v", err)
|
||||
}
|
||||
@ -364,7 +364,7 @@ func TestPickingRepositoryValidCNAMEWithTXTLookupAndSubdirectory(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "blog/index.html", &client)
|
||||
repo, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "blog/index.html", &client)
|
||||
if err != nil {
|
||||
t.Fatalf("Error returned: %v", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user