Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 malcolmt authored
Showing with 9 additions and 2 deletions.
  1. +9 −2 django/http/__init__.py
11 django/http/__init__.py
View
@@ -379,9 +379,16 @@ def __init__(self, *args, **kwargs):
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
Please sign in to comment.
Something went wrong with that request. Please try again.