Format: application/x-python-code
⬇ Download File (4.7 KB)
   1  o
   2  
   3  -ha�@sHddlmZddlmZddlmZmZGdd�d�ZGdd�d�ZdS)	�)�BytesIO)�SpooledTemporaryFile)�AsyncToSync�
   4  sync_to_asyncc@s eZdZdZdd�Zdd�ZdS)�
   5  WsgiToAsgizG
   6      Wraps a WSGI application to make it into an ASGI application.
   7      cCs
   8  ||_dS�N)�wsgi_application��selfr�r�BC:\Users\shubh\Videos\agent\nova\Lib\site-packages\asgiref/wsgi.py�__init__s
   9  zWsgiToAsgi.__init__c�s�t|j�|||�IdHdS)z�
  10          ASGI application instantiation point.
  11          We return a new WsgiToAsgiInstance here with the WSGI app
  12          and the scope, ready to respond when it is __call__ed.
  13          N)�WsgiToAsgiInstancer)r
  14  �scope�receive�sendrrr�__call__s�zWsgiToAsgi.__call__N)�__name__�
  15  __module__�__qualname__�__doc__r
  16  rrrrrrsrc@s>eZdZdZdd�Zdd�Zdd�Zd
  17  d	d
  18  �Zedd��Z	dS)rz;
  19      Per-socket instance of a wrapped WSGI application
  20      cCs||_d|_d|_dS)NF)r�response_started�response_content_lengthr	rrrr
  21  s
  22  zWsgiToAsgiInstance.__init__c�s��|ddkrtd��||_tdd��<}	|�IdH}|ddkr%td��|�|�d	d
  23  ��|�d�s4nq|�d�t|�|_|�|�IdHWd�dS1sRwYdS)
  24  N�type�httpz&WSGI wrapper received a non-HTTP scopei)�max_sizeTzhttp.requestz0WSGI wrapper received a non-HTTP-request message�body��	more_bodyr)	�
  25  ValueErrorrr�write�get�seekr�	sync_send�run_wsgi_app)r
  26  rrrr�messagerrrr"s"�
  27  �
  28  
  29  "�zWsgiToAsgiInstance.__call__c	
  30  Cs`|�dd��d��d�}|d�d��d�}|�|�r#|t|�d�}|d|||d�d	�d
  31  |dd|�d
  32  d�|t�dddd�}d|vrY|dd|d<t|dd�|d<nd|d<d|d<|�d�durp|dd|d<|j�dg�D]6\}}|�d�}|dkr�d}n|dkr�d}n
  33  d |���	d!d"�}|�d�}||vr�||d#|}|||<qw|S)$zM
  34          Builds a scope and request body into a WSGI environ object.
  35          �	root_path��utf8�latin1�pathN�methodZquery_string�asciizHTTP/%s�http_version)�r�schemerTF)�REQUEST_METHODZSCRIPT_NAMEZ	PATH_INFOZQUERY_STRINGZSERVER_PROTOCOLzwsgi.versionzwsgi.url_schemez
  36  wsgi.inputzwsgi.errorszwsgi.multithreadzwsgi.multiprocessz
  37  wsgi.run_once�serverrZSERVER_NAMEr.ZSERVER_PORT�	localhost�80�clientZREMOTE_ADDR�headers�content-lengthZCONTENT_LENGTHzcontent-typeZCONTENT_TYPEzHTTP_%s�-�_�,)
  38  r!�encode�decode�
  39  startswith�lenr�strr�upper�replace)	r
  40  rr�script_nameZ	path_info�environ�name�valueZcorrected_namerrr�
  41  build_environ5sH
  42  
  43  
  44  �
  45  
  46  
  47  z WsgiToAsgiInstance.build_environNc	Cs�|jr|d�|d��t|d�r|durtd��|�dd�\}}t|�}dd�|D�}d|_|D]\}}|��d	kr@t|�|_q1d
  48  ||d�|_dS)z/
  49          WSGI start_response callable.
  50          r.��response_startNz=You cannot call start_response a second time without exc_info� cSs(g|]\}}|���d�|�d�f�qS)r,)�lowerr:)�.0rCrDrrr�
  51  <listcomp>vs��z5WsgiToAsgiInstance.start_response.<locals>.<listcomp>r6zhttp.response.start)r�statusr5)	r�with_traceback�hasattrr�split�intrrIrG)	r
  52  rL�response_headers�exc_info�status_coder8r5rCrDrrr�start_responsefs(��
  53  ��z!WsgiToAsgiInstance.start_responsecCs�|�|j|�}d}|�||j�D]:}|jsd|_|�|j�|jdur4|j|}t|�|kr4|d|�}|�d|dd��|t|�7}||jkrJnq|jsWd|_|�|j�|�ddi�dS)z�
  54          Called in a subthread to run the WSGI app. We encapsulate like
  55          this so that the start_response callable is called in the same thread.
  56          rTNzhttp.response.body)rrrr)	rErrrTrr#rGrr=)r
  57  rrBZ
  58  bytes_sent�outputZ
  59  bytes_allowedrrrr$�s*
  60  
  61  
  62  �
  63  �zWsgiToAsgiInstance.run_wsgi_appr)
  64  rrrrr
  65  rrErTrr$rrrrrs
  66  1 rN)	�ior�tempfilerZasgiref.syncrrrrrrrr�<module>s
  67