diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2007-09-15 21:34:09 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2007-09-15 21:34:09 +0000 |
| commit | bf6a46d8ad11d49990a3878166cce2af2fc6c4fe (patch) | |
| tree | b24514e1780e8ec80afb9f295005e196af460f15 /django/http | |
| parent | fb6a0c8ffa1cd74c63aaf4b011665e5952d449e7 (diff) | |
queryset-refactor: Merged to [6190]
git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6334 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/http')
| -rw-r--r-- | django/http/__init__.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/django/http/__init__.py b/django/http/__init__.py index 20818f138b..2b68a6243a 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -2,6 +2,7 @@ import os from Cookie import SimpleCookie from pprint import pformat from urllib import urlencode +from urlparse import urljoin from django.utils.datastructures import MultiValueDict, FileDict from django.utils.encoding import smart_str, iri_to_uri, force_unicode @@ -42,10 +43,24 @@ class HttpRequest(object): return key in self.GET or key in self.POST __contains__ = has_key - + def get_full_path(self): return '' + def build_absolute_uri(self, location=None): + """ + Builds an absolute URI from the location and the variables available in + this request. If no location is specified, the absolute URI is built on + ``request.get_full_path()``. + """ + if not location: + 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) + location = urljoin(current_uri, location) + return location + def is_secure(self): return os.environ.get("HTTPS") == "on" @@ -364,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 |
