How to use WSGI to redirect a user from http to https

Original question


App Engine SDK 1.6.5
Python 2.7
webapp2

I have implemented webapp2 schemes to protect pages up to https. The problem is that when the user goes to http: // site / login instead of https: // site / login, they get a 404 error due to the scheme not recognizing the route.

example main.py

# Libraries import webapp2 # Local Controllers from controllers.HomeHandler import HomeHandler from controllers.LoginHandler import LoginHandler app = webapp2.WSGIApplication([ webapp2.Route(r'/', HomeHandler), webapp2.Route(r'/login', LoginHandler, schemes=['https'], name='login') ], debug=True) 

I added another route / controller below the https route to catch http requests:
webapp2.Route(r'/login', RouteLogin)

RouteLogin.py

 # Libraries import webapp2 class RouteLogin(webapp2.RequestHandler): def get(self): self.redirect('https://site.appspot.com/login') 

This works, but there seems to be a better way to do this. Similar to using htaccess on an Apache web server. This is too much like hacking for me. I really don't like hardcoded URLs in my code. Not to mention that these are two requests that do not really matter for logging in, but there may be other examples when this ends up being too expensive.

NOTE 1. If you are considering this solution, keep in mind that using HTTPS schemes also means that you cannot use the dev console without removing SCHEME or setting the variable set to dev.

NOTE 2. I was able to get a programmatic way to serve HTTPS instead of HTTP. I was on the right track with a comment below, but he needs an argument.

webapp2.uri_for('login', _scheme='https')
This will give you the correct https://someapp.appspot.com/login url. Unfortunately, my main problem is not how to deal with people who enter the URL into the address bar without https, and get an error message if I do not use the hack above. Therefore, I'm still looking for a WSGI method for sending revenue requests to HTPPS.

Editing: Note 1 is added and the name is clarified, I thought it was obvious that I used WSGI from the source, not CGI.

+7
source share
2 answers

This is the working code that I used when testing for this question.

Note. The development web server (starting with this version v1.6.5) does not support https, so your WSGI routes will need to be deleted to work in the development environment. You can add them back before deployment or create a variable to set up a circuit that checks the environment, as I did below.

You can force the App Engine Python to redirect the request by specifying app.yaml as:
app.yaml

 application: cgi-vs-wsgi version: 1 runtime: python27 api_version: 1 threadsafe: yes libraries: - name: webapp2 version: latest handlers: - url: /profile script: main.app secure: always - url: /login script: main.app secure: always - url: /.* script: main.app 

Then in main.py you can declare WSGI handlers as usual:

main.py

 import webapp2 import os # Models from models.Shout import Shout # Controllers from controllers.HomeHandler import HomeHandler from controllers.LoginHandler import LoginHandler from controllers.ProfileHandler import ProfileHandler if os.environ['SERVER_SOFTWARE'].startswith('Development'): app_scheme = 'http' else: app_scheme = 'https' app = webapp.WSGIApplication([ webapp2.Route(r'/login', LoginHandler, name='login', schemes=[app_scheme]), webapp2.Route(r'/profile', ProfileHandler, name='profile', schemes=[app_scheme]), webapp2.Route(r'/', HomeHandler) ], debug=True) 

I downloaded the code for this application in AE-BaseApp GitHub , feel free to download and use it in my applications. Code licensed by Apache License 2.0.

+1
source

Set the urls in app.yaml, not in the code. See https://developers.google.com/appengine/docs/python/config/appconfig#Secure_URLs

For example:

 handlers: - url: /foo/.* script: accounts.py secure: always 

This will redirect HTTP to HTTPS.

+8
source

All Articles