SSL Middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
__license__ = "Python"
__copyright__ = "Copyright (C) 2007, Stephen Zabel"
__author__ = "Stephen Zabel - sjzabel@gmail.com"
__contributors__ = "Jay Parlar - parlar@gmail.com"

from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect, get_host

SSL = 'SSL'

class SSLRedirect:
    
    def process_view(self, request, view_func, view_args, view_kwargs):
        if SSL in view_kwargs:
            secure = view_kwargs[SSL]
            del view_kwargs[SSL]
        else:
            secure = False

        if not secure == self._is_secure(request):
            return self._redirect(request, secure)

    def _is_secure(self, request):
        if request.is_secure():
	    return True

        #Handle the Webfaction case until this gets resolved in the request.is_secure()
        if 'HTTP_X_FORWARDED_SSL' in request.META:
            return request.META['HTTP_X_FORWARDED_SSL'] == 'on'

        return False

    def _redirect(self, request, secure):
        protocol = secure and "https" or "http"
        newurl = "%s://%s%s" % (protocol,get_host(request),request.get_full_path())
        if settings.DEBUG and request.method == 'POST':
            raise RuntimeError, \
        """Django can't perform a SSL redirect while maintaining POST data.
           Please structure your views so that redirects only occur during GETs."""

        return HttpResponsePermanentRedirect(newurl)

Comments

parlar (on May 16, 2007):

This middleware would not work, as is, with Webfaction, do to a problem with is_secure(). So I've slightly modified it, and posted the modified version, along with instructions, at http://www.djangosnippets.org/snippets/240/

#

sjzabel (on July 3, 2007):

Cool, I updated my code with your check for the webfaction header. I like the idea of checking both better than having two branches of the code.

Cheers

#

sleytr (on April 23, 2008):

To accept both http and https requests with same view I added "secure!=1 and " to the line 20;

 if secure!=1 and not secure == request.is_secure():

Then marked that view with {'SSL':1} in urls file.

#

sleytr (on April 23, 2008):

Sorry, in my previous comment I forgotten the fact that 1==True in Python. So, the value "1" must be changed with some another value. I changed them with -1.

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.