func IsHostnameMatch()

in pkg/providers/utils/domain.go [26:70]


func IsHostnameMatch(listener, route string) bool {
	if listener == "" || route == "" {
		return true
	}

	// reverse hostname from "ingress.apisix.com" to "moc.xisipa.ssergni" for matching
	l := ReverseString(listener)
	r := ReverseString(route)

	lLabels := strings.Split(l, ".")
	rLabels := strings.Split(r, ".")
	lLength := len(lLabels)
	rLength := len(rLabels)

	// reverse matching, hostname seq like ["moc", "xisipa", "ssergni"]
	for i, j := 0, 0; i < lLength && j < rLength; i, j = i+1, j+1 {
		if i == (lLength - 1) {
			// wildcard prefix matched
			if lLabels[i] == "*" {
				break
			}

			// "ingress.apisix.com" "apisix.com" mismatched.
			if j != (rLength - 1) {
				return false
			}
		}

		if j == (rLength - 1) {
			if rLabels[j] == "*" {
				break
			}

			if i != (lLength - 1) {
				return false
			}
		}

		if lLabels[i] != rLabels[j] {
			return false
		}
	}

	return true
}