
    Jn6ha                     T    d dl mZ d dlmZ d dlmZmZ  G d d      Z G d d      Zy)	    )BytesIO)SpooledTemporaryFile)AsyncToSyncsync_to_asyncc                       e Zd ZdZd Zd Zy)
WsgiToAsgizG
    Wraps a WSGI application to make it into an ASGI application.
    c                     || _         y N)wsgi_applicationselfr   s     ]/var/www/django-portfolio.reubendavern.link/venv/lib/python3.12/site-packages/asgiref/wsgi.py__init__zWsgiToAsgi.__init__   s
     0    c                 Z   K    t        | j                        |||       d{    y7 w)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   scopereceivesends       r   __call__zWsgiToAsgi.__call__   s(      8 !6!67wMMMs   !+)+N)__name__
__module____qualname____doc__r   r    r   r   r   r      s    1Nr   r   c                   :    e Zd ZdZd Zd Zd ZddZed        Z	y)	r   z;
    Per-socket instance of a wrapped WSGI application
    c                 .    || _         d| _        d | _        y )NF)r   response_startedresponse_content_lengthr   s     r   r   zWsgiToAsgiInstance.__init__   s     0 %'+$r   c                   K   |d   dk7  rt        d      || _        t        d      5 }	  |        d {   }|d   dk7  rt        d      |j                  |j	                  dd	             |j	                  d
      snV|j                  d       t        |      | _        | j                  |       d {    d d d        y 7 7 # 1 sw Y   y xY ww)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   zWsgiToAsgiInstance.__call__"   s     =F"EFF
!51 	*T '	/6?n4$%WXX

7;;vs34{{;/  IIaL(.DN##D)))	* 	* * *	* 	*s?   'CCCB C6C7C;	CCCCCc                    |j                  dd      j                  d      j                  d      }|d   j                  d      j                  d      }|j                  |      r|t	        |      d }|d   |||d   j                  d	      d
|d   z  d|j                  dd      |t               dddd}d|v r |d   d   |d<   t        |d   d         |d<   n
d|d<   d|d<   |j                  d      |d   d   |d<   | j                  j                  dg       D ]n  \  }}|j                  d      }|dk(  rd}n+|dk(  rd}n#d |j                         j                  d!d"      z  }|j                  d      }||v r||   d#z   |z   }|||<   p |S )$zM
        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_NAMEr7   SERVER_PORT	localhost80clientREMOTE_ADDRheaderscontent-lengthCONTENT_LENGTHzcontent-typeCONTENT_TYPEzHTTP_%s-_,)
r(   encodedecode
startswithlenr   strr   upperreplace)	r   r   r$   script_name	path_infoenvironnamevaluecorrected_names	            r   build_environz WsgiToAsgiInstance.build_environ5   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:"9 $!%"
 u%*8_Q%7GM"%(x);%<GM"%0GM"%)GM"99X*%*8_Q%7GM"  ::>>)R8 	,KD%;;x(D''!1'!/!*TZZ\-A-A#s-K!KLL*E(/#5=&+GN#	, r   Nc                    | j                   r|d   j                  |d         t        | d      r|t        d      |j	                  dd      \  }}t        |      }|D cg c]5  \  }}|j                         j                  d      |j                  d      f7 }}}d| _        |D ])  \  }}|j                         dk(  st        |      | _        + d	||d
| _	        yc c}}w )z/
        WSGI start_response callable.
        r7      response_startNz=You cannot call start_response a second time without exc_info r5   rF   zhttp.response.start)r!   statusrE   )
r   with_tracebackhasattrr&   splitintlowerrL   r   r\   )	r   r^   response_headersexc_infostatus_coderJ   rV   rW   rE   s	            r   start_responsez!WsgiToAsgiInstance.start_responsef   s    
   1+,,Xa[994)*x/?O   c1-Q+&  0
e ZZ\  )5<<+@A
 

 (,$+ 	:KD%zz|///25z,	:
 *!

s   ":C c                 $   | j                  | j                  |      }d}| j                  || j                        D ]  }| j                  s"d| _        | j                  | j                         | j                  "| j                  |z
  }t        |      |kD  r|d| }| j                  d|dd       |t        |      z  }|| j                  k(  s n | j                  s"d| _        | j                  | j                         | j                  ddi       y)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.
        r   TNzhttp.response.body)r!   r$   r%   r!   )	rY   r   r   rg   r   r*   r\   r   rO   )r   r$   rU   
bytes_sentoutputbytes_alloweds         r   r+   zWsgiToAsgiInstance.run_wsgi_app   s    $$TZZ6
++GT5H5HI 	F(((,%t223++7 $ < <z Iv;.#N]3FNN-vDQ #f+%JT999#	& $$$(D!NN4../ 456r   r
   )
r   r   r   r   r   r   rY   rg   r   r+   r   r   r   r   r      s1    ,
*&/b
@ 7 7r   r   N)	ior   tempfiler   asgiref.syncr   r   r   r   r   r   r   <module>ro      s&     ) 3N N"N7 N7r   