'Process requests for unhandled domains
If the request is not for our ``PUBLIC_DOMAIN``, or if ``PUBLIC_DOMAIN``
is not set and the request is for a subdomain on ``PRODUCTION_DOMAIN``,
process the request as a request a documentation project.'

def process_request(self, request):

    if (not getattr(settings, 'USE_SUBDOMAIN', False)):
        return None
    full_host = host = request.get_host().lower()
    path = request.get_full_path()
    log_kwargs = dict(host=host, path=path)
    public_domain = getattr(settings, 'PUBLIC_DOMAIN', None)
    production_domain = getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org')
    if (public_domain is None):
        public_domain = production_domain
    if (':' in host):
        host = host.split(':')[0]
    domain_parts = host.split('.')
    if (len(domain_parts) == (len(public_domain.split('.')) + 1)):
        subdomain = domain_parts[0]
        is_www = (subdomain.lower() == 'www')
        if ((not is_www) and ((public_domain in host) or (public_domain in full_host))):
            request.subdomain = True
            request.slug = subdomain
            request.urlconf = SUBDOMAIN_URLCONF
            return None
    if ((public_domain not in host) and (production_domain not in host) and ('localhost' not in host) and ('testserver' not in host)):
        request.cname = True
        domains = Domain.objects.filter(domain=host)
        if domains.count():
            for domain in domains:
                if (domain.domain == host):
                    request.slug = domain.project.slug
                    request.urlconf = SUBDOMAIN_URLCONF
                    request.domain_object = True
                    log.debug(LOG_TEMPLATE.format(msg=('Domain    Object    Detected:    %s' % domain.domain), **log_kwargs))
                    break
        if ((not hasattr(request, 'domain_object')) and ('HTTP_X_RTD_SLUG' in request.META)):
            request.slug = request.META['HTTP_X_RTD_SLUG'].lower()
            request.urlconf = SUBDOMAIN_URLCONF
            request.rtdheader = True
            log.debug(LOG_TEMPLATE.format(msg=('X-RTD-Slug    header    detected:    %s' % request.slug), **log_kwargs))
        elif (not hasattr(request, 'domain_object')):
            try:
                slug = cache.get(host)
                if (not slug):
                    slug = cname_to_slug(host)
                    cache.set(host, slug, (60 * 60))
                    log.debug(LOG_TEMPLATE.format(msg=('CNAME    cached:    %s->%s' % (slug, host)), **log_kwargs))
                request.slug = slug
                request.urlconf = SUBDOMAIN_URLCONF
                log.debug(LOG_TEMPLATE.format(msg=('CNAME    detected:    %s' % request.slug), **log_kwargs))
            except:
                log.exception(LOG_TEMPLATE.format(msg='CNAME    404', **log_kwargs))
                raise Http404(_('Invalid    hostname'))
    if ((len(domain_parts) > 3) and (not settings.DEBUG)):
        if (domain_parts[0] == 'www'):
            log.debug(LOG_TEMPLATE.format(msg='404ing    long    domain', **log_kwargs))
            return HttpResponseBadRequest(_('Invalid    hostname'))
        log.debug(LOG_TEMPLATE.format(msg='Allowing    long    domain    name', **log_kwargs))
    return None
