Fix IsSubDomain
This commit is contained in:
parent
b5720f99ed
commit
af2f485c8e
28
defaults.go
28
defaults.go
|
@ -273,32 +273,8 @@ func IsDomainName(s string) (uint8, uint8, bool) { // copied from net package.
|
|||
|
||||
// IsSubDomain checks if child is indeed a child of the parent.
|
||||
func IsSubDomain(parent, child string) bool {
|
||||
// If the number of labels both domain name have
|
||||
// in common equals the number of labels of parent,
|
||||
// child is a subdomain of parent.
|
||||
plabs := SplitLabels(parent)
|
||||
clabs := SplitLabels(child)
|
||||
if len(clabs) < len(plabs) {
|
||||
// child is smaller than parent, reversed arguments?
|
||||
return false
|
||||
}
|
||||
// Copied from CompareLabels to prevent another SplitLabels
|
||||
n := 0
|
||||
p := len(plabs) - 1
|
||||
c := len(clabs) - 1
|
||||
for {
|
||||
if p < 0 || c < 0 {
|
||||
break
|
||||
}
|
||||
if plabs[p] == clabs[c] {
|
||||
n++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
p--
|
||||
c--
|
||||
}
|
||||
return n == len(plabs)
|
||||
// Entire child is contained in parent
|
||||
return CompareLabels(child, parent) == LenLabels(child)
|
||||
}
|
||||
|
||||
// IsFqdn checks if a domain name is fully qualified.
|
||||
|
|
|
@ -38,9 +38,6 @@ func SplitLabels(s string) []string {
|
|||
//
|
||||
// www.miek.nl. and miek.nl. have two labels in common: miek and nl
|
||||
// www.miek.nl. and www.bla.nl. have one label in common: nl
|
||||
//
|
||||
// Domain s1 is a subdomain of s2 if
|
||||
// CompareLabels(s1, s2) == LenLabels(s1)
|
||||
func CompareLabels(s1, s2 string) (n int) {
|
||||
l1 := SplitLabels(s1)
|
||||
l2 := SplitLabels(s2)
|
||||
|
@ -62,7 +59,7 @@ func CompareLabels(s1, s2 string) (n int) {
|
|||
return
|
||||
}
|
||||
|
||||
// LenLabels returns the number of labels in a domain name
|
||||
// LenLabels returns the number of labels in a domain name.
|
||||
func LenLabels(s string) (labels int) {
|
||||
if s == "." {
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue