<s> from flask import request , redirect , current_app <EOL> YEAR_IN_SECS = <NUM_LIT> <EOL> class SSLify ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app = None , age = YEAR_IN_SECS , subdomains = False , permanent = False , skips = None ) : <EOL> self . app = app or current_app <EOL> self . hsts_age = age <EOL> self . app . config . setdefault ( '<STR_LIT>' , False ) <EOL> self . app . config . setdefault ( '<STR_LIT>' , False ) <EOL> self . app . config . setdefault ( '<STR_LIT>' , None ) <EOL> self . hsts_include_subdomains = subdomains or self . app . config [ '<STR_LIT>' ] <EOL> self . permanent = permanent or self . app . config [ '<STR_LIT>' ] <EOL> self . skip_list = skips or self . app . config [ '<STR_LIT>' ] <EOL> if app is not None : <EOL> self . init_app ( app ) <EOL> def init_app ( self , app ) : <EOL> """<STR_LIT>""" <EOL> app . before_request ( self . redirect_to_ssl ) <EOL> app . after_request ( self . set_hsts_header ) <EOL> @ property <EOL> def hsts_header ( self ) : <EOL> """<STR_LIT>""" <EOL> hsts_policy = '<STR_LIT>' . format ( self . hsts_age ) <EOL> if self . hsts_include_subdomains : <EOL> hsts_policy += '<STR_LIT>' <EOL> return hsts_policy <EOL> @ property <EOL> def skip ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . skip_list and isinstance ( self . skip_list , list ) : <EOL> for skip in self . skip_list : <EOL> if request . path . startswith ( '<STR_LIT>' . format ( skip ) ) : <EOL> return True <EOL> return False <EOL> def redirect_to_ssl ( self ) : <EOL> """<STR_LIT>""" <EOL> criteria = [ <EOL> request . is_secure , <EOL> current_app . debug , <EOL> current_app . testing , <EOL> request . headers . get ( '<STR_LIT>' , '<STR_LIT:http>' ) == '<STR_LIT>' <EOL> ] <EOL> if not any ( criteria ) and not self . skip : <EOL> if request . url . startswith ( '<STR_LIT>' ) : <EOL> url = request . url . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> code = <NUM_LIT> <EOL> if self . permanent : <EOL> code = <NUM_LIT> <EOL> r = redirect ( url , code = code ) <EOL> return r <EOL> def set_hsts_header ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if request . is_secure and not self . skip : <EOL> response . headers . setdefault ( '<STR_LIT>' , self . hsts_header ) <EOL> return response </s>
