Skip to content

Instantly share code, notes, and snippets.

@bryanchow
Last active February 22, 2016 22:58
Show Gist options
  • Select an option

  • Save bryanchow/6917ddc1823c09b5cc9f to your computer and use it in GitHub Desktop.

Select an option

Save bryanchow/6917ddc1823c09b5cc9f to your computer and use it in GitHub Desktop.

Revisions

  1. bryanchow revised this gist Feb 22, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion sslredirectmiddleware.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@
    from django.conf import settings


    SHOULD_REDIRECT_SSL = getattr(
    settings, 'SHOULD_REDIRECT_SSL', not settings.DEBUG
    )
    SSL_URLS = getattr(settings, 'SSL_URLS', [])
    SSL_IGNORE_URLS = getattr(settings, 'SSL_IGNORE_URLS', [])

    @@ -15,7 +18,7 @@ class SSLRedirectMiddleware:
    ignore_urls = tuple([re.compile(url) for url in SSL_IGNORE_URLS])

    def process_request(self, request):
    if settings.DEBUG:
    if not SHOULD_REDIRECT_SSL:
    return
    secure = False
    ignore = False
  2. bryanchow revised this gist Feb 22, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion sslredirectmiddleware.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # https://gist.github.com/bryanchow/6917ddc1823c09b5cc9f

    import re
    from django.http import HttpResponseRedirect
    from django.conf import settings
    @@ -47,4 +49,4 @@ def _redirect(self, request, secure):
    "data. Please structure your views so that redirects only "
    "occur during GETs."
    )
    return HttpResponseRedirect(new_url)
    return HttpResponseRedirect(new_url)
  3. bryanchow created this gist Feb 22, 2016.
    50 changes: 50 additions & 0 deletions sslredirectmiddleware.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    import re
    from django.http import HttpResponseRedirect
    from django.conf import settings


    SSL_URLS = getattr(settings, 'SSL_URLS', [])
    SSL_IGNORE_URLS = getattr(settings, 'SSL_IGNORE_URLS', [])


    class SSLRedirectMiddleware:

    secure_urls = tuple([re.compile(url) for url in SSL_URLS])
    ignore_urls = tuple([re.compile(url) for url in SSL_IGNORE_URLS])

    def process_request(self, request):
    if settings.DEBUG:
    return
    secure = False
    ignore = False
    for url in self.ignore_urls:
    if url.match(request.path):
    ignore = True
    break
    if not ignore:
    for url in self.secure_urls:
    if url.match(request.path):
    secure = True
    break
    if not secure == self._is_secure(request):
    return self._redirect(request, secure)

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

    def _redirect(self, request, secure):
    protocol = secure and "https" or "http"
    if secure:
    host = getattr(settings, 'SSL_HOST', request.get_host())
    else:
    host = getattr(settings, 'HTTP_HOST', request.get_host())
    new_url = "%s://%s%s" % (protocol, host, 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 HttpResponseRedirect(new_url)