Django

Code

Changeset 6296

Show
Ignore:
Timestamp:
09/15/07 12:46:03 (1 year ago)
Author:
mtredinnick
Message:

Added a get_host() method to HttpRequest. There is still an http.get_host() version in place, so this is fully backwards compatible.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/views/doc.py

    r5803 r6296  
    66from django.shortcuts import render_to_response 
    77from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist 
    8 from django.http import Http404, get_host 
     8from django.http import Http404 
    99from django.core import urlresolvers 
    1010from django.contrib.admin import utils 
     
    3030    admin_root = request.path[:-len('doc/bookmarklets/')] 
    3131    return render_to_response('admin_doc/bookmarklets.html', { 
    32         'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', get_host(request), admin_root), 
     32        'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', request.get_host(), admin_root), 
    3333    }, context_instance=RequestContext(request)) 
    3434bookmarklets = staff_member_required(bookmarklets) 
  • django/trunk/django/contrib/sites/models.py

    r6180 r6296  
    11from django.db import models 
    22from django.utils.translation import ugettext_lazy as _ 
    3 from django.http import get_host 
    43 
    54SITE_CACHE = {} 
     
    5554    """ 
    5655    def __init__(self, request): 
    57         self.domain = self.name = get_host(request
     56        self.domain = self.name = request.get_host(
    5857 
    5958    def __unicode__(self): 
  • django/trunk/django/core/handlers/base.py

    r6292 r6296  
    143143    this function converts them to absolute paths. 
    144144    """ 
    145     if 'Location' in response and http.get_host(request): 
     145    if 'Location' in response and request.get_host(): 
    146146        response['Location'] = request.build_absolute_uri(response['Location']) 
    147147    return response 
  • django/trunk/django/http/__init__.py

    r6235 r6296  
    4444 
    4545    __contains__ = has_key 
     46 
     47    def get_host(self): 
     48        "Returns the HTTP host using the environment or request headers." 
     49        # We try three options, in order of decreasing preference. 
     50        host = self.META.get('HTTP_X_FORWARDED_HOST', '') 
     51        if 'HTTP_HOST' in self.META: 
     52            host = self.META['HTTP_HOST'] 
     53        else: 
     54            # Reconstruct the host using the algorithm from PEP 333. 
     55            host = self.META['SERVER_NAME'] 
     56            server_port = self.META['SERVER_PORT'] 
     57            if server_port != (self.is_secure() and 443 or 80): 
     58                host = '%s:%s' % (host, server_port) 
     59        return host 
    4660 
    4761    def get_full_path(self): 
     
    5872        if not ':' in location: 
    5973            current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http', 
    60                                          get_host(self), self.path) 
     74                                         self.get_host(), self.path) 
    6175            location = urljoin(current_uri, location) 
    6276        return location 
     
    382396        HttpResponse.__init__(self, *args, **kwargs) 
    383397 
     398# A backwards compatible alias for HttpRequest.get_host. 
    384399def get_host(request): 
    385     "Gets the HTTP host from the environment or request headers." 
    386     # We try three options, in order of decreasing preference. 
    387     host = request.META.get('HTTP_X_FORWARDED_HOST', '') 
    388     if 'HTTP_HOST' in request.META: 
    389         host = request.META['HTTP_HOST'] 
    390     else: 
    391         # Reconstruct the host using the algorithm from PEP 333. 
    392         host = request.META['SERVER_NAME'] 
    393         server_port = request.META['SERVER_PORT'] 
    394         if server_port != (request.is_secure() and 443 or 80): 
    395             host = '%s:%s' % (host, server_port) 
    396     return host 
     400    return request.get_host() 
    397401 
    398402# It's neither necessary nor appropriate to use 
  • django/trunk/django/middleware/common.py

    r5878 r6296  
    3333 
    3434        # Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW 
    35         host = http.get_host(request
     35        host = request.get_host(
    3636        old_url = [host, request.path] 
    3737        new_url = old_url[:] 
     
    6262                # If the referrer was from an internal link or a non-search-engine site, 
    6363                # send a note to the managers. 
    64                 domain = http.get_host(request
     64                domain = request.get_host(
    6565                referer = request.META.get('HTTP_REFERER', None) 
    6666                is_internal = _is_internal_request(domain, referer)