summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-09-15 17:46:03 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-09-15 17:46:03 +0000
commit4b610f42d36a39cd975122ab46f85323844c2c72 (patch)
tree1e656053d74a18c6b33fadabf6a0c0762ba5a735
parent061ed82b22679113d65a69b0a41ba22d3ae618d3 (diff)
Added a get_host() method to HttpRequest. There is still an http.get_host() version in place, so this is fully backwards compatible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6296 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/admin/views/doc.py4
-rw-r--r--django/contrib/sites/models.py3
-rw-r--r--django/core/handlers/base.py2
-rw-r--r--django/http/__init__.py30
-rw-r--r--django/middleware/common.py4
5 files changed, 23 insertions, 20 deletions
diff --git a/django/contrib/admin/views/doc.py b/django/contrib/admin/views/doc.py
index 37caa0f43b..2ebd0cd282 100644
--- a/django/contrib/admin/views/doc.py
+++ b/django/contrib/admin/views/doc.py
@@ -5,7 +5,7 @@ from django.contrib.admin.views.decorators import staff_member_required
from django.db import models
from django.shortcuts import render_to_response
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
-from django.http import Http404, get_host
+from django.http import Http404
from django.core import urlresolvers
from django.contrib.admin import utils
from django.contrib.sites.models import Site
@@ -29,7 +29,7 @@ def bookmarklets(request):
# Hack! This couples this view to the URL it lives at.
admin_root = request.path[:-len('doc/bookmarklets/')]
return render_to_response('admin_doc/bookmarklets.html', {
- 'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', get_host(request), admin_root),
+ 'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', request.get_host(), admin_root),
}, context_instance=RequestContext(request))
bookmarklets = staff_member_required(bookmarklets)
diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
index 253a2722b6..fb6d0a2b1e 100644
--- a/django/contrib/sites/models.py
+++ b/django/contrib/sites/models.py
@@ -1,6 +1,5 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
-from django.http import get_host
SITE_CACHE = {}
@@ -54,7 +53,7 @@ class RequestSite(object):
The save() and delete() methods raise NotImplementedError.
"""
def __init__(self, request):
- self.domain = self.name = get_host(request)
+ self.domain = self.name = request.get_host()
def __unicode__(self):
return self.domain
diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py
index 5f52460ec0..13c7f4193f 100644
--- a/django/core/handlers/base.py
+++ b/django/core/handlers/base.py
@@ -142,7 +142,7 @@ def fix_location_header(request, response):
Code constructing response objects is free to insert relative paths and
this function converts them to absolute paths.
"""
- if 'Location' in response and http.get_host(request):
+ if 'Location' in response and request.get_host():
response['Location'] = request.build_absolute_uri(response['Location'])
return response
diff --git a/django/http/__init__.py b/django/http/__init__.py
index 1b80237290..9a47e70592 100644
--- a/django/http/__init__.py
+++ b/django/http/__init__.py
@@ -44,6 +44,20 @@ class HttpRequest(object):
__contains__ = has_key
+ def get_host(self):
+ "Returns the HTTP host using the environment or request headers."
+ # We try three options, in order of decreasing preference.
+ host = self.META.get('HTTP_X_FORWARDED_HOST', '')
+ if 'HTTP_HOST' in self.META:
+ host = self.META['HTTP_HOST']
+ else:
+ # Reconstruct the host using the algorithm from PEP 333.
+ host = self.META['SERVER_NAME']
+ server_port = self.META['SERVER_PORT']
+ if server_port != (self.is_secure() and 443 or 80):
+ host = '%s:%s' % (host, server_port)
+ return host
+
def get_full_path(self):
return ''
@@ -57,7 +71,7 @@ class HttpRequest(object):
location = self.get_full_path()
if not ':' in location:
current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
- get_host(self), self.path)
+ self.get_host(), self.path)
location = urljoin(current_uri, location)
return location
@@ -381,19 +395,9 @@ class HttpResponseServerError(HttpResponse):
def __init__(self, *args, **kwargs):
HttpResponse.__init__(self, *args, **kwargs)
+# A backwards compatible alias for HttpRequest.get_host.
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 '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
+ return request.get_host()
# It's neither necessary nor appropriate to use
# django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus,
diff --git a/django/middleware/common.py b/django/middleware/common.py
index e18ec079af..57af4eb481 100644
--- a/django/middleware/common.py
+++ b/django/middleware/common.py
@@ -32,7 +32,7 @@ class CommonMiddleware(object):
return http.HttpResponseForbidden('<h1>Forbidden</h1>')
# Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW
- host = http.get_host(request)
+ host = request.get_host()
old_url = [host, request.path]
new_url = old_url[:]
if settings.PREPEND_WWW and old_url[0] and not old_url[0].startswith('www.'):
@@ -61,7 +61,7 @@ class CommonMiddleware(object):
if settings.SEND_BROKEN_LINK_EMAILS:
# If the referrer was from an internal link or a non-search-engine site,
# send a note to the managers.
- domain = http.get_host(request)
+ domain = request.get_host()
referer = request.META.get('HTTP_REFERER', None)
is_internal = _is_internal_request(domain, referer)
path = request.get_full_path()