o a[8@sddlZddlZddlZddlmZmZddlmZddlm Z m Z ddl m Z ddl mZmZddlmZmZddlmZdd lmZd d lmZed ZGd ddZddZe edS)N) async_to_sync sync_to_async)settings)ImproperlyConfiguredMiddlewareNotUsed)request_finished) connections transaction) get_resolver set_urlconf) log_response) import_string)convert_exception_to_responsezdjango.requestc@steZdZdZdZdZdZdddZ dddZddZ d d Z d d Z d dZ ddZ dddZddZddZdS) BaseHandlerNFc Csg|_g|_g|_|r|jn|j}t|}|}ttjD]}t |}t |dd}t |dd}|s8|s8t d||s?|r?d} n|} z|j | ||tj d|d} || } Wn'ty{} ztj rqt| rktd|| ntd |WYd } ~ qd } ~ ww| }| d urtd |t| d r|jd | || jt| dr|j| || jt| dr|j| d| jt| }| }q| |||}||_d S)z Populate middleware lists from settings.MIDDLEWARE. Must be called after the environment is fixed (see __call__ in subclasses). sync_capableT async_capableFzOMiddleware %s must have at least one of sync_capable/async_capable set to True.z middleware %s)debugnamezMiddlewareNotUsed(%r): %szMiddlewareNotUsed: %rNz$Middleware factory %s returned None. process_viewrprocess_template_responseprocess_exception)_view_middleware_template_response_middleware_exception_middleware_get_response_async _get_responserreversedr MIDDLEWAREr getattr RuntimeErroradapt_method_modeDEBUGrstrloggerrrhasattrinsertrappendrr_middleware_chain) selfis_async get_responsehandlerhandler_is_asyncmiddleware_path middlewaremiddleware_can_syncmiddleware_can_asyncmiddleware_is_asyncadapted_handler mw_instanceexcr6;/usr/lib/python3/dist-packages/django/core/handlers/base.pyload_middlewaresn             zBaseHandler.load_middlewarecCsp|dur t|}|r|s|pd|j}|r(|s&|r td|t|ddS|S|r6|r2td|t|S|S)a\ Adapt a method to be in the correct "mode": - If is_async is False: - Synchronous methods are left alone - Asynchronous methods are wrapped with async_to_sync - If is_async is True: - Synchronous methods are wrapped with sync_to_async() - Asynchronous methods are left alone Nz method %s()zSynchronous %s adapted.Tthread_sensitivezAsynchronous %s adapted.)asyncioiscoroutinefunction __qualname__r$rrr)r)r*methodmethod_is_asyncrrr6r6r7r!cs    zBaseHandler.adapt_method_modecCsFttj||}|j|j|jdkr!td|j |j ||d|S)z8Return an HttpResponse object for the given HttpRequest.%s: %sresponserequest) r r ROOT_URLCONFr(_resource_closersr'close status_coder reason_phrasepathr)rDrCr6r6r7r+~s    zBaseHandler.get_responsecs\ttj||IdH}|j|j|jdkr,tt ddd|j |j ||dIdH|S)z Asynchronous version of get_response. Funneling everything, including WSGI, into a single async get_response() is too slow. Avoid the context switch by using a separate async response path. Nr@Fr9rArB) r rrEr(rFr'rGrHrr rIrJrKr6r6r7get_response_asyncs     zBaseHandler.get_response_asyncc CsRd}||\}}}|jD] }|||||}|rnq |durX||}t|r-t|}z ||g|Ri|}WntyW}z|||}|durMWYd}~nd}~ww|||t |drt |j r|j D]}|||}|j||d|j jjfdqkz| }W|Sty}z|||}|durWYd}~|Sd}~ww|S) Resolve and call the view, then apply view, exception, and template_response middleware. This method is everything that happens inside the request/response middleware. Nrender%s.process_template_responser)resolve_requestrmake_view_atomicr;r<r Exceptionprocess_exception_by_middlewarecheck_responser%callablerNr__self__ __class____name__ r)rDrCcallback callback_argscallback_kwargsmiddleware_methodwrapped_callbacker6r6r7rsV           zBaseHandler._get_responsec sd}||\}}}|jD]}|||||IdH}|rnq|durh||}t|s3t|dd}z||g|Ri|IdH}Wn$tyg}zt|jdd||IdH}|dur]WYd}~nd}~ww|||t |drt |j r|j D]}|||IdH}|j||d|j jjfdq{zt|j r| IdH}n t|j ddIdH}Wn$ty}zt|jdd||IdH}|durɂWYd}~nd}~wwt|rtd|S)rMNTr9rNrOrPzResponse is still a coroutine.)rQrrRr;r<rrSrTrUr%rVrNrrWrXrY iscoroutiner rZr6r6r7rsr             zBaseHandler._get_response_asynccCs>t|dr|j}t|t|}nt}||j}||_|S)zw Retrieve/set the urlconf for the request. Return the view resolved, with its args and kwargs. urlconf)r%rbr r resolve path_inforesolver_match)r)rDrbresolverrer6r6r7rQs   zBaseHandler.resolve_requestcCsx|dus t|s dS|s%t|tjrd|j|jf}n d|j|jjf}|dur/td|t|r:td|dS)zT Raise an error if the view returned None or an uncalled coroutine. NzThe view %s.%szThe view %s.%s.__call__zB%s didn't return an HttpResponse object. It returned None instead.z%s didn't return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an 'await' into your view.) r;ra isinstancetypes FunctionType __module__rYrX ValueError)r)rCr[rr6r6r7rU&s,  zBaseHandler.check_responsecCsXt|dt}tD]}|jdr)|j|vr)t|r tdt j |jd|}q |S)N_non_atomic_requestsATOMIC_REQUESTSz0You cannot use ATOMIC_REQUESTS with async views.)using) rsetrall settings_dictaliasr;r<r r atomic)r)viewnon_atomic_requestsdbr6r6r7rRBs  zBaseHandler.make_view_atomiccCs&|jD] }|||}|r|SqdS)z Pass the exception to the exception middleware. If no middleware return a response for this exception, return None. N)r)r) exceptionrDr^rCr6r6r7rTMs  z+BaseHandler.process_exception_by_middleware)F)NFN)N)rYrjr=rrrr(r8r!r+rLrrrQrUrRrTr6r6r6r7rs  J 4A  rcKs tddS)z1Reset the URLconf after each request is finished.N)r )senderkwargsr6r6r7 reset_urlconfYs rz)r;loggingrh asgiref.syncrr django.confrdjango.core.exceptionsrrdjango.core.signalsr django.dbrr django.urlsr r django.utils.logr django.utils.module_loadingr rwr getLoggerr$rrzconnectr6r6r6r7s$      G