
    GWPi5                     "   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZ d d	lmZ d d
lmZ  ej>                  d      Z d Z! G d de      Z" G d dejF                        Z$y)    N)aclosing)ThreadSensitiveContextsync_to_async)settings)signals)RequestAbortedRequestDataTooBig)base)FileResponseHttpRequestHttpResponseHttpResponseBadRequestHttpResponseServerError	QueryDictparse_cookie)set_script_prefix)cached_propertyzdjango.requestc                 n    t         j                  rt         j                  S | j                  dd      xs dS )zM
    Return the script prefix to use from either the scope or a setting.
    	root_path )r   FORCE_SCRIPT_NAMEget)scopes    O/root/twitch_bot/venv/lib/python3.12/site-packages/django/core/handlers/asgi.pyget_script_prefixr      s/     !!)))99["%++    c                        e Zd ZdZdZd Zed        Z fdZd Z	d Z
d Z ee	e
      Z ee      Zed	        Z fd
Z xZS )ASGIRequestzv
    Custom request subclass that decodes from an ASGI-standard request dict
    and wraps request body handling.
    <   c                 &   || _         d| _        d| _        d | _        t	        |      | _        | j
                  r$|d   j                  | j
                        | _        n
|d   | _        | j
                  rA| j
                  j                  d      d| j                  j                  ddd      | _
        n
|d   | _
        | j                   d   j                         | _        | j                   j                  dd      }t        |t              r|j!                         }| j                  || j
                  | j                  ddd	| _        | j                   j                  d
      rZ| j                   d
   d   | j"                  d<   | j"                  d   | j"                  d<   | j                   d
   d   | j"                  d<   | j                   j                  d      rH| j                   d   d   | j"                  d<   t%        | j                   d   d         | j"                  d<   nd| j"                  d<   d| j"                  d<   | j                   j                  dg       D ]  \  }}|j!                  d      }|dk(  rd}n+|dk(  rd}n#d|j                         j                  dd      z  }|j!                  d      }|| j"                  v r| j"                  |   dz   |z   }|| j"                  |<    | j'                  | j"                         || _        d | _        y )NFpath/r      methodquery_stringT)REQUEST_METHODQUERY_STRINGSCRIPT_NAME	PATH_INFOzwsgi.multithreadzwsgi.multiprocessclientr   REMOTE_ADDRREMOTE_HOSTREMOTE_PORTserverSERVER_NAMESERVER_PORTunknown0headerslatin1zcontent-lengthCONTENT_LENGTHzcontent-typeCONTENT_TYPEzHTTP_%s-_,)r   _post_parse_error_read_startedresolver_matchr   script_nameremoveprefix	path_inforstripreplacer!   upperr$   r   
isinstancebytesdecodeMETAstr_set_content_type_params_stream)selfr   	body_filer%   namevaluecorrected_names          r   __init__zASGIRequest.__init__0   s   
!&"",U3"6]778H8HIDN"6]DN   '',&&sB2DI
 fDIjj*002zz~~nb9lE*'..0L"kk(++ $!%
	 ::>>(#'+zz(';A'>DIIm$'+yy'?DIIm$'+zz(';A'>DIIm$::>>(#'+zz(';A'>DIIm$'*4::h+?+B'CDIIm$'0DIIm$'*DIIm$::>>)R8 	.KD%;;x(D''!1'!/!*TZZ\-A-A#s-K!K LL*E*		.1C7%?(-DIIn%	. 	%%dii0 "r   c                 2    t        | j                  d         S )Nr'   )r   rF   rJ   s    r   GETzASGIRequest.GETs   s    >233r   c                 Z    | j                   j                  d      xs t        |          S )Nscheme)r   r   super_get_schemerJ   	__class__s    r   rV   zASGIRequest._get_schemew   s#    zz~~h'@57+>+@@r   c                 R    t        | d      s| j                          | j                  S )N_post)hasattr_load_post_and_filesrZ   rQ   s    r   	_get_postzASGIRequest._get_postz   s"    tW%%%'zzr   c                     || _         y N)rZ   )rJ   posts     r   	_set_postzASGIRequest._set_post   s	    
r   c                 R    t        | d      s| j                          | j                  S )N_files)r[   r\   rc   rQ   s    r   
_get_fileszASGIRequest._get_files   s"    tX&%%'{{r   c                 L    t        | j                  j                  dd            S )NHTTP_COOKIEr   )r   rF   r   rQ   s    r   COOKIESzASGIRequest.COOKIES   s    DIIMM-<==r   c                 V    t         |           | j                  j                          y r_   )rU   closerI   rW   s    r   ri   zASGIRequest.close   s    r   )__name__
__module____qualname____doc__body_receive_timeoutrO   r   rR   rV   r]   ra   rd   propertyPOSTFILESrg   ri   __classcell__rX   s   @r   r   r   &   st     A#F 4 4A

 Iy)DZ E> > r   r   c                   n     e Zd ZdZeZdZ fdZd Zd Z	d Z
d Zd Zd	 Z fd
Zd Zed        Z xZS )ASGIHandlerzHandler for ASGI requests.i   c                 F    t         |           | j                  d       y )NT)is_async)rU   rO   load_middlewarerW   s    r   rO   zASGIHandler.__init__   s    d+r   c                    K   |d   dk7  rt        d|d   z        t               4 d{    | j                  |||       d{    ddd      d{    y7 07 7 	# 1 d{  7  sw Y   yxY ww)zV
        Async entrypoint - parses the request and hands off to get_response.
        typehttpz5Django can only handle ASGI/HTTP connections, not %s.N)
ValueErrorr   handle)rJ   r   receivesends       r   __call__zASGIHandler.__call__   s      =F"G%PV-W  *+ 	4 	4++eWd333	4 	4 	43	4 	4 	4 	4sV   (A6AA6A!AA!
A6AA6A!A6!A3'A*(A3/A6c                    K   	  j                  |       d{   }t        t        |             t        j
                  j                   j                  |       d{     j                  ||      \  }}|+|j                           j                  ||       d{    y fd}t        j                   j                  |            t        j                   |||            g}t        j                  |t        j                         d{    |D ]?  }	|	j!                         r	 |	j#                          %|	j'                          	 |	 d{    A |j                          y7 U# t        $ r Y yw xY w7  7 7 p# t        $ r Y wt$        $ r |j                           w xY w7 X# t        j(                  $ r Y w xY ww)zK
        Handles the ASGI request. Called via the __call__ method.
        N)senderr   c                 |   K   j                  |        d {   }j                  ||       d {    y 7 7 wr_   )run_get_responsesend_response)requestr   responserJ   s      r   process_requestz+ASGIHandler.handle.<locals>.process_request   s9     !227;;H$$Xt444 <4s   <8<:<<)return_when)	read_bodyr   r   r   r   request_startedasendrX   create_requestri   r   asynciocreate_tasklisten_for_disconnectwaitFIRST_COMPLETEDdoneresultAssertionErrorcancelCancelledError)
rJ   r   r~   r   rK   r   error_responser   taskstasks
   `         r   r}   zASGIHandler.handle   s    
	"nnW55I 	+E23%%++4>>+OOO"&"5"5eY"G?OO$$^T:::	5  : :7 CD >?
 ll5g.E.EFFF 	Dyy{KKM JJ	$ 	] 6 		 	P
 ;  	G & % OO% -- s   G
E1 E.E1 AG
 F !A G
!F"A2G
FG
/F?G
F1F/F1G
.E1 1	E=:G
<E==G
G
G
	F,G
F,,G
/F11GG
GG
c                 j   K    |        d{   }|d   dk(  r
t               J d|d   z         7 #w)z&Listen for disconnect from the client.Nrz   http.disconnectz+Invalid ASGI message after request body: %s)r   )rJ   r~   messages      r   r   z!ASGIHandler.listen_for_disconnect   s>     	/6?// ""UCgfoUUu	 "s   31$3c                    K   | j                  |       d{   }| j                  |_        t        |t              r| j
                  |_        |S 7 8w)zGet async response.N)get_response_asyncrX   _handler_classrC   r   
chunk_size
block_size)rJ   r   r   s      r   r   zASGIHandler.run_get_response   sI      0099"&.. h-"&//H :s   AA9Ac                 :  K   t        j                  t        j                  d      }	  |        d{   }|d   dk(  r|j	                          t               d|v r|j                  |d          |j                  dd      sn]|j                  d	       |S 7 ew)
z+Reads an HTTP body from an ASGI connection.zw+b)max_sizemodeNrz   r   body	more_bodyFr   )	tempfileSpooledTemporaryFiler   FILE_UPLOAD_MAX_MEMORY_SIZEri   r   writer   seek)rJ   r~   rK   r   s       r   r   zASGIHandler.read_body   s      1199
	 #IoGv"33!$&& 0;;{E2  	q &s   1BBA&Bc                     	 | j                  ||      dfS # t        $ r; t        j                  dt	        j
                         ddi       dt               fcY S t        $ r dt        dd      fcY S w xY w)	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_infoextraz413 Payload too largei  )status)	request_classUnicodeDecodeErrorloggerwarningsysr   r   r	   r   )rJ   r   rK   s      r   r   zASGIHandler.create_request
  s    

	K%%eY7==! 	2NN2$c*  
 /111  	K&=cJJJ	Ks    AA3A32A3c                     	 t         |   |||      S # t        $ r4 t        t        j
                  rt        j                         ndd      cY S w xY w)z#Last-chance handler for exceptions.zInternal Server Errorz
text/plain)content_type)rU   handle_uncaught_exception	Exceptionr   r   DEBUG	traceback
format_exc)rJ   r   resolverr   rX   s       r   r   z%ASGIHandler.handle_uncaught_exception  sP    	74WhQQ 	**2..	$$&>U) 	s    :AAc           	        K   g }|j                         D ]l  \  }}t        |t              r|j                  d      }t        |t              r|j                  d      }|j	                  t        |      t        |      f       n |j                  j                         D ]B  }|j	                  d|j                  d      j                  d      j                         f       D  |d|j                  |d       d{    |j                  rVt        t        |            4 d{   }|2 3 d{   }| j                  |      D ]  \  }	}
 |d	|	d
d       d{     7| j                  |j                        D ]  \  }	} |d	|	| d       d{      t!        |j"                  d
              d{    y7 7 7 7 j6 ddd      d{  7   n# 1 d{  7  sw Y   nxY w |dd	i       d{  7   i7 o7 Kw)z)Encode and send a response out over ASGI.asciir4   s
   Set-Cookier   )headerzhttp.response.start)rz   r   r3   Nzhttp.response.bodyT)rz   r   r   rz   )thread_sensitive)itemsrC   rG   encodeappendrD   cookiesvaluesoutputstripr   	streamingr   aiterchunk_bytescontentr   ri   )rJ   r   r   response_headersr   rM   cr   partchunkr8   lasts               r   r   zASGIHandler.send_response'  s$     %^^- 	CMFE&#&w/%%X.##U6]E%L$AB	C !!((* 	A## 3 : :7 C I I KL	
 -"..+
 	
 	
 
  h0  G") 
 
$$($4$4T$: 	q"(<(- .2  	  $//0@0@A t 4 %)-X   CmHNNTBDDDM	

 #*      45666
 	Es   C:H<G =(H%G&H)G,G0G1G4'GG
G#1HH%H:H;HHGGG	HGHG-!G$"G-)H=H >HHc              #      K   d}|s|df y|t        |      k  rN|||| j                  z    || j                  z   t        |      k\  f || j                  z  }|t        |      k  rMyyw)z
        Chunks some data up so it can be sent in reasonable size messages.
        Yields (chunk, last_chunk) tuples.
        r   TN)lenr   )clsdatapositions      r   r   zASGIHandler.chunk_bytes_  sy      *T"X3>> 9:CNN*s4y8  &H T"s   A&A+)A+)rj   rk   rl   rm   r   r   r   rO   r   r}   r   r   r   r   r   r   classmethodr   rr   rs   s   @r   ru   ru      sV    $MJ,44lV	*K"
6Ep ' 'r   ru   )%r   loggingr   r   r   
contextlibr   asgiref.syncr   r   django.confr   django.corer   django.core.exceptionsr   r	   django.core.handlersr
   django.httpr   r   r   r   r   r   r   django.urlsr   django.utils.functionalr   	getLoggerr   r   r   BaseHandlerru    r   r   <module>r      sx      
    >    D %   * 3			+	,,j+ jZ['$"" ['r   