o 3a+@sddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z ddl mZddlmZmZmZmZmZmZmZddlmZdd lmZed ZGd d d eZGd ddejZdS)N) sync_to_async)settings)signals)RequestAbortedRequestDataTooBig)base) FileResponse HttpRequest HttpResponseHttpResponseBadRequestHttpResponseServerError QueryDict parse_cookie)set_script_prefix)cached_propertyzdjango.requestcsneZdZdZdZddZeddZfddZd d Z d d Z d dZ e e e Z e e ZeddZZS) ASGIRequestzv Custom request subclass that decodes from an ASGI-standard request dict and wraps request body handling. <cCs||_d|_d|_d|_|jdd|_|jr,|d|jr,|dt|jd|_n|d|_|jrFd|j d|j dddf|_ n|d|_ |jd |_ |jd d}t|trc|}|j ||j|jd d d |_|jd r|jd d |jd<|jd|jd<|jd d|jd<|jdr|jdd |jd<t|jdd|jd<n d|jd<d|jd<|jdgD]9\}}|d}|dkrd}n|dkrd}n d| dd}|d}||jvr|j|d|}||j|<q||j||_d|_dS) NF root_pathpathz%s/%s/method query_stringT)REQUEST_METHOD QUERY_STRING SCRIPT_NAME PATH_INFOzwsgi.multithreadzwsgi.multiprocessclientr REMOTE_ADDR REMOTE_HOST REMOTE_PORTserver SERVER_NAME SERVER_PORTunknown0headerslatin1zcontent-lengthCONTENT_LENGTHz content-type CONTENT_TYPEzHTTP_%s-_,)scope_post_parse_error _read_startedresolver_matchget script_name startswithlen path_inforstripreplacerupperr isinstancebytesdecodeMETAstr_set_content_type_params_stream)selfr. body_filernamevaluecorrected_namerF;/usr/lib/python3/dist-packages/django/core/handlers/asgi.py__init__s`              zASGIRequest.__init__cCst|jdS)Nr)r r=rArFrFrGGETbszASGIRequest.GETcs|jdp tS)Nscheme)r.r2super _get_schemerI __class__rFrGrMfszASGIRequest._get_schemecCt|ds ||jS)N_post)hasattr_load_post_and_filesrQrIrFrFrG _get_posti zASGIRequest._get_postcCs ||_dS)N)rQ)rApostrFrFrG _set_postns zASGIRequest._set_postcCrP)N_files)rRrSrXrIrFrFrG _get_filesqrUzASGIRequest._get_filescCst|jddS)N HTTP_COOKIEr)rr=r2rIrFrFrGCOOKIESyszASGIRequest.COOKIES)__name__ __module__ __qualname____doc__body_receive_timeoutrHrrJrMrTrWrYpropertyPOSTFILESr[ __classcell__rFrFrNrGrsC   rcsheZdZdZeZdZfddZddZddZ d d Z fd d Z d dZ e ddZddZZS) ASGIHandlerzHandler for ASGI requests.icst|jdddS)NT)is_async)rLrHload_middlewarerIrNrFrGrHs zASGIHandler.__init__cs|ddkrtd|dz ||IdH}Wn ty#YdSwt||ttjjdd|j |dIdH| ||\}}|durR| ||IdHdS| |IdH}|j |_ t|trg|j|_| ||IdHdS)zV Async entrypoint - parses the request and hands off to get_response. typehttpz5Django can only handle ASGI/HTTP connections, not %s.NTthread_sensitive)senderr.) ValueError read_bodyrrget_script_prefixrrrequest_startedsendrOcreate_request send_responseget_response_async_handler_classr:r chunk_size block_size)rAr.receiverqrBrequesterror_responseresponserFrFrG__call__s.    zASGIHandler.__call__csftjtjdd} |IdH}|ddkrtd|vr$||d|dd s+nq |d |S) z*Reads a HTTP body from an ASGI connection.zw+b)max_sizemodeTNrhzhttp.disconnectbody more_bodyFr)tempfileSpooledTemporaryFilerFILE_UPLOAD_MAX_MEMORY_SIZErwriter2seek)rArxrBmessagerFrFrGrns    zASGIHandler.read_bodycCshz |||dfWSty#tjdtddiddtfYSty3dtdddfYSw) z Create the Request object and returns either (request, None) or (None, response) if there is an error response. Nz Bad Request (UnicodeDecodeError) status_codei)exc_infoextraz413 Payload too largei)status) request_classUnicodeDecodeErrorloggerwarningsysrr rr )rAr.rBrFrFrGrrs  zASGIHandler.create_requestcs@z t|||WStyttjrtndddYSw)z#Last-chance handler for exceptions.zInternal Server Errorz text/plain) content_type)rLhandle_uncaught_exception Exceptionr rDEBUG traceback format_exc)rAryresolverrrNrFrGrs  z%ASGIHandler.handle_uncaught_exceptionc s4g}|D]#\}}t|tr|d}t|tr|d}|t|t|fq|jD]}|d|jddd fq0|d|j |dIdH|j rv|D]}| |D]\}} |d |d d IdHq[qT|d d iIdHn| |j D]\}} |d || d IdHq|t|jd d IdHdS)z)Encode and send a response out over ASGI.asciir(s Set-Cookier)headerzhttp.response.start)rhrr'Nzhttp.response.bodyT)rhrrrhrj)itemsr:r>encodeappendr;cookiesvaluesoutputstripr streaming chunk_bytescontentrclose) rAr{rqresponse_headersrrDcpartchunkr,lastrFrFrGrssD     zASGIHandler.send_responseccshd}|s |dfVdS|t|kr2||||j||jt|kfV||j7}|t|ksdSdS)z Chunks some data up so it can be sent in reasonable size messages. Yields (chunk, last_chunk) tuples. rTN)r5rv)clsdatapositionrFrFrGrs   zASGIHandler.chunk_bytescCstjrtjS|ddp dS)zU Return the script prefix to use from either the scope or a setting. rr)rFORCE_SCRIPT_NAMEr2)rAr.rFrFrGroszASGIHandler.get_script_prefix)r\r]r^r_rrrvrHr|rnrrrrs classmethodrrordrFrFrNrGre~s "  / re) loggingrrr asgiref.syncr django.confr django.corerdjango.core.exceptionsrrdjango.core.handlersr django.httprr r r r r r django.urlsrdjango.utils.functionalr getLoggerrr BaseHandlerrerFrFrFrGs    $   h