Django

Code

Changeset 6166

Show
Ignore:
Timestamp:
09/14/07 00:39:42 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4986 -- Improved get_host() host detection. Thanks, SmileyChris?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/http/__init__.py

    r6164 r6166  
    380380def get_host(request): 
    381381    "Gets the HTTP host from the environment or request headers." 
     382    # We try three options, in order of decreasing preference. 
    382383    host = request.META.get('HTTP_X_FORWARDED_HOST', '') 
    383     if not host: 
    384         host = request.META.get('HTTP_HOST', '') 
     384    if 'HTTP_HOST' in request.META: 
     385        host = request.META['HTTP_HOST'] 
     386    else: 
     387        # Reconstruct the host using the algorithm from PEP 333. 
     388        host = request.META['SERVER_NAME'] 
     389        server_port = request.META['SERVER_PORT'] 
     390        if server_port != (request.is_secure() and 443 or 80): 
     391            host = '%s:%s' % (host, server_port) 
    385392    return host 
    386393