<s> """<STR_LIT>""" <EOL> try : <EOL> from urlparse import urlparse <EOL> except ImportError : <EOL> from urllib . parse import urlparse <EOL> __version__ = '<STR_LIT>' <EOL> __author__ = '<STR_LIT>' <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> def turbolinks ( app ) : <EOL> """<STR_LIT>""" <EOL> from flask import request , session <EOL> app . wsgi_app = TurbolinksMiddleware ( app . wsgi_app ) <EOL> @ app . after_request <EOL> def turbolinks_response ( response ) : <EOL> referrer = request . headers . get ( '<STR_LIT>' ) <EOL> if not referrer : <EOL> return response <EOL> method = request . cookies . get ( '<STR_LIT>' , None ) <EOL> if not method or method != request . method : <EOL> response . set_cookie ( '<STR_LIT>' , request . method ) <EOL> if '<STR_LIT>' in response . headers : <EOL> loc = response . headers [ '<STR_LIT>' ] <EOL> session [ '<STR_LIT>' ] = loc <EOL> if referrer and not same_origin ( loc , referrer ) : <EOL> response . status_code = <NUM_LIT:200> <EOL> response . data = ( <EOL> '<STR_LIT>' <EOL> ) % loc <EOL> else : <EOL> if '<STR_LIT>' in session : <EOL> loc = session . pop ( '<STR_LIT>' ) <EOL> response . headers [ '<STR_LIT>' ] = loc <EOL> return response <EOL> return app <EOL> class TurbolinksMiddleware ( object ) : <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> def __call__ ( self , environ , start_response ) : <EOL> referrer = environ . get ( '<STR_LIT>' ) <EOL> if referrer : <EOL> environ [ '<STR_LIT>' ] = referrer <EOL> return self . app ( environ , start_response ) <EOL> def same_origin ( current_uri , redirect_uri ) : <EOL> parsed_uri = urlparse ( current_uri ) <EOL> if not parsed_uri . scheme : <EOL> return True <EOL> parsed_redirect = urlparse ( redirect_uri ) <EOL> if parsed_uri . scheme != parsed_redirect . scheme : <EOL> return False <EOL> if parsed_uri . hostname != parsed_redirect . hostname : <EOL> return False <EOL> if parsed_uri . port != parsed_redirect . port : <EOL> return False <EOL> return True </s>
