Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 4b610f42d36a39cd975122ab46f85323844c2c72 1 parent 061ed82
Malcolm Tredinnick authored September 15, 2007
4  django/contrib/admin/views/doc.py
@@ -5,7 +5,7 @@
5 5
 from django.db import models
6 6
 from django.shortcuts import render_to_response
7 7
 from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
8  
-from django.http import Http404, get_host
  8
+from django.http import Http404
9 9
 from django.core import urlresolvers
10 10
 from django.contrib.admin import utils
11 11
 from django.contrib.sites.models import Site
@@ -29,7 +29,7 @@ def bookmarklets(request):
29 29
     # Hack! This couples this view to the URL it lives at.
30 30
     admin_root = request.path[:-len('doc/bookmarklets/')]
31 31
     return render_to_response('admin_doc/bookmarklets.html', {
32  
-        'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', get_host(request), admin_root),
  32
+        'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', request.get_host(), admin_root),
33 33
     }, context_instance=RequestContext(request))
34 34
 bookmarklets = staff_member_required(bookmarklets)
35 35
 
3  django/contrib/sites/models.py
... ...
@@ -1,6 +1,5 @@
1 1
 from django.db import models
2 2
 from django.utils.translation import ugettext_lazy as _
3  
-from django.http import get_host
4 3
 
5 4
 SITE_CACHE = {}
6 5
 
@@ -54,7 +53,7 @@ class RequestSite(object):
54 53
     The save() and delete() methods raise NotImplementedError.
55 54
     """
56 55
     def __init__(self, request):
57  
-        self.domain = self.name = get_host(request)
  56
+        self.domain = self.name = request.get_host()
58 57
 
59 58
     def __unicode__(self):
60 59
         return self.domain
2  django/core/handlers/base.py
@@ -142,7 +142,7 @@ def fix_location_header(request, response):
142 142
     Code constructing response objects is free to insert relative paths and
143 143
     this function converts them to absolute paths.
144 144
     """
145  
-    if 'Location' in response and http.get_host(request):
  145
+    if 'Location' in response and request.get_host():
146 146
         response['Location'] = request.build_absolute_uri(response['Location'])
147 147
     return response
148 148
 
30  django/http/__init__.py
@@ -44,6 +44,20 @@ def has_key(self, key):
44 44
 
45 45
     __contains__ = has_key
46 46
 
  47
+    def get_host(self):
  48
+        "Returns the HTTP host using the environment or request headers."
  49
+        # We try three options, in order of decreasing preference.
  50
+        host = self.META.get('HTTP_X_FORWARDED_HOST', '')
  51
+        if 'HTTP_HOST' in self.META:
  52
+            host = self.META['HTTP_HOST']
  53
+        else:
  54
+            # Reconstruct the host using the algorithm from PEP 333.
  55
+            host = self.META['SERVER_NAME']
  56
+            server_port = self.META['SERVER_PORT']
  57
+            if server_port != (self.is_secure() and 443 or 80):
  58
+                host = '%s:%s' % (host, server_port)
  59
+        return host
  60
+
47 61
     def get_full_path(self):
48 62
         return ''
49 63
 
@@ -57,7 +71,7 @@ def build_absolute_uri(self, location=None):
57 71
             location = self.get_full_path()
58 72
         if not ':' in location:
59 73
             current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
60  
-                                         get_host(self), self.path)
  74
+                                         self.get_host(), self.path)
61 75
             location = urljoin(current_uri, location)
62 76
         return location
63 77
 
@@ -381,19 +395,9 @@ class HttpResponseServerError(HttpResponse):
381 395
     def __init__(self, *args, **kwargs):
382 396
         HttpResponse.__init__(self, *args, **kwargs)
383 397
 
  398
+# A backwards compatible alias for HttpRequest.get_host.
384 399
 def get_host(request):
385  
-    "Gets the HTTP host from the environment or request headers."
386  
-    # We try three options, in order of decreasing preference.
387  
-    host = request.META.get('HTTP_X_FORWARDED_HOST', '')
388  
-    if 'HTTP_HOST' in request.META:
389  
-        host = request.META['HTTP_HOST']
390  
-    else:
391  
-        # Reconstruct the host using the algorithm from PEP 333.
392  
-        host = request.META['SERVER_NAME']
393  
-        server_port = request.META['SERVER_PORT']
394  
-        if server_port != (request.is_secure() and 443 or 80):
395  
-            host = '%s:%s' % (host, server_port)
396  
-    return host
  400
+    return request.get_host()
397 401
 
398 402
 # It's neither necessary nor appropriate to use
399 403
 # django.utils.encoding.smart_unicode for parsing URLs and form inputs. Thus,
4  django/middleware/common.py
@@ -32,7 +32,7 @@ def process_request(self, request):
32 32
                     return http.HttpResponseForbidden('<h1>Forbidden</h1>')
33 33
 
34 34
         # Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW
35  
-        host = http.get_host(request)
  35
+        host = request.get_host()
36 36
         old_url = [host, request.path]
37 37
         new_url = old_url[:]
38 38
         if settings.PREPEND_WWW and old_url[0] and not old_url[0].startswith('www.'):
@@ -61,7 +61,7 @@ def process_response(self, request, response):
61 61
             if settings.SEND_BROKEN_LINK_EMAILS:
62 62
                 # If the referrer was from an internal link or a non-search-engine site,
63 63
                 # send a note to the managers.
64  
-                domain = http.get_host(request)
  64
+                domain = request.get_host()
65 65
                 referer = request.META.get('HTTP_REFERER', None)
66 66
                 is_internal = _is_internal_request(domain, referer)
67 67
                 path = request.get_full_path()

0 notes on commit 4b610f4

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