From 60ac9925229aaa00e16df0b6563376210233b6c5 Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Mon, 17 Oct 2011 12:28:50 -0700 Subject: [PATCH] Add is_subdomain helper for proper subdomain checking. --- r2/r2/lib/utils/utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/r2/r2/lib/utils/utils.py b/r2/r2/lib/utils/utils.py index 2b2236ec1..c9a0c078d 100644 --- a/r2/r2/lib/utils/utils.py +++ b/r2/r2/lib/utils/utils.py @@ -52,8 +52,7 @@ def randstr(len, reallyrandom = False): for i in range(len)) def is_authorized_cname(domain, cnames): - return any((domain == cname or domain.endswith('.' + cname)) - for cname in cnames) + return any(is_subdomain(domain, cname) for cname in cnames) class Storage(dict): """ @@ -212,6 +211,10 @@ def strip_www(domain): else: return domain +def is_subdomain(subdomain, base): + """Check if a domain is equal to or a subdomain of a base domain.""" + return subdomain == base or subdomain.endswith('.' + base) + r_base_url = re.compile("(?i)(?:.+?://)?(?:www[\d]*\.)?([^#]*[^#/])/?") def base_url(url): res = r_base_url.findall(url) @@ -513,10 +516,10 @@ class UrlParser(object): """ from pylons import g return (not self.hostname or - self.hostname.endswith(g.domain) or + is_subdomain(self.hostname, g.domain) or is_authorized_cname(self.hostname, g.authorized_cnames) or (subreddit and subreddit.domain and - self.hostname.endswith(subreddit.domain))) + is_subdomain(self.hostname, subreddit.domain))) def path_add_subreddit(self, subreddit): """