Returns true iff hostName matches the domain name pattern with case-insensitive. Wildcard pattern rules: A single asterisk ( ) matches any domain. Asterisk ( ) is only permitted in the left-most or the right-most part of the pattern, but not both.</l
(String hostName, String pattern)
| 92 | * </ol> |
| 93 | */ |
| 94 | private static boolean matchHostName(String hostName, String pattern) { |
| 95 | checkArgument(hostName.length() != 0 && !hostName.startsWith(".") && !hostName.endsWith("."), |
| 96 | "Invalid host name"); |
| 97 | checkArgument(pattern.length() != 0 && !pattern.startsWith(".") && !pattern.endsWith("."), |
| 98 | "Invalid pattern/domain name"); |
| 99 | |
| 100 | hostName = hostName.toLowerCase(Locale.US); |
| 101 | pattern = pattern.toLowerCase(Locale.US); |
| 102 | // hostName and pattern are now in lower case -- domain names are case-insensitive. |
| 103 | |
| 104 | if (!pattern.contains("*")) { |
| 105 | // Not a wildcard pattern -- hostName and pattern must match exactly. |
| 106 | return hostName.equals(pattern); |
| 107 | } |
| 108 | // Wildcard pattern |
| 109 | |
| 110 | if (pattern.length() == 1) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | int index = pattern.indexOf('*'); |
| 115 | |
| 116 | // At most one asterisk (*) is allowed. |
| 117 | if (pattern.indexOf('*', index + 1) != -1) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // Asterisk can only match prefix or suffix. |
| 122 | if (index != 0 && index != pattern.length() - 1) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // HostName must be at least as long as the pattern because asterisk has to |
| 127 | // match one or more characters. |
| 128 | if (hostName.length() < pattern.length()) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | if (index == 0 && hostName.endsWith(pattern.substring(1))) { |
| 133 | // Prefix matching fails. |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | // Pattern matches hostname if suffix matching succeeds. |
| 138 | return index == pattern.length() - 1 |
| 139 | && hostName.startsWith(pattern.substring(0, pattern.length() - 1)); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns {@code true} iff the given {@link RouteMatch} matches the RPC's full method name and |
no test coverage detected