mirror of https://github.com/django/django.git
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.
This commit is contained in:
parent
9bfe66164e
commit
c7634cd7fe
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -110,8 +110,8 @@ class WSGIRequest(http.HttpRequest):
|
|||
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):
|
||||
"""
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -85,7 +85,7 @@ class CommonMiddleware(object):
|
|||
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])
|
||||
|
|
|
@ -51,8 +51,8 @@ class LocaleMiddleware(object):
|
|||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
^^^^^
|
||||
|
||||
|
|
Loading…
Reference in New Issue