Skip to content

Instantly share code, notes, and snippets.

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.
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)'redirecting to {url}'. format(url=secure_url))
view.redirect(secure_url, permanent=True)
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