Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Secure:always view method decorator for Google App Engine - workaround for issue 7979
import os
import logging
from functools import update_wrapper
def secure(view_method):
"""Decorates view/handler method to redirect to https if requested over http.
Equivalent to handler setting secure:always in app.yaml.
Provided as a workaround for App Engine issue 7979.
Does not decorate the view method if used on a local SDK development server.
References:
* https://developers.google.com/appengine/docs/python/config/appconfig#Secure_URLs
* https://code.google.com/p/googleappengine/issues/detail?id=7979
* https://groups.google.com/d/topic/google-appengine/w3aKx9KxYoU/discussion
* http://stackoverflow.com/questions/13064081/how-to-get-major-appengine-issues-fixed
"""
if os.environ.get('SERVER_SOFTWARE', 'Dev').startswith('Dev'):
return view_method
def secure_view_method(view, *args, **kwargs):
if view.request.scheme == 'http':
secure_url = view.request.url.replace('http://', 'https://', 1)
logging.info(u'redirecting to {url}'. format(url=secure_url))
view.redirect(secure_url, permanent=True)
else:
view_method(view, *args, **kwargs)
update_wrapper(secure_view_method, view_method)
return secure_view_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment