Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6166 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 300e19effca99319991d5ca0323bfc9a869a0515 1 parent 30b24a6
Malcolm Tredinnick authored September 14, 2007

Showing 1 changed file with 9 additions and 2 deletions. Show diff stats Hide diff stats

  1. 11  django/http/__init__.py
11  django/http/__init__.py
@@ -379,9 +379,16 @@ def __init__(self, *args, **kwargs):
379 379
 
380 380
 def get_host(request):
381 381
     "Gets the HTTP host from the environment or request headers."
  382
+    # We try three options, in order of decreasing preference.
382 383
     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)
385 392
     return host
386 393
 
387 394
 # It's neither necessary nor appropriate to use

0 notes on commit 300e19e

Please sign in to comment.
Something went wrong with that request. Please try again.