'Returns the HTTP host using the environment or request headers.'

def get_host(self):

    if (settings.USE_X_FORWARDED_HOST and (u'HTTP_X_FORWARDED_HOST' in self.META)):
        host = self.META[u'HTTP_X_FORWARDED_HOST']
    elif (u'HTTP_HOST' in self.META):
        host = self.META[u'HTTP_HOST']
    else:
        host = self.META[u'SERVER_NAME']
        server_port = str(self.META[u'SERVER_PORT'])
        if (server_port != (u'443' if self.is_secure() else u'80')):
            host = (u'%s:%s' % (host, server_port))
    allowed_hosts = ([u'*'] if settings.DEBUG else settings.ALLOWED_HOSTS)
    (domain, port) = split_domain_port(host)
    if (domain and validate_host(domain, allowed_hosts)):
        return host
    else:
        msg = (u'Invalid    HTTP_HOST    header:    %r.' % host)
        if domain:
            msg += (u'You    may    need    to    add    %r    to    ALLOWED_HOSTS.' % domain)
        raise DisallowedHost(msg)
