def __call__(self, environ, start_response): g = self.config['pylons.app_globals'] http_host = environ.get('HTTP_HOST', 'localhost').lower() domain, s, port = http_host.partition(':') # remember the port try: environ['request_port'] = int(port) except ValueError: pass # localhost is exempt so paster run/shell will work # media_domain doesn't need special processing since it's just ads is_media_only_domain = (is_subdomain(domain, g.media_domain) and g.domain != g.media_domain) if domain == "localhost" or is_media_only_domain: return self.app(environ, start_response) # tell reddit_base to redirect to the appropriate subreddit for # a legacy CNAME if not is_subdomain(domain, g.domain): environ['legacy-cname'] = domain return self.app(environ, start_response) # How many characters to chop off the end of the hostname before # we start looking at subdomains ignored_suffix_len = len(g.domain) # figure out what subdomain we're on, if any subdomains = domain[:-ignored_suffix_len - 1].split('.') sr_redirect = None prefix_parts = [] for subdomain in subdomains[:]: extension = g.extension_subdomains.get(subdomain) # These subdomains are reserved, don't treat them as SR # or language subdomains. if subdomain in g.reserved_subdomains: # Some subdomains are reserved, but also can't be mixed into # the domain prefix for various reasons (permalinks will be # broken, etc.) if subdomain in g.ignored_subdomains: continue prefix_parts.append(subdomain) elif extension: environ['reddit-domain-extension'] = extension elif is_language_subdomain(subdomain): environ['reddit-prefer-lang'] = subdomain else: sr_redirect = subdomain subdomains.remove(subdomain) if 'reddit-prefer-lang' in environ: prefix_parts.insert(0, environ['reddit-prefer-lang']) if prefix_parts: environ['reddit-domain-prefix'] = '.'.join(prefix_parts) # if there was a subreddit subdomain, redirect if sr_redirect and environ.get("FULLPATH"): if not subdomains and g.domain_prefix: subdomains.append(g.domain_prefix) subdomains.append(g.domain) redir = "%s/%s/%s/%s" % ('.'.join(subdomains), g.brander_community_abbr, sr_redirect, environ['FULLPATH']) redir = g.default_scheme + "://" + redir.replace('//', '/') start_response("301 Moved Permanently", [("Location", redir)]) return [""] return self.app(environ, start_response)
def __call__(self, environ, start_response): g = self.config['pylons.app_globals'] http_host = environ.get('HTTP_HOST', 'localhost').lower() domain, s, port = http_host.partition(':') # remember the port try: environ['request_port'] = int(port) except ValueError: pass # localhost is exempt so paster run/shell will work # media_domain doesn't need special processing since it's just ads is_media_only_domain = (is_subdomain(domain, g.media_domain) and g.domain != g.media_domain) if domain == "localhost" or is_media_only_domain: return self.app(environ, start_response) # tell reddit_base to redirect to the appropriate subreddit for # a legacy CNAME if not is_subdomain(domain, g.domain): environ['legacy-cname'] = domain return self.app(environ, start_response) # How many characters to chop off the end of the hostname before # we start looking at subdomains ignored_suffix_len = len(g.domain) # figure out what subdomain we're on, if any subdomains = domain[:-ignored_suffix_len - 1].split('.') sr_redirect = None prefix_parts = [] for subdomain in subdomains[:]: extension = g.extension_subdomains.get(subdomain) # These subdomains are reserved, don't treat them as SR # or language subdomains. if subdomain in g.reserved_subdomains: # Some subdomains are reserved, but also can't be mixed into # the domain prefix for various reasons (permalinks will be # broken, etc.) if subdomain in g.ignored_subdomains: continue prefix_parts.append(subdomain) elif extension: environ['reddit-domain-extension'] = extension elif is_language_subdomain(subdomain): environ['reddit-prefer-lang'] = subdomain else: sr_redirect = subdomain subdomains.remove(subdomain) if 'reddit-prefer-lang' in environ: prefix_parts.insert(0, environ['reddit-prefer-lang']) if prefix_parts: environ['reddit-domain-prefix'] = '.'.join(prefix_parts) # if there was a subreddit subdomain, redirect if sr_redirect and environ.get("FULLPATH"): if not subdomains and g.domain_prefix: subdomains.append(g.domain_prefix) subdomains.append(g.domain) redir = "%s/r/%s/%s" % ('.'.join(subdomains), sr_redirect, environ['FULLPATH']) redir = g.default_scheme + "://" + redir.replace('//', '/') start_response("301 Moved Permanently", [("Location", redir)]) return [""] return self.app(environ, start_response)