<s> from __future__ import unicode_literals <EOL> import logging <EOL> import sys <EOL> import types <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . core import exceptions <EOL> from django . core import urlresolvers <EOL> from django . core import signals <EOL> from django . utils . encoding import force_text <EOL> from django . utils . importlib import import_module <EOL> from django . utils import six <EOL> from django . views import debug <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class BaseHandler ( object ) : <EOL> response_fixes = [ <EOL> http . fix_location_header , <EOL> http . conditional_content_removal , <EOL> http . fix_IE_for_attach , <EOL> http . fix_IE_for_vary , <EOL> ] <EOL> def __init__ ( self ) : <EOL> self . _request_middleware = self . _view_middleware = self . _template_response_middleware = self . _response_middleware = self . _exception_middleware = None <EOL> def load_middleware ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _view_middleware = [ ] <EOL> self . _template_response_middleware = [ ] <EOL> self . _response_middleware = [ ] <EOL> self . _exception_middleware = [ ] <EOL> request_middleware = [ ] <EOL> for middleware_path in settings . MIDDLEWARE_CLASSES : <EOL> try : <EOL> mw_module , mw_classname = middleware_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise exceptions . ImproperlyConfigured ( '<STR_LIT>' % middleware_path ) <EOL> try : <EOL> mod = import_module ( mw_module ) <EOL> except ImportError as e : <EOL> raise exceptions . ImproperlyConfigured ( '<STR_LIT>' % ( mw_module , e ) ) <EOL> try : <EOL> mw_class = getattr ( mod , mw_classname ) <EOL> except AttributeError : <EOL> raise exceptions . ImproperlyConfigured ( '<STR_LIT>' % ( mw_module , mw_classname ) ) <EOL> try : <EOL> mw_instance = mw_class ( ) <EOL> except exceptions . MiddlewareNotUsed : <EOL> continue <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> request_middleware . append ( mw_instance . process_request ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _view_middleware . append ( mw_instance . process_view ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _template_response_middleware . insert ( <NUM_LIT:0> , mw_instance . process_template_response ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _response_middleware . insert ( <NUM_LIT:0> , mw_instance . process_response ) <EOL> if hasattr ( mw_instance , '<STR_LIT>' ) : <EOL> self . _exception_middleware . insert ( <NUM_LIT:0> , mw_instance . process_exception ) <EOL> self . _request_middleware = request_middleware <EOL> def get_response ( self , request ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> urlconf = settings . ROOT_URLCONF <EOL> urlresolvers . set_urlconf ( urlconf ) <EOL> resolver = urlresolvers . RegexURLResolver ( r'<STR_LIT>' , urlconf ) <EOL> try : <EOL> response = None <EOL> for middleware_method in self . _request_middleware : <EOL> response = middleware_method ( request ) <EOL> if response : <EOL> break <EOL> if response is None : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> urlconf = request . urlconf <EOL> urlresolvers . set_urlconf ( urlconf ) <EOL> resolver = urlresolvers . RegexURLResolver ( r'<STR_LIT>' , urlconf ) <EOL> resolver_match = resolver . resolve ( request . path_info ) <EOL> callback , callback_args , callback_kwargs = resolver_match <EOL> request . resolver_match = resolver_match <EOL> for middleware_method in self . _view_middleware : <EOL> response = middleware_method ( request , callback , callback_args , callback_kwargs ) <EOL> if response : <EOL> break <EOL> if response is None : <EOL> try : <EOL> response = callback ( request , * callback_args , ** callback_kwargs ) <EOL> except Exception as e : <EOL> for middleware_method in self . _exception_middleware : <EOL> response = middleware_method ( request , e ) <EOL> if response : <EOL> break <EOL> if response is None : <EOL> raise <EOL> if response is None : <EOL> if isinstance ( callback , types . FunctionType ) : <EOL> view_name = callback . __name__ <EOL> else : <EOL> view_name = callback . __class__ . __name__ + '<STR_LIT>' <EOL> raise ValueError ( "<STR_LIT>" % ( callback . __module__ , view_name ) ) <EOL> if hasattr ( response , '<STR_LIT>' ) and callable ( response . render ) : <EOL> for middleware_method in self . _template_response_middleware : <EOL> response = middleware_method ( request , response ) <EOL> response = response . render ( ) <EOL> except http . Http404 as e : <EOL> logger . warning ( '<STR_LIT>' , request . path , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request <EOL> } ) <EOL> if settings . DEBUG : <EOL> response = debug . technical_404_response ( request , e ) <EOL> else : <EOL> try : <EOL> callback , param_dict = resolver . resolve404 ( ) <EOL> response = callback ( request , ** param_dict ) <EOL> except : <EOL> signals . got_request_exception . send ( sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , resolver , sys . exc_info ( ) ) <EOL> except exceptions . PermissionDenied : <EOL> logger . warning ( <EOL> '<STR_LIT>' , request . path , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request <EOL> } ) <EOL> try : <EOL> callback , param_dict = resolver . resolve403 ( ) <EOL> response = callback ( request , ** param_dict ) <EOL> except : <EOL> signals . got_request_exception . send ( <EOL> sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , <EOL> resolver , sys . exc_info ( ) ) <EOL> except SystemExit : <EOL> raise <EOL> except : <EOL> signals . got_request_exception . send ( sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , resolver , sys . exc_info ( ) ) <EOL> finally : <EOL> urlresolvers . set_urlconf ( None ) <EOL> try : <EOL> for middleware_method in self . _response_middleware : <EOL> response = middleware_method ( request , response ) <EOL> response = self . apply_response_fixes ( request , response ) <EOL> except : <EOL> signals . got_request_exception . send ( sender = self . __class__ , request = request ) <EOL> response = self . handle_uncaught_exception ( request , resolver , sys . exc_info ( ) ) <EOL> return response <EOL> def handle_uncaught_exception ( self , request , resolver , exc_info ) : <EOL> """<STR_LIT>""" <EOL> if settings . DEBUG_PROPAGATE_EXCEPTIONS : <EOL> raise <EOL> logger . error ( '<STR_LIT>' , request . path , <EOL> exc_info = exc_info , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request <EOL> } <EOL> ) <EOL> if settings . DEBUG : <EOL> return debug . technical_500_response ( request , * exc_info ) <EOL> if resolver . urlconf_module is None : <EOL> six . reraise ( * exc_info ) <EOL> callback , param_dict = resolver . resolve500 ( ) <EOL> return callback ( request , ** param_dict ) <EOL> def apply_response_fixes ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> for func in self . response_fixes : <EOL> response = func ( request , response ) <EOL> return response <EOL> def get_path_info ( environ ) : <EOL> """<STR_LIT>""" <EOL> path_info = environ . get ( '<STR_LIT>' , str ( '<STR_LIT:/>' ) ) <EOL> if six . PY3 : <EOL> path_info = path_info . encode ( '<STR_LIT>' ) <EOL> return path_info . decode ( '<STR_LIT:utf-8>' ) <EOL> def get_script_name ( environ ) : <EOL> """<STR_LIT>""" <EOL> if settings . FORCE_SCRIPT_NAME is not None : <EOL> return force_text ( settings . FORCE_SCRIPT_NAME ) <EOL> script_url = environ . get ( '<STR_LIT>' , environ . get ( '<STR_LIT>' , str ( '<STR_LIT>' ) ) ) <EOL> if script_url : <EOL> script_name = script_url [ : - len ( environ . get ( '<STR_LIT>' , str ( '<STR_LIT>' ) ) ) ] <EOL> else : <EOL> script_name = environ . get ( '<STR_LIT>' , str ( '<STR_LIT>' ) ) <EOL> if six . PY3 : <EOL> script_name = script_name . encode ( '<STR_LIT>' ) <EOL> return script_name . decode ( '<STR_LIT:utf-8>' ) </s>
