2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-09-23 16:21:19 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gstr
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// IsSubDomain checks whether `subDomain` is sub-domain of mainDomain.
|
|
|
|
// It supports '*' in `mainDomain`.
|
2019-09-23 16:21:19 +08:00
|
|
|
func IsSubDomain(subDomain string, mainDomain string) bool {
|
2019-09-23 19:25:03 +08:00
|
|
|
if p := strings.IndexByte(subDomain, ':'); p != -1 {
|
|
|
|
subDomain = subDomain[0:p]
|
|
|
|
}
|
|
|
|
if p := strings.IndexByte(mainDomain, ':'); p != -1 {
|
|
|
|
mainDomain = mainDomain[0:p]
|
|
|
|
}
|
2019-09-23 16:21:19 +08:00
|
|
|
subArray := strings.Split(subDomain, ".")
|
|
|
|
mainArray := strings.Split(mainDomain, ".")
|
|
|
|
subLength := len(subArray)
|
|
|
|
mainLength := len(mainArray)
|
|
|
|
// Eg:
|
|
|
|
// "s.s.goframe.org" is not sub-domain of "*.goframe.org"
|
|
|
|
// but
|
|
|
|
// "s.s.goframe.org" is not sub-domain of "goframe.org"
|
|
|
|
if mainLength > 2 && subLength > mainLength {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
minLength := subLength
|
|
|
|
if mainLength < minLength {
|
|
|
|
minLength = mainLength
|
|
|
|
}
|
|
|
|
for i := minLength; i > 0; i-- {
|
|
|
|
if mainArray[mainLength-i] == "*" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if mainArray[mainLength-i] != subArray[subLength-i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|