#NEWLINE# def __check_request_limit( self ) : #NEWLINE# #INDENT# endpoint = ( request . endpoint or '' ) #NEWLINE# view_func = current_app . view_functions . get ( endpoint , None ) #NEWLINE# name = ( ( '%s.%s' % ( view_func . __module__ , view_func . __name__ ) ) if view_func else '' ) #NEWLINE# if ( ( not request . endpoint ) or ( not self . enabled ) or ( view_func == current_app . send_static_file ) or ( name in self . _exempt_routes ) or ( request . blueprint in self . _blueprint_exempt ) or any ( ( fn ( ) for fn in self . _request_filters ) ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# limits = ( ( ( name in self . _route_limits ) and self . _route_limits [ name ] ) or [ ] ) #NEWLINE# dynamic_limits = [ ] #NEWLINE# if ( name in self . _dynamic_route_limits ) : #NEWLINE# #INDENT# for lim in self . _dynamic_route_limits [ name ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# dynamic_limits . extend ( ( ExtLimit ( limit , lim . key_func , lim . scope , lim . per_method , lim . methods , lim . error_message , lim . exempt_when ) for limit in parse_many ( lim . limit ) ) ) #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# self . logger . error ( 'failed to load ratelimit for view function %s (%s)' , name , e ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if request . blueprint : #NEWLINE# #INDENT# if ( ( request . blueprint in self . _blueprint_dynamic_limits ) and ( not dynamic_limits ) ) : #NEWLINE# #INDENT# for lim in self . _blueprint_dynamic_limits [ request . blueprint ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# dynamic_limits . extend ( ( ExtLimit ( limit , lim . key_func , lim . scope , lim . per_method , lim . methods , lim . error_message , lim . exempt_when ) for limit in parse_many ( lim . limit ) ) ) #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# self . logger . error ( 'failed to load ratelimit for blueprint %s (%s)' , request . blueprint , e ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( ( request . blueprint in self . _blueprint_limits ) and ( not limits ) ) : #NEWLINE# #INDENT# limits . extend ( self . _blueprint_limits [ request . blueprint ] ) #NEWLINE# #UNINDENT# #UNINDENT# failed_limit = None #NEWLINE# limit_for_header = None #NEWLINE# try : #NEWLINE# #INDENT# all_limits = [ ] #NEWLINE# if ( self . _storage_dead and self . _fallback_limiter ) : #NEWLINE# #INDENT# if ( self . __should_check_backend ( ) and self . _storage . check ( ) ) : #NEWLINE# #INDENT# self . logger . info ( 'Rate limit storage recovered' ) #NEWLINE# self . _storage_dead = False #NEWLINE# self . __check_backend_count = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# all_limits = fn . _in_memory_fallback #NEWLINE# #UNINDENT# #UNINDENT# if ( not all_limits ) : #NEWLINE# #INDENT# all_limits = ( ( limits + dynamic_limits ) or self . _global_limits ) #NEWLINE# #UNINDENT# for lim in all_limits : #NEWLINE# #INDENT# limit_scope = ( lim . scope or endpoint ) #NEWLINE# if lim . is_exempt : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# if ( ( lim . methods is not None ) and ( request . method . lower ( ) not in lim . methods ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# if lim . per_method : #NEWLINE# #INDENT# limit_scope += ( ':%s' % request . method ) #NEWLINE# #UNINDENT# if ( ( not limit_for_header ) or ( lim . limit < limit_for_header [ 0 ] ) ) : #NEWLINE# #INDENT# limit_for_header = ( lim . limit , lim . key_func ( ) , limit_scope ) #NEWLINE# #UNINDENT# if ( not self . limiter . hit ( lim . limit , lim . key_func ( ) , limit_scope ) ) : #NEWLINE# #INDENT# self . logger . warning ( 'ratelimit %s (%s) exceeded at endpoint: %s' , lim . limit , lim . key_func ( ) , limit_scope ) #NEWLINE# failed_limit = lim #NEWLINE# limit_for_header = ( lim . limit , lim . key_func ( ) , limit_scope ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# g . view_rate_limit = limit_for_header #NEWLINE# if failed_limit : #NEWLINE# #INDENT# if failed_limit . error_message : #NEWLINE# #INDENT# exc_description = ( failed_limit . error_message if ( not callable ( failed_limit . error_message ) ) else failed_limit . error_message ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# exc_description = six . text_type ( failed_limit . limit ) #NEWLINE# #UNINDENT# raise RateLimitExceeded ( exc_description ) #NEWLINE# #UNINDENT# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# if isinstance ( e , RateLimitExceeded ) : #NEWLINE# #INDENT# six . reraise ( * sys . exc_info ( ) ) #NEWLINE# #UNINDENT# if ( self . _in_memory_fallback and ( not self . _storage_dead ) ) : #NEWLINE# #INDENT# self . logger . warn ( 'Rate limit storage unreachable - falling back to in-memory storage' ) #NEWLINE# self . _storage_dead = True #NEWLINE# self . __check_request_limit ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if self . _swallow_errors : #NEWLINE# #INDENT# self . logger . exception ( 'Failed to rate limit. Swallowing error' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# six . reraise ( * sys . exc_info ( ) )