Detect no subdomains in GetTopFqdn

This commit is contained in:
Melon 2023-10-29 13:15:25 +00:00
parent 11b989b50c
commit 1194717a32
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 12 additions and 14 deletions

View File

@ -65,23 +65,17 @@ func GetParentDomain(domain string) (string, bool) {
//
// hello.world.example.com => example.com
func GetTopFqdn(domain string) (string, bool) {
var countDot int
n := strings.LastIndexFunc(domain, func(r rune) bool {
// return true if this is the second '.'
// otherwise counts one and continues
if r == '.' {
if countDot == 1 {
return true
}
countDot++
}
return false
})
n := strings.LastIndexByte(domain, '.')
// if a valid index isn't found then return false
if n == -1 {
return "", false
}
return domain[n+1:], true
// if a valid index isn't found then this is the fqdn
n2 := strings.LastIndexByte(domain[:n], '.')
if n2 == -1 {
return domain, true
}
return domain[n2+1:], true
}
// SplitHostPath extracts the host/path from the input

View File

@ -52,7 +52,11 @@ func TestGetBaseDomain(t *testing.T) {
}
func TestGetTopFqdn(t *testing.T) {
domain, ok := GetTopFqdn("www.example.com")
domain, ok := GetTopFqdn("example.com")
assert.True(t, ok, "Output should be true")
assert.Equal(t, "example.com", domain)
domain, ok = GetTopFqdn("www.example.com")
assert.True(t, ok, "Output should be true")
assert.Equal(t, "example.com", domain)