diff options
author | Unai Zalakain <unai@gisa-elkartea.org> | 2013-10-08 20:30:29 +0200 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2013-10-15 09:04:12 -0400 |
commit | c7634cd7fe7dc09338fcec0ca48d816a29d791b0 (patch) | |
tree | e7e2d0d4c17dcaa6ed8eaf3f61741409ea988aae /django/http/request.py | |
parent | 9bfe66164e0e214abc6063a1a60ce729094d0632 (diff) | |
download | django-c7634cd7fe7dc09338fcec0ca48d816a29d791b0.tar.gz |
Fixed #7603 -- Added a 'scheme' property to the HttpRequest object
`HttpRequest.scheme` is `https` if `settings.SECURE_PROXY_SSL_HEADER` is
appropriately set and falls back to `HttpRequest._get_scheme()` (a hook
for subclasses to implement) otherwise.
`WSGIRequest._get_scheme()` makes use of the `wsgi.url_scheme` WSGI
environ variable to determine the request scheme.
`HttpRequest.is_secure()` simply checks if `HttpRequest.scheme` is
`https`.
This provides a way to check the current scheme in templates, for example.
It also allows us to deal with other schemes.
Thanks nslater for the suggestion.
Diffstat (limited to 'django/http/request.py')
-rw-r--r-- | django/http/request.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/django/http/request.py b/django/http/request.py index 3972151865..1cecbe191a 100644 --- a/django/http/request.py +++ b/django/http/request.py @@ -129,15 +129,16 @@ class HttpRequest(object): if not location: location = self.get_full_path() if not absolute_http_url_re.match(location): - current_uri = '%s://%s%s' % ('https' if self.is_secure() else 'http', + current_uri = '%s://%s%s' % (self.scheme, self.get_host(), self.path) location = urljoin(current_uri, location) return iri_to_uri(location) - def _is_secure(self): - return os.environ.get("HTTPS") == "on" + def _get_scheme(self): + return 'https' if os.environ.get("HTTPS") == "on" else 'http' - def is_secure(self): + @property + def scheme(self): # First, check the SECURE_PROXY_SSL_HEADER setting. if settings.SECURE_PROXY_SSL_HEADER: try: @@ -145,11 +146,13 @@ class HttpRequest(object): except ValueError: raise ImproperlyConfigured('The SECURE_PROXY_SSL_HEADER setting must be a tuple containing two values.') if self.META.get(header, None) == value: - return True - - # Failing that, fall back to _is_secure(), which is a hook for + return 'https' + # Failing that, fall back to _get_scheme(), which is a hook for # subclasses to implement. - return self._is_secure() + return self._get_scheme() + + def is_secure(self): + return self.scheme == 'https' def is_ajax(self): return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' |