
    FȂi                     \    S SK r S SKJr  S SKJr  S SKJrJr   " S S5      r " S S5      r	g)	    N)defaultdict)SpooledTemporaryFile)AsyncToSyncsync_to_asyncc                   (    \ rS rSrSrSS jrS rSrg)
WsgiToAsgi   z?
Wraps a WSGI application to make it into an ASGI application.
c                     Xl         X l        g N)wsgi_applicationduplicate_header_limitselfr   r   s      =D:\sksbv-state-backend\venv\Lib\site-packages\asgiref/wsgi.py__init__WsgiToAsgi.__init__   s     0&<#    c                 n   #    [        U R                  U R                  5      " XU5      I Sh  vN   g N7f)z
ASGI application instantiation point.
We return a new WsgiToAsgiInstance here with the WSGI app
and the scope, ready to respond when it is __call__ed.
N)WsgiToAsgiInstancer   r   )r   scopereceivesends       r   __call__WsgiToAsgi.__call__   s1      !!6!68S8STD
 	
 	
s   +535)r   r   Nd   )__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__ r   r   r   r      s    =
r   r   c                   H    \ rS rSrSrS
S jrS rS rSS jr\	S 5       r
S	rg)r      z3
Per-socket instance of a wrapped WSGI application
c                 8    Xl         X l        SU l        S U l        g )NF)r   r   response_startedresponse_content_lengthr   s      r   r   WsgiToAsgiInstance.__init__!   s     0&<# %'+$r   c                   #    US   S:w  a  [        S5      eXl        [        SS9 n U" 5       I S h  vN nUS   S:w  a  [        S5      eUR                  UR	                  SS	5      5        UR	                  S
5      (       d  OM]  UR                  S5        [        U5      U l        U R                  U5      I S h  vN   S S S 5        g  N N! , (       d  f       g = f7f)Ntypehttpz&WSGI wrapper received a non-HTTP scopei   )max_sizezhttp.requestz0WSGI wrapper received a non-HTTP-request messagebodyr   	more_bodyr   )	
ValueErrorr   r   writegetseekr   	sync_sendrun_wsgi_app)r   r   r   r   r.   messages         r   r   WsgiToAsgiInstance.__call__'   s     =F"EFF
!51T '	/6?n4$%WXX

7;;vs34{{;//  IIaL(.DN##D))) 21 * * 21s?   $CCCBC:C
;C?	CC
C
CCc                 @   UR                  SS5      R                  S5      R                  S5      nUS   R                  S5      R                  S5      nUR                  U5      (       a  U[	        U5      S nUS   UUUS   R                  S	5      S
US   -  SUR                  SS5      U[
        R                  SSSS.nSU;   a   US   S   US'   [        US   S   5      US'   O
SUS'   SUS'   UR                  S5      b  US   S   US'   [        [        5      nU R                  R                  S/ 5       H  u  pxUR                  S5      nUS:X  a  Sn	O,US:X  a  Sn	O#S UR                  5       R                  S!S"5      -  n	UR                  S5      nU R                  (       a6  [	        Xi   5      U R                  :  a  [        S#U	 S$U R                   35      eXi   R                  U5        M     UR!                  5        H  u  pzS%R#                  U
5      XW'   M     U$ )&z=
Builds a scope and request body into a WSGI environ object.
	root_path utf8latin1pathNmethodquery_stringasciizHTTP/%shttp_version)   r   schemer,   TF)REQUEST_METHODSCRIPT_NAME	PATH_INFOQUERY_STRINGSERVER_PROTOCOLzwsgi.versionzwsgi.url_schemez
wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocesszwsgi.run_onceserverr   SERVER_NAMErB   SERVER_PORT	localhost80clientREMOTE_ADDRheaderscontent-lengthCONTENT_LENGTHzcontent-typeCONTENT_TYPEzHTTP_%s-_zToo many duplicate headers: z exceeds limit of,)r2   encodedecode
startswithlensysstderrstrr   listr   upperreplacer   r0   appenditemsjoin)r   r   r.   script_name	path_infoenviron_headersnamevaluecorrected_namevaluess              r   build_environ WsgiToAsgiInstance.build_environ:   s/    iiR077?FFxP&M((077A	,,!#k"2"45I#Ho&"!.188A(5+@@"$yy6::: $!%"
 u%*8_Q%7GM"%(x);%<GM"%0GM"%)GM"99X*%*8_Q%7GM" t$::>>)R8KD;;x(D''!1'!/!*TZZ\-A-A#s-K!KLL*E++01T5P5PP 2>2BBS2235  $++E2% 9& %NN,LDHHV,GM -r   Nc                    U R                   (       a  US   R                  US   5      e[        U S5      (       a  Uc  [        S5      eUR	                  SS5      u  pE[        U5      nU VVs/ sH5  u  pgUR                  5       R                  S5      UR                  S5      4PM7     nnnSU l        U H+  u  pgUR                  5       S:X  d  M  [        U5      U l        M-     S	UUS
.U l	        gs  snnf )z
WSGI start_response callable.
rB      response_startNz=You cannot call start_response a second time without exc_info r@   rQ   http.response.startr+   statusrP   )
r'   with_tracebackhasattrr0   splitintlowerrW   r(   rp   )	r   rt   response_headersexc_infostatus_coderU   rh   ri   rP   s	            r   start_response!WsgiToAsgiInstance.start_responset   s    
   1+,,Xa[994)**x/?O   c1-+&  0
/ ZZ\  )5<<+@A/ 	 

 (,$+KDzz|///25z, ,
 *!

s   ,;C.c                     U R                  U R                  U5      nS	nU R	                  X R
                  5       H  nU R                  (       d"  S
U l        U R                  U R                  5        U R                  b#  U R                  U-
  n[        U5      U:  a  USU nU R                  SUS
S.5        U[        U5      -  nX0R                  :X  d  M    O   U R                  (       d"  S
U l        U R                  U R                  5        U R                  SS05        g! [         a-    U R                  SSS/S.5        U R                  SSS.5         gf = f)z
Called in a subthread to run the WSGI app. We encapsulate like
this so that the start_response callable is called in the same thread.
rr   i  )s   content-types
   text/plainrs   zhttp.response.bodys'   Bad Request: Too many duplicate headers)r+   r.   Nr   T)r+   r.   r/   r+   )
rl   r   r0   r4   r   r}   r'   rp   r(   rZ   )r   r.   rf   
bytes_sentoutputbytes_alloweds         r   r5   WsgiToAsgiInstance.run_wsgi_app   sP   	((T:G$ 
++G5H5HIF(((,%t223++7 $ < <z Iv;.#N]3FNN-vDQ #f+%J999# J& $$$(D!NN4../ 456Q  	NN1! @A NN0F 	s   D   4EE)r   r(   rp   r'   r   r4   r   r   r   )r   r   r   r    r!   r   r   rl   r}   r   r5   r"   r#   r   r   r   r      s1    ,*&8t
@ 07 07r   r   )
r[   collectionsr   tempfiler   asgiref.syncr   r   r   r   r#   r   r   <module>r      s'    
 # ) 3
 
(i7 i7r   