summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-09-14 05:39:42 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-09-14 05:39:42 +0000
commit300e19effca99319991d5ca0323bfc9a869a0515 (patch)
treedee4bd7055b975fa31976577651230320f6eae6b
parent30b24a6ccece2131aa2fd931ac157cda5a2bb1b6 (diff)
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
-rw-r--r--django/http/__init__.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/django/http/__init__.py b/django/http/__init__.py
index 74764690a3..4421573258 100644
--- a/django/http/__init__.py
+++ b/django/http/__init__.py
@@ -379,9 +379,16 @@ class HttpResponseServerError(HttpResponse):
def get_host(request):
"Gets the HTTP host from the environment or request headers."
+ # We try three options, in order of decreasing preference.
host = request.META.get('HTTP_X_FORWARDED_HOST', '')
- if not host:
- host = request.META.get('HTTP_HOST', '')
+ if 'HTTP_HOST' in request.META:
+ host = request.META['HTTP_HOST']
+ else:
+ # Reconstruct the host using the algorithm from PEP 333.
+ host = request.META['SERVER_NAME']
+ server_port = request.META['SERVER_PORT']
+ if server_port != (request.is_secure() and 443 or 80):
+ host = '%s:%s' % (host, server_port)
return host
# It's neither necessary nor appropriate to use