我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用django.http.request.split_domain_port()。
def _get_site_by_request(self, request): host = request.get_host() try: # First attempt to look up the site by host with or without port. if host not in SITE_CACHE: SITE_CACHE[host] = self.get(domain__iexact=host) return SITE_CACHE[host] except Site.DoesNotExist: # Fallback to looking up site after stripping port from the host. domain, port = split_domain_port(host) if not port: raise if domain not in SITE_CACHE: SITE_CACHE[domain] = self.get(domain__iexact=domain) return SITE_CACHE[domain]
def new_get_current(self, request=None): from django.conf import settings if getattr(settings, 'SITE_ID', ''): site_id = settings.SITE_ID if site_id not in THREADED_SITE_CACHE: with lock: site = self.prefetch_related('settings').filter(pk=site_id)[0] THREADED_SITE_CACHE[site_id] = site return THREADED_SITE_CACHE[site_id] elif request: host = request.get_host() try: # First attempt to look up the site by host with or without port. if host not in THREADED_SITE_CACHE: with lock: site = self.prefetch_related('settings').filter( domain__iexact=host)[0] THREADED_SITE_CACHE[host] = site return THREADED_SITE_CACHE[host] except Site.DoesNotExist: # Fallback to looking up site after stripping port from the host. domain, port = split_domain_port(host) if domain not in THREADED_SITE_CACHE: with lock: site = self.prefetch_related('settings').filter( domain__iexact=domain)[0] THREADED_SITE_CACHE[domain] = site return THREADED_SITE_CACHE[domain] raise ImproperlyConfigured( "You're using the Django \"sites framework\" without having " "set the SITE_ID setting. Create a site in your database and " "set the SITE_ID setting or pass a request to " "Site.objects.get_current() to fix this error." )
def _get_site_by_request(self, request): host = request.get_host() try: # First attempt to look up the site by host with or without port. if host not in SITE_CACHE: SITE_CACHE[host] = self.get(domain__iexact=host) return SITE_CACHE[host] except Site.DoesNotExist: # Fallback to looking up site after stripping port from the host. domain, port = split_domain_port(host) if domain not in SITE_CACHE: SITE_CACHE[domain] = self.get(domain__iexact=domain) return SITE_CACHE[domain]
def process_request(self, request): host = self.get_host(request) domain, port = split_domain_port(host) if not domain: raise DisallowedHost(f'Invalid HTTP_HOST header: {host}.') request.host = domain request.port = int(port) if port else None event_slug = resolve(request.path).kwargs.get('event') if event_slug: event = get_object_or_404(Event, slug__iexact=event_slug) request.event = event if event.settings.custom_domain: custom_domain = urlparse(event.settings.custom_domain) event_domain, event_port = split_domain_port(custom_domain.netloc) if event_domain == domain and event_port == port: request.uses_custom_domain = True return else: request.needs_redirect = True return redirect(urljoin(event.settings.custom_domain, request.path)) default_domain, default_port = split_domain_port(settings.SITE_NETLOC) if domain == default_domain: return if settings.DEBUG or domain in LOCAL_HOST_NAMES: return raise DisallowedHost(f'Unknown host: {host}')
def get_cookie_domain(request): if '.' not in request.host: # As per spec, browsers do not accept cookie domains without dots in it, # e.g. "localhost", see http://curl.haxx.se/rfc/cookie_spec.html return None default_domain, default_port = split_domain_port(settings.SITE_NETLOC) if request.host == default_domain: # We are on our main domain, set the cookie domain the user has chosen return settings.SESSION_COOKIE_DOMAIN else: # We are on an organizer's custom domain, set no cookie domain, as we do not want # the cookies to be present on any other domain. Setting an explicit value can be # dangerous, see http://erik.io/blog/2014/03/04/definitive-guide-to-cookie-domains/ return None
def __call__(self, request): domain, port = split_domain_port(request.get_host()) try: current_site = Site.objects.get(domain=domain) except Site.DoesNotExist: current_site = Site.objects.get(id=settings.SITE_ID) request.site = current_site _thread_local.request = request return self.get_response(request)