mirror of
https://github.com/1f349/violet.git
synced 2024-11-21 19:01:39 +00:00
Detect no subdomains in GetTopFqdn
This commit is contained in:
parent
11b989b50c
commit
1194717a32
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user