def _load_new_middleware(self): from django.core.handlers.exception import convert_exception_to_response handler = convert_exception_to_response(self._get_response) middleware_classes = reversed(self.middleware or []) for mw_class in middleware_classes: mw_class, mw_types = self._unpack_middleware(mw_class) mw_instance = mw_class(handler) if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_VIEW): self._view_middleware.insert(0, mw_instance.process_view) if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_TEMPLATE_RESPONSE): self._template_response_middleware.append(mw_instance.process_template_response) if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_EXCEPTION): self._exception_middleware.append(mw_instance.process_exception) handler = convert_exception_to_response(mw_instance) self._middleware_chain = handler
def load_middleware(self) -> None: """ Populate middleware lists from settings.MIDDLEWARE. This is copied from Django. This uses settings.MIDDLEWARE setting with the old business logic. The middleware architecture is not compatible with our asynchronous handlers. The problem occurs when we return None from our handler. The Django middlewares throw exception because they can't handler None, so we can either upgrade the Django middlewares or just override this method to use the new setting with the old logic. The added advantage is that due to this our event system code doesn't change. """ self._request_middleware = [ ] # type: Optional[List[Callable[[HttpRequest], HttpResponse]]] self._view_middleware = [ ] # type: List[Callable[[HttpRequest, Callable[..., HttpResponse], List[str], Dict[str, Any]], Optional[HttpResponse]]] self._template_response_middleware = [ ] # type: List[Callable[[HttpRequest, HttpResponse], HttpResponse]] self._response_middleware = [ ] # type: List[Callable[[HttpRequest, HttpResponse], HttpResponse]] self._exception_middleware = [ ] # type: List[Callable[[HttpRequest, Exception], Optional[HttpResponse]]] handler = convert_exception_to_response(self._legacy_get_response) for middleware_path in settings.MIDDLEWARE: mw_class = import_string(middleware_path) try: mw_instance = mw_class() except MiddlewareNotUsed as exc: if settings.DEBUG: if str(exc): base.logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc) else: base.logger.debug('MiddlewareNotUsed: %r', middleware_path) continue if hasattr(mw_instance, 'process_request'): self._request_middleware.append(mw_instance.process_request) if hasattr(mw_instance, 'process_view'): self._view_middleware.append(mw_instance.process_view) if hasattr(mw_instance, 'process_template_response'): self._template_response_middleware.insert( 0, mw_instance.process_template_response) if hasattr(mw_instance, 'process_response'): self._response_middleware.insert(0, mw_instance.process_response) if hasattr(mw_instance, 'process_exception'): self._exception_middleware.insert( 0, mw_instance.process_exception) # We only assign to this when initialization is complete as it is used # as a flag for initialization being complete. self._middleware_chain = handler
def _load_new_middleware(self): from django.core.handlers.exception import convert_exception_to_response handler = convert_exception_to_response(self._get_response) middleware_classes = reversed(self.middleware or []) for mw_class in middleware_classes: mw_class, mw_types = self._unpack_middleware(mw_class) mw_instance = mw_class(handler) if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_VIEW): self._view_middleware.insert(0, mw_instance.process_view) if self._should_add_middleware( mw_instance, mw_types, MiddlewareType.PROCESS_TEMPLATE_RESPONSE): self._template_response_middleware.append( mw_instance.process_template_response) if self._should_add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_EXCEPTION): self._exception_middleware.append( mw_instance.process_exception) handler = convert_exception_to_response(mw_instance) self._middleware_chain = handler
def load_middleware(self): # type: () -> None """ Populate middleware lists from settings.MIDDLEWARE. This is copied from Django. This uses settings.MIDDLEWARE setting with the old business logic. The middleware architecture is not compatible with our asynchronous handlers. The problem occurs when we return None from our handler. The Django middlewares throw exception because they can't handler None, so we can either upgrade the Django middlewares or just override this method to use the new setting with the old logic. The added advantage is that due to this our event system code doesn't change. """ self._request_middleware = [] # type: Optional[List[Callable]] self._view_middleware = [] # type: List[Callable] self._template_response_middleware = [] # type: List[Callable] self._response_middleware = [] # type: List[Callable] self._exception_middleware = [] # type: List[Callable] handler = convert_exception_to_response(self._legacy_get_response) for middleware_path in settings.MIDDLEWARE: mw_class = import_string(middleware_path) try: mw_instance = mw_class() except MiddlewareNotUsed as exc: if settings.DEBUG: if six.text_type(exc): base.logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc) else: base.logger.debug('MiddlewareNotUsed: %r', middleware_path) continue if hasattr(mw_instance, 'process_request'): self._request_middleware.append(mw_instance.process_request) if hasattr(mw_instance, 'process_view'): self._view_middleware.append(mw_instance.process_view) if hasattr(mw_instance, 'process_template_response'): self._template_response_middleware.insert(0, mw_instance.process_template_response) if hasattr(mw_instance, 'process_response'): self._response_middleware.insert(0, mw_instance.process_response) if hasattr(mw_instance, 'process_exception'): self._exception_middleware.insert(0, mw_instance.process_exception) # We only assign to this when initialization is complete as it is used # as a flag for initialization being complete. self._middleware_chain = handler
def config(self, app): if not is_django_present(): raise RuntimeError("django is not available in system runtime") from django.core.handlers.base import BaseHandler from django.core.handlers.exception import convert_exception_to_response if not isinstance(app, BaseHandler): raise RuntimeError("app is not a valid django.core.handlers.BaseHandler instance") self.request_logger = logging.getLogger('json_logging.django') self.request_logger.setLevel(logging.DEBUG) self.request_logger.addHandler(logging.StreamHandler(sys.stdout)) handler = app._middleware_chain mw_instance = Middleware(handler) app._middleware_chain = convert_exception_to_response(mw_instance)
def load_middleware(self, key): view_mw, template_response_mw, exception_mw = [], [], [] MIDDLEWARE = settings.PER_REQUEST_MIDDLEWARE[key] handler = copy.deepcopy(self.get_response) for mw_path in reversed(MIDDLEWARE): try: mw_instance = import_string(mw_path)(handler) except MiddlewareNotUsed: continue if mw_instance is None: raise ImproperlyConfigured(f"Middleware factory {mw_path} returned None.") if hasattr(mw_instance, "process_view"): view_mw.insert(0, mw_instance.process_view) if hasattr(mw_instance, "process_template_response"): template_response_mw.append(mw_instance.process_template_response) if hasattr(mw_instance, "process_exception"): exception_mw.append(mw_instance.process_exception) handler = convert_exception_to_response(mw_instance) self.mws[(key, None)] = handler self.mws[(key, "view")] = view_mw self.mws[(key, "template_response")] = template_response_mw self.mws[(key, "exception")] = exception_mw
def _load_middleware(self): self._request_middleware = [] self._view_middleware = [] self._template_response_middleware = [] self._response_middleware = [] self._exception_middleware = [] if self.middleware is None: middleware_classes = self.middleware_classes or [] for mw_class in middleware_classes: if isinstance(mw_class, tuple): mw_class, mw_types = mw_class[0], mw_class[1:] else: mw_types = None mw_instance = mw_class() if self._add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_REQUEST): self._request_middleware.append( mw_instance.process_request) if self._add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_VIEW): self._view_middleware.append(mw_instance.process_view) if self._add_middleware( mw_instance, mw_types, MiddlewareType.PROCESS_TEMPLATE_RESPONSE): self._template_response_middleware.insert( 0, mw_instance.process_template_response) if self._add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_RESPONSE): self._response_middleware.insert( 0, mw_instance.process_response) if self._add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_EXCEPTION): self._exception_middleware.insert( 0, mw_instance.process_exception) else: if django.VERSION < (1, 10): raise NotImplementedError( 'New style middleware is not intended to be used with older django versions' ) from django.core.handlers.exception import convert_exception_to_response handler = convert_exception_to_response(self._get_response) middleware_classes = reversed(self.middleware or []) for mw_class in middleware_classes: if isinstance(mw_class, tuple): mw_class, mw_types = mw_class[0], mw_class[1:] else: mw_types = None mw_instance = mw_class(handler) if self._add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_VIEW): self._view_middleware.insert(0, mw_instance.process_view) if self._add_middleware( mw_instance, mw_types, MiddlewareType.PROCESS_TEMPLATE_RESPONSE): self._template_response_middleware.append( mw_instance.process_template_response) if self._add_middleware(mw_instance, mw_types, MiddlewareType.PROCESS_EXCEPTION): self._exception_middleware.append( mw_instance.process_exception) handler = convert_exception_to_response(mw_instance) self._middleware_chain = handler
async def async_load_middleware_dj_1_10(self): """ Populate middleware lists from settings.MIDDLEWARE (or the deprecated MIDDLEWARE_CLASSES). Must be called after the environment is fixed (see __call__ in subclasses). """ self._request_middleware = [] self._view_middleware = [] self._template_response_middleware = [] self._response_middleware = [] self._exception_middleware = [] if settings.MIDDLEWARE is None: warnings.warn( "Old-style middleware using settings.MIDDLEWARE_CLASSES is " "deprecated. Update your middleware and use settings.MIDDLEWARE " "instead.", RemovedInDjango20Warning) handler = convert_exception_to_response(self._legacy_get_response) for middleware_path in settings.MIDDLEWARE_CLASSES: mw_class = import_string(middleware_path) try: mw_instance = mw_class() except MiddlewareNotUsed as exc: if settings.DEBUG: if isinstance(exc, str): logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc) else: logger.debug('MiddlewareNotUsed: %r', middleware_path) continue if hasattr(mw_instance, 'process_request'): self._request_middleware.append( mw_instance.process_request) if hasattr(mw_instance, 'process_view'): self._view_middleware.append(mw_instance.process_view) if hasattr(mw_instance, 'process_template_response'): self._template_response_middleware.insert( 0, mw_instance.process_template_response) if hasattr(mw_instance, 'process_response'): self._response_middleware.insert( 0, mw_instance.process_response) if hasattr(mw_instance, 'process_exception'): self._exception_middleware.insert( 0, mw_instance.process_exception) else: handler = convert_exception_to_response(self._get_response) for middleware_path in reversed(settings.MIDDLEWARE): middleware = import_string(middleware_path) try: mw_instance = middleware(handler) except MiddlewareNotUsed as exc: if settings.DEBUG: if isinstance(exc, str): logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc) else: logger.debug('MiddlewareNotUsed: %r', middleware_path) continue if mw_instance is None: raise ImproperlyConfigured( 'Middleware factory %s returned None.' % middleware_path) if hasattr(mw_instance, 'process_view'): self._view_middleware.insert(0, mw_instance.process_view) if hasattr(mw_instance, 'process_template_response'): self._template_response_middleware.append( mw_instance.process_template_response) if hasattr(mw_instance, 'process_exception'): self._exception_middleware.append( mw_instance.process_exception) handler = convert_exception_to_response(mw_instance) # We only assign to this when initialization is complete as it is used # as a flag for initialization being complete. self._middleware_chain = handler