<s> import time <EOL> import logging <EOL> import json <EOL> import six <EOL> from pyramid . settings import asbool <EOL> from nefertari . utils import drop_reserved_params <EOL> log = logging . getLogger ( __name__ ) <EOL> def request_timing ( handler , registry ) : <EOL> threshold = float ( registry . settings . get ( <EOL> '<STR_LIT>' , <NUM_LIT:2> ) ) <EOL> log . info ( '<STR_LIT>' % threshold ) <EOL> def timing ( request ) : <EOL> start = time . time ( ) <EOL> try : <EOL> return handler ( request ) <EOL> finally : <EOL> delta = time . time ( ) - start <EOL> msg = '<STR_LIT>' % ( <EOL> request . method , request . url , delta ) <EOL> if delta > threshold : <EOL> log . warning ( msg ) <EOL> else : <EOL> log . debug ( msg ) <EOL> return timing <EOL> def get_tunneling ( handler , registry ) : <EOL> """<STR_LIT>""" <EOL> log . info ( '<STR_LIT>' ) <EOL> def get_tunneling ( request ) : <EOL> if request . method == '<STR_LIT:GET>' : <EOL> method = request . GET . pop ( '<STR_LIT>' , '<STR_LIT:GET>' ) <EOL> request . method = method <EOL> if method in [ '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> get_params = request . GET . mixed ( ) <EOL> valid_params = drop_reserved_params ( get_params ) <EOL> request . body = six . b ( json . dumps ( valid_params ) ) <EOL> request . content_type = '<STR_LIT:application/json>' <EOL> request . _tunneled_get = True <EOL> return handler ( request ) <EOL> return get_tunneling <EOL> def cors ( handler , registry ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> allow_origins_setting = registry . settings . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> allow_origins = [ <EOL> each . strip ( ) for each in allow_origins_setting . split ( '<STR_LIT:U+002C>' ) ] <EOL> allow_credentials = registry . settings . get ( '<STR_LIT>' , None ) <EOL> def cors ( request ) : <EOL> origin = request . headers . get ( '<STR_LIT>' ) or request . host_url <EOL> response = handler ( request ) <EOL> if origin in allow_origins or '<STR_LIT:*>' in allow_origins : <EOL> response . headerlist . append ( ( '<STR_LIT>' , origin ) ) <EOL> if allow_credentials is not None : <EOL> response . headerlist . append ( <EOL> ( '<STR_LIT>' , allow_credentials ) ) <EOL> return response <EOL> if not allow_origins_setting : <EOL> log . warning ( '<STR_LIT>' ) <EOL> else : <EOL> log . info ( '<STR_LIT>' % allow_origins ) <EOL> if allow_credentials is None : <EOL> log . warning ( '<STR_LIT>' ) <EOL> elif asbool ( allow_credentials ) and allow_origins_setting == '<STR_LIT:*>' : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> log . info ( '<STR_LIT>' % allow_credentials ) <EOL> return cors <EOL> def cache_control ( handler , registry ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> def cache_control ( request ) : <EOL> response = handler ( request ) <EOL> add_header = True <EOL> for header in response . headerlist : <EOL> if '<STR_LIT>' in header : <EOL> add_header = False <EOL> if add_header : <EOL> response . cache_expires ( <NUM_LIT:0> ) <EOL> return response <EOL> return cache_control <EOL> def ssl ( handler , registry ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> def ssl ( request ) : <EOL> scheme = request . environ . get ( '<STR_LIT>' ) or request . environ . get ( '<STR_LIT>' ) <EOL> if scheme : <EOL> scheme = scheme . lower ( ) <EOL> log . debug ( '<STR_LIT>' , scheme ) <EOL> request . scheme = request . environ [ '<STR_LIT>' ] = scheme <EOL> return handler ( request ) <EOL> return ssl <EOL> from pyramid . events import ContextFound <EOL> def enable_selfalias ( config , id_name ) : <EOL> """<STR_LIT>""" <EOL> def context_found_subscriber ( event ) : <EOL> request = event . request <EOL> user = getattr ( request , '<STR_LIT:user>' , None ) <EOL> if ( request . matchdict and <EOL> request . matchdict . get ( id_name , None ) == '<STR_LIT>' and <EOL> user ) : <EOL> request . matchdict [ id_name ] = user . username <EOL> config . add_subscriber ( context_found_subscriber , ContextFound ) </s>
