Created
August 19, 2010 09:11
-
-
Save rozza/537457 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect | |
class SecureRequiredMiddleware(object): | |
def process_response(self, request, response): | |
""" | |
Ensures that any non decorated secure_required urls redirect if accessed | |
via https (only GET requests returning 200's are redirected to HTTP urls) | |
""" | |
if request.method != 'GET': | |
return response | |
secure_required = getattr(request, 'secure_required', False) | |
if request.is_secure() and not secure_required and response.status_code == 200: | |
request_url = request.build_absolute_uri(request.get_full_path()) | |
http_url = request_url.replace('https://', 'http://') | |
return HttpResponseRedirect(http_url) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment