def _get_ranked_matches(self, target_name): """Returns a ranked list of vhosts that match target_name. :param str target_name: The name to match :returns: list of dicts containing the vhost, the matching name, and the numerical rank :rtype: list """ # Nginx chooses a matching server name for a request with precedence: # 1. exact name match # 2. longest wildcard name starting with * # 3. longest wildcard name ending with * # 4. first matching regex in order of appearance in the file matches = [] for vhost in self.parser.get_vhosts(): name_type, name = parser.get_best_match(target_name, vhost.names) if name_type == "exact": matches.append({"vhost": vhost, "name": name, "rank": 0 if vhost.ssl else 1}) elif name_type == "wildcard_start": matches.append({"vhost": vhost, "name": name, "rank": 2 if vhost.ssl else 3}) elif name_type == "wildcard_end": matches.append({"vhost": vhost, "name": name, "rank": 4 if vhost.ssl else 5}) elif name_type == "regex": matches.append({"vhost": vhost, "name": name, "rank": 6 if vhost.ssl else 7}) return sorted(matches, key=lambda x: x["rank"])
def test_get_best_match(self): target_name = 'www.eff.org' names = [set(['www.eff.org', 'irrelevant.long.name.eff.org', '*.org']), set(['eff.org', 'ww2.eff.org', 'test.www.eff.org']), set(['*.eff.org', '.www.eff.org']), set(['.eff.org', '*.org']), set(['www.eff.', 'www.eff.*', '*.www.eff.org']), set(['example.com', r'~^(www\.)?(eff.+)', '*.eff.*']), set(['*', r'~^(www\.)?(eff.+)']), set(['www.*', r'~^(www\.)?(eff.+)', '.test.eff.org']), set(['*.org', r'*.eff.org', 'www.eff.*']), set(['*.www.eff.org', 'www.*']), set(['*.org']), set([]), set(['example.com'])] winners = [('exact', 'www.eff.org'), (None, None), ('exact', '.www.eff.org'), ('wildcard_start', '.eff.org'), ('wildcard_end', 'www.eff.*'), ('regex', r'~^(www\.)?(eff.+)'), ('wildcard_start', '*'), ('wildcard_end', 'www.*'), ('wildcard_start', '*.eff.org'), ('wildcard_end', 'www.*'), ('wildcard_start', '*.org'), (None, None), (None, None)] for i, winner in enumerate(winners): self.assertEqual(winner, parser.get_best_match(target_name, names[i]))
def test_get_best_match(self): target_name = 'www.eff.org' names = [ set(['www.eff.org', 'irrelevant.long.name.eff.org', '*.org']), set(['eff.org', 'ww2.eff.org', 'test.www.eff.org']), set(['*.eff.org', '.www.eff.org']), set(['.eff.org', '*.org']), set(['www.eff.', 'www.eff.*', '*.www.eff.org']), set(['example.com', r'~^(www\.)?(eff.+)', '*.eff.*']), set(['*', r'~^(www\.)?(eff.+)']), set(['www.*', r'~^(www\.)?(eff.+)', '.test.eff.org']), set(['*.org', r'*.eff.org', 'www.eff.*']), set(['*.www.eff.org', 'www.*']), set(['*.org']), set([]), set(['example.com']) ] winners = [('exact', 'www.eff.org'), (None, None), ('exact', '.www.eff.org'), ('wildcard_start', '.eff.org'), ('wildcard_end', 'www.eff.*'), ('regex', r'~^(www\.)?(eff.+)'), ('wildcard_start', '*'), ('wildcard_end', 'www.*'), ('wildcard_start', '*.eff.org'), ('wildcard_end', 'www.*'), ('wildcard_start', '*.org'), (None, None), (None, None)] for i, winner in enumerate(winners): self.assertEqual(winner, parser.get_best_match(target_name, names[i]))
def _get_ranked_matches(self, target_name): """Returns a ranked list of vhosts that match target_name. The ranking gives preference to SSL vhosts. :param str target_name: The name to match :returns: list of dicts containing the vhost, the matching name, and the numerical rank :rtype: list """ # Nginx chooses a matching server name for a request with precedence: # 1. exact name match # 2. longest wildcard name starting with * # 3. longest wildcard name ending with * # 4. first matching regex in order of appearance in the file matches = [] for vhost in self.parser.get_vhosts(): name_type, name = parser.get_best_match(target_name, vhost.names) if name_type == 'exact': matches.append({'vhost': vhost, 'name': name, 'rank': 0 if vhost.ssl else 1}) elif name_type == 'wildcard_start': matches.append({'vhost': vhost, 'name': name, 'rank': 2 if vhost.ssl else 3}) elif name_type == 'wildcard_end': matches.append({'vhost': vhost, 'name': name, 'rank': 4 if vhost.ssl else 5}) elif name_type == 'regex': matches.append({'vhost': vhost, 'name': name, 'rank': 6 if vhost.ssl else 7}) return sorted(matches, key=lambda x: x['rank'])
def _get_ranked_matches(self, target_name): """Returns a ranked list of vhosts that match target_name. :param str target_name: The name to match :returns: list of dicts containing the vhost, the matching name, and the numerical rank :rtype: list """ # Nginx chooses a matching server name for a request with precedence: # 1. exact name match # 2. longest wildcard name starting with * # 3. longest wildcard name ending with * # 4. first matching regex in order of appearance in the file matches = [] for vhost in self.parser.get_vhosts(): name_type, name = parser.get_best_match(target_name, vhost.names) if name_type == 'exact': matches.append({'vhost': vhost, 'name': name, 'rank': 0 if vhost.ssl else 1}) elif name_type == 'wildcard_start': matches.append({'vhost': vhost, 'name': name, 'rank': 2 if vhost.ssl else 3}) elif name_type == 'wildcard_end': matches.append({'vhost': vhost, 'name': name, 'rank': 4 if vhost.ssl else 5}) elif name_type == 'regex': matches.append({'vhost': vhost, 'name': name, 'rank': 6 if vhost.ssl else 7}) return sorted(matches, key=lambda x: x['rank'])