Skip to content

Instantly share code, notes, and snippets.

@devries
Last active June 16, 2025 21:34
Show Gist options
  • Save devries/4a747a284e75a5d63f93 to your computer and use it in GitHub Desktop.
Save devries/4a747a284e75a5d63f93 to your computer and use it in GitHub Desktop.

Revisions

  1. devries revised this gist Jul 28, 2015. No changes.
  2. devries created this gist Jul 28, 2015.
    31 changes: 31 additions & 0 deletions ssl_redirect.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    from urllib import quote

    class SSLRedirect(object):
    def __init__(self,app):
    self.app=app

    def __call__(self,environ,start_response):
    proto = environ.get('HTTP_X_FORWARDED_PROTO') or environ.get('wsgi.url_scheme', 'http')

    if proto=='https':
    return self.app(environ,start_response)

    else:
    url = 'https://'

    if environ.get('HTTP_HOST'):
    url += environ['HTTP_HOST']
    else:
    url += environ['SERVER_NAME']

    url += quote(environ.get('SCRIPT_NAME', ''))
    url += quote(environ.get('PATH_INFO', ''))
    if environ.get('QUERY_STRING'):
    url += '?' + environ['QUERY_STRING']

    status = "301 Moved Permanently"
    headers = [('Location',url),('Content-Length','0')]

    start_response(status,headers)

    return ['']