Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit c7634cd7fe7dc09338fcec0ca48d816a29d791b0 1 parent 9bfe661
@unaizalakain unaizalakain authored timgraham committed
View
2  django/contrib/admindocs/views.py
@@ -38,7 +38,7 @@ def bookmarklets(request):
admin_root = urlresolvers.reverse('admin:index')
return render_to_response('admin_doc/bookmarklets.html', {
'root_path': admin_root,
- 'admin_url': "%s://%s%s" % ('https' if request.is_secure() else 'http', request.get_host(), admin_root),
+ 'admin_url': "%s://%s%s" % (request.scheme, request.get_host(), admin_root),
}, context_instance=RequestContext(request))
@staff_member_required
View
2  django/contrib/contenttypes/views.py
@@ -75,7 +75,7 @@ def shortcut(request, content_type_id, object_id):
# If all that malarkey found an object domain, use it. Otherwise, fall back
# to whatever get_absolute_url() returned.
if object_domain is not None:
- protocol = 'https' if request.is_secure() else 'http'
+ protocol = request.scheme
return http.HttpResponseRedirect('%s://%s%s'
% (protocol, object_domain, absurl))
else:
View
2  django/contrib/gis/sitemaps/views.py
@@ -21,7 +21,7 @@ def index(request, sitemaps):
"""
current_site = get_current_site(request)
sites = []
- protocol = 'https' if request.is_secure() else 'http'
+ protocol = request.scheme
for section, site in sitemaps.items():
if callable(site):
pages = site().paginator.num_pages
View
4 django/contrib/sitemaps/views.py
@@ -22,7 +22,7 @@ def index(request, sitemaps,
template_name='sitemap_index.xml', content_type='application/xml',
sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
- req_protocol = 'https' if request.is_secure() else 'http'
+ req_protocol = request.scheme
req_site = get_current_site(request)
sites = []
@@ -44,7 +44,7 @@ def index(request, sitemaps,
def sitemap(request, sitemaps, section=None,
template_name='sitemap.xml', content_type='application/xml'):
- req_protocol = 'https' if request.is_secure() else 'http'
+ req_protocol = request.scheme
req_site = get_current_site(request)
if section is not None:
View
4 django/core/handlers/wsgi.py
@@ -110,8 +110,8 @@ def __init__(self, environ):
self._read_started = False
self.resolver_match = None
- def _is_secure(self):
- return self.environ.get('wsgi.url_scheme') == 'https'
+ def _get_scheme(self):
+ return self.environ.get('wsgi.url_scheme')
def _parse_content_type(self, ctype):
"""
View
19 django/http/request.py
@@ -129,15 +129,16 @@ def build_absolute_uri(self, location=None):
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 @@ def is_secure(self):
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'
View
2  django/middleware/common.py
@@ -85,7 +85,7 @@ def process_request(self, request):
return
if new_url[0]:
newurl = "%s://%s%s" % (
- 'https' if request.is_secure() else 'http',
+ request.scheme,
new_url[0], urlquote(new_url[1]))
else:
newurl = urlquote(new_url[1])
View
4 django/middleware/locale.py
@@ -51,8 +51,8 @@ def process_response(self, request, response):
if path_valid:
language_url = "%s://%s/%s%s" % (
- 'https' if request.is_secure() else 'http',
- request.get_host(), language, request.get_full_path())
+ request.scheme, request.get_host(), language,
+ request.get_full_path())
return self.response_redirect_class(language_url)
# Store language back into session if it is not present
View
7 docs/ref/request-response.txt
@@ -32,6 +32,13 @@ Attributes
All attributes should be considered read-only, unless stated otherwise below.
``session`` is a notable exception.
+.. attribute:: HttpRequest.scheme
+
+ .. versionadded:: 1.7
+
+ A string representing the scheme of the request (``http`` or ``https``
+ usually).
+
.. attribute:: HttpRequest.body
The raw HTTP request body as a byte string. This is useful for processing
View
6 docs/releases/1.7.txt
@@ -386,6 +386,12 @@ Templates
<naive_vs_aware_datetimes>` ``datetime`` instances performing the expected
rendering.
+Requests
+^^^^^^^^
+
+* The new :attr:`HttpRequest.scheme <django.http.HttpRequest.scheme>` attribute
+ specifies the scheme of the request (``http`` or ``https`` normally).
+
Tests
^^^^^
Please sign in to comment.
Something went wrong with that request. Please try again.