only serve x-rtd-slug project if it exists
@@ -11,7 +11,7 @@ from django.conf import settings
from django.shortcuts import render
from django.utils.deprecation import MiddlewareMixin
-from readthedocs.projects.models import Domain
+from readthedocs.projects.models import Domain, Project
log = logging.getLogger(__name__) # noqa
@@ -40,14 +40,18 @@ def map_host_to_project_slug(request):
# Explicit Project slug being passed in
if 'HTTP_X_RTD_SLUG' in request.META:
project_slug = request.META['HTTP_X_RTD_SLUG'].lower()
+ if Project.objects.filter(slug=project_slug).exists():
request.rtdheader = True
+ log.info('Setting project based on X_RTD_SLUG header: %s' % project_slug)
+ return project_slug
- elif public_domain in host or host == 'proxito':
+ if public_domain in host or host == 'proxito':
# Serve from the PUBLIC_DOMAIN, ensuring it looks like `foo.PUBLIC_DOMAIN`
if public_domain_parts == host_parts[1:]:
project_slug = host_parts[0]
request.subdomain = True
log.debug('Proxito Public Domain: host=%s', host)
+ return project_slug
else:
# TODO: This can catch some possibly valid domains (docs.readthedocs.io.com) for example
# But these feel like they might be phishing, etc. so let's block them for now.
@@ -57,12 +61,12 @@ def map_host_to_project_slug(request):
)
# Serve CNAMEs
- else:
domain = Domain.objects.filter(domain=host).first()
if domain:
project_slug = domain.project.slug
request.cname = True
log.debug('Proxito CNAME: host=%s', host)
+ return project_slug
else:
# Some person is CNAMEing to us without configuring a domain - 404.
log.debug('CNAME 404: host=%s', host)
@@ -70,9 +74,6 @@ def map_host_to_project_slug(request):
request, 'core/dns-404.html', context={'host': host}, status=404
)
- log.debug('Proxito Project: slug=%s', project_slug)
- return project_slug
-
class ProxitoMiddleware(MiddlewareMixin):
@@ -90,6 +91,8 @@ class ProxitoMiddleware(MiddlewareMixin):
if hasattr(ret, 'status_code'):
return ret
+ log.debug('Proxito Project: slug=%s', ret)
+
# Otherwise set the slug on the request
request.host_project_slug = request.slug = ret
