Django 源碼小剖: 初探中間件(middleware)

由於考慮到文章的長度, 因此 BaseHandler 的展開被推遲了. 在 BaseHandler 中隱藏着中間件的信息, 較常見的 SessionMiddleware 就已經默認安裝.  BaseHandler 的展開主要是以代碼爲主, 但已經加入了註釋; 文章的最後附一張美圖 .python

最後, 祝程序員們節日快樂, 別太宅了 ;)git

BaseHandler 詳解

BaseHandler 在 django.core.handlers.base.py 中定義, 有兩個核心的成員方法不得不提, 裏面就涉及了中間件的信息, 照抄以下(有點長, 但已經加入註釋):程序員

# 好經典的 handler
class BaseHandler(object):
    # Changes that are always applied to a response (in this order).
    response_fixes = [
        http.fix_location_header,
        http.conditional_content_removal,
        http.fix_IE_for_attach,
        http.fix_IE_for_vary,
    ]

    初始化函數, 初始化請求中間件, 視圖中間件, 模版中間件, 響應中間件和異常中間件.
    def __init__(self):
        self._request_middleware = self._view_middleware =
            self._template_response_middleware =
            self._response_middleware =
            self._exception_middleware = None  視圖, 模版相應, 相應, 異常中間件, 請求中間件

    根據 mysite.settings.py 中的 `MIDDLEWARE_CLASSES` 添加全部的中間件.
    def load_middleware(self):
        """
        Populate middleware lists from settings.MIDDLEWARE_CLASSES.

        從 settings 中加載各類中間件

        Must be called after the environment is fixed (see __call__ in subclasses).
        """
        # 初始化四種中間件
        self._view_middleware = []
        self._template_response_middleware = []
        self._response_middleware = []
        self._exception_middleware = []

        # 臨時的請求中間件, 由於在加入中間件的過程當中, 可能會出現異常, 而出現異常都致使加載中間件的不成功, 所以將 self._request_middleware 的賦值放在最後, 表示已經成功.
        request_middleware = []

        # settings.MIDDLEWARE_CLASSES 設置項指定須要預裝的中間件
        for middleware_path in settings.MIDDLEWARE_CLASSES:
            try:
                mw_module, mw_classname = middleware_path.rsplit('.', 1)
            except ValueError:
                raise exceptions.ImproperlyConfigured('%s isn\'t a middleware module' % middleware_path)

            try:
                嘗試導入中間件所在模塊.
                mod = import_module(mw_module)
            except ImportError as e:
                raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))

            try:
                嘗試獲得某種中間件類
                mw_class = getattr(mod, mw_classname)
            except AttributeError:
                raise exceptions.ImproperlyConfigured('Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname))

            try:
                嘗試實例化
                mw_instance = mw_class()
            except exceptions.MiddlewareNotUsed:
                continue

            和 urllib 的處理方法相似: 請求預處理, 視圖處理?, 模版處理, 相應處理, 錯誤處理(詳見個人 urllib 源碼剖析)
            if hasattr(mw_instance, 'process_request'):
                # 這裏 request_middleware 用的是 append(), 這裏是有講究的:
                # django 規定, 多個請求中間件調用的次序是其出現的次序, 下同
                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'):
                # 這裏 _template_response_middleware 用的是 insert() 頭插法, 這裏是有講究的:
                # django 規定, 多個模版相應中間件調用的次序是其出現次序的逆序, 下同
                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._request_middleware = request_middleware

    # 處理請求的函數, 並返回 response
    def get_response(self, request):
        "Returns an HttpResponse object for the given HttpRequest"
        根據請求, 獲得響應

        try:
            爲該線程提供默認的 url 處理器
            # Setup default url resolver for this thread, this code is outside
            # the try/except so we don't get a spurious "unbound local
            # variable" exception in the event an exception is raised before
            # resolver is set

            #ROOT_URLCONF = 'mysite.urls'
            urlconf = settings.ROOT_URLCONF

            # set_urlconf() 會設置 url 配置即 settings.ROOT_URLCONF
            urlresolvers.set_urlconf(urlconf)

            # 實例化 RegexURLResolver, 暫且將其理解爲一個 url 的匹配處理器, 下節展開
            resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)

            try:
                response = None

                # Apply request middleware 調用請求中間件
                for middleware_method in self._request_middleware:
                    response = middleware_method(request)

                    # 若是此 response 有效, 即不走下面的邏輯
                    if response:
                        break

                # 若是沒有結果
                if response is None:
                    # 嘗試 request 中是否有 urlconf, 通常沒有, 能夠忽略此段代碼!!!
                    if hasattr(request, 'urlconf'):
                        # Reset url resolver with a custom urlconf. 自定義的 urlconf
                        urlconf = request.urlconf
                        urlresolvers.set_urlconf(urlconf)
                        resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
                    # 調用 RegexURLResolver.resolve(), 能夠理解爲啓動匹配的函數; 返回 ResolverMatch 實例
                    resolver_match = resolver.resolve(request.path_info)

                    # resolver_match 對象中存儲了有用的信息, 譬如 callback 就是咱們在 views.py 中定義的函數.
                    callback, callback_args, callback_kwargs = resolver_match

                    # 將返回的 resolver_match 掛鉤到 request
                    request.resolver_match = resolver_match

                    # Apply view middleware 調用視圖中間件
                    for middleware_method in self._view_middleware:
                        response = middleware_method(request, callback, callback_args, callback_kwargs)

                        # 若是此 response 有效, 即不走下面的邏輯
                        if response:
                            break

                # response 仍是爲空
                if response is None:
                    try:
                        # 這裏調用的是真正的處理函數, 咱們通常在 view.py 中定義這些函數
                        response = callback(request, *callback_args, **callback_kwargs)

                    except Exception as e:
                        # If the view raised an exception, run it through exception
                        # middleware, and if the exception middleware returns a
                        # response, use that. Otherwise, reraise the exception.

                        # 出現異常, 調用異常中間件
                        for middleware_method in self._exception_middleware:
                            response = middleware_method(request, e)

                            # 若是此 response 有效, 即不走下面的邏輯
                            if response:
                                break

                        if response is None:
                            raise

                # response 仍是爲空, 可能就要異常了
                # Complain if the view returned None (a common error).
                if response is None:
                    if isinstance(callback, types.FunctionType):    # FBV
                        view_name = callback.__name__
                    else:                                           # CBV
                        view_name = callback.__class__.__name__ + '.__call__'
                    raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))

                # If the response supports deferred rendering, apply template
                # response middleware and the render the response 若是 response 實現了 render, 那麼渲染返回.
                if hasattr(response, 'render') and callable(response.render):
                    for middleware_method in self._template_response_middleware:
                        response = middleware_method(request, response)
                    response = response.render()

            except http.Http404 as e:
                logger.warning('Not Found: %s', request.path,
                            extra={
                                'status_code': 404,
                                'request': request
                            })

                # 若是是調試下, 直接要返回 404 頁面
                if settings.DEBUG:
                    response = debug.technical_404_response(request, e)
                else:
                    try:
                        # 非調試模式下, 獲取 url 處理器的默認 404 處理
                        callback, param_dict = resolver.resolve404()
                        response = callback(request, **param_dict)
                    except:
                        signals.got_request_exception.send(sender=self.__class__, request=request)
                        response = self.handle_uncaught_exception(request, resolver, sys.exc_info())

            # 訪問拒絕
            except exceptions.PermissionDenied:
                logger.warning(
                    'Forbidden (Permission denied): %s', request.path,
                    extra={
                        'status_code': 403,
                        'request': request
                    })
                try:
                    callback, param_dict = resolver.resolve403()
                    response = callback(request, **param_dict)
                except:
                    signals.got_request_exception.send(
                            sender=self.__class__, request=request)
                    response = self.handle_uncaught_exception(request,
                            resolver, sys.exc_info())

            except SystemExit:
                # Allow sys.exit() to actually exit. See tickets #1023 and #4701
                raise

            except: # Handle everything else, including SuspiciousOperation, etc.
                # Get the exception info now, in case another exception is thrown later.
                signals.got_request_exception.send(sender=self.__class__, request=request)
                response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
        finally:
            # Reset URLconf for this thread on the way out for complete
            # isolation of request.urlconf 重置, 由於前面有兩種 url resolver 的可能, 拒絕混淆
            urlresolvers.set_urlconf(None)

        try:
            # Apply response middleware, regardless of the response 調用響應中間件
            for middleware_method in self._response_middleware:
                response = middleware_method(request, response)

            response = self.apply_response_fixes(request, response)

        except: # Any exception should be gathered and handled
            signals.got_request_exception.send(sender=self.__class__, request=request)
            response = self.handle_uncaught_exception(request, resolver, sys.exc_info())

        return response

    def handle_uncaught_exception(self, request, resolver, exc_info):
        """
        處理未能捕捉的錯誤

        Processing for any otherwise uncaught exceptions (those that will
        generate HTTP 500 responses). Can be overridden by subclasses who want
        customised 500 handling. 子類中能夠重寫 500 狀態的處理

        Be *very* careful when overriding this because the error could be
        caused by anything, so assuming something like the database is always
        available would be an error.
        """
        if settings.DEBUG_PROPAGATE_EXCEPTIONS:
            raise

        logger.error('Internal Server Error: %s', request.path,
            exc_info=exc_info,
            extra={
                'status_code': 500,
                'request': request
            }
        )

        調試模式特殊處理
        if settings.DEBUG:
            return debug.technical_500_response(request, *exc_info)

        # If Http500 handler is not installed, re-raise last exception 若是http500 處理器都沒有安裝, 可能會崩潰
        if resolver.urlconf_module is None:
            six.reraise(*exc_info)

        # Return an HttpResponse that displays a friendly error message.
        #這是自定義的 500 處理器
        callback, param_dict = resolver.resolve500()
        return callback(request, **param_dict)

    def apply_response_fixes(self, request, response):
        """
        Applies each of the functions in self.response_fixes to the request and
        response, modifying the response in the process. Returns the new
        response.
        """
        for func in self.response_fixes:
            response = func(request, response)
        return response

故此總結

load_middleware() 函數會根據 mysite.settings.py 中的 MIDDLEWARE_CLASSES 導入全部的中間件. 在 eclipse + pydev 建立 django 的默認設置當中就有默認的中間件:github

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

每個中間件都是一個類, 其內部會實現 process_request(),process_view(),process_template_response(),process_response() 或者 process_exception() 方法. 不必定都實現, 看需求. 而這些方法若是存在, 都會被保存響應的函數列表中, 待未來調用.django

get_response() 方法, 中間件調用執行的順序是請求中間件, 視圖中間件, 模版中間件, 異常中間件(可選), 響應中間件. 習慣上, 我把這些簡稱爲請求預處理和響應善後處理.get_response() 返回了 response, 但一長串的 url 是如何匹配的, 而且本身在 views.py 中的函數是在何時調用的?session

我已經在 github 備份了 Django 源碼的註釋: Decode-Django, 有興趣的童鞋 fork 吧.app

圖解中間件:less

django 處理流程圖

 

搗亂 2013-9-14eclipse

http://daoluan.netide

相關文章
相關標籤/搜索