
    7iK                         d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZ d dlmZ ddlmZ  G d d	e      Z G d
 dej$                        Zy)    N)OverflowableBufferReadOnlyFileBasedBuffer)HTTPRequestParser)	ErrorTaskWSGITask)InternalServerError   )	wasyncorec                       e Zd ZdZy)ClientDisconnectedz3Raised when attempting to write to a closed socket.N)__name__
__module____qualname____doc__     C/var/www/html/venv/lib/python3.12/site-packages/waitress/channel.pyr   r      s    =r   r   c                       e Zd ZdZeZeZeZ	dZ
dZdZdZdZdZdZddZd Zd Zd Zdd	Zd
 Zd Zd Zd ZddZddZd ZddZddZd Zd Z d Z!d Z"y)HTTPChannelz
    Setting self.requests = [somerequest] prevents more requests from being
    received until the out buffers have been flushed.

    Setting self.requests = [] allows more requests to be received.
    Nr   Fc                    || _         || _        t        |j                        g| _        t        j
                         x| _        | _        |j                  t        j                  t        j                        | _        t        j                         | _        t        j                          | _        t$        j&                  j)                  | ||       d| _        || _        g | _        y )N)mapT)serveradjr   outbuf_overflowoutbufstimecreation_timelast_activity
getsockoptsocket
SOL_SOCKET	SO_SNDBUFsendbuf_len	threadingLockrequests_lock	Conditionoutbuf_lockr
   
dispatcher__init__	connectedaddrrequests)selfr   sockr,   r   r   s         r   r*   zHTTPChannel.__init__8   s    *3+>+>?@26))+=T/??6+<+<f>N>NO '^^-$..0%%dDc%:	r   c                     | j                    S )z
        This method is inserted into the environment of any created task so it
        may occasionally check if the client has disconnected and interrupt
        execution.
        )r+   r.   s    r   check_client_disconnectedz%HTTPChannel.check_client_disconnectedJ   s     >>!!r   c                 R    | j                   xs | j                  xs | j                  S N)total_outbufs_len
will_closeclose_when_flushedr1   s    r   writablezHTTPChannel.writableS   s$    
 %%SSD<S<SSr   c                 B   | j                   s| j                  }n2| j                  | j                  j                  k\  r| j
                  }nd }| j                  |       | j                  r| j                  sd| _        d| _        | j                  r| j                          y y )NFT)
r-   _flush_somer5   r   
send_bytes_flush_some_if_lockable_flush_exceptionr7   r6   handle_close)r.   flushs     r   handle_writezHTTPChannel.handle_writeZ   s    
 }} $$E##txx':'::
 00E Ee$""4+A+A&+D#"DO?? r   c                    |r	  ||      dfS y # t         $ r; | j                  j                  r| j                  j	                  d       d| _        Y yt        $ r% | j                  j	                  d       d| _        Y yw xY w)Ndo_closeFSocket errorT)FTz"Unexpected exception when flushing)OSErrorr   log_socket_errorslogger	exceptionr6   	Exception)r.   r?   rC   s      r   r=   zHTTPChannel._flush_exceptionz   s~    %x0%88   %88--KK)).9"&$ %%%&JK"&$	%s   
 AB *B ?B c                     | j                   xsH | j                  xs: t        | j                        | j                  j
                  kD  xs | j                   S r4   )r6   r7   lenr-   r   channel_request_lookaheadr5   r1   s    r   readablezHTTPChannel.readable   sS     OO &&&&4==!DHH$F$FF& %%	
 	
r   c                 X   	 | j                  | j                  j                        }|r+t        j                         | _	        | j                  |       y d| _        y # t        $ rD | j                  j                  r| j
                  j                  d       | j                          Y y w xY w)NrD   F)recvr   
recv_bytesrE   rF   rG   rH   r>   r   r   receivedr+   )r.   datas     r   handle_readzHTTPChannel.handle_read   s    	99TXX001D !%DMM$ #DN  	xx))%%n5	s   %A A
B)(B)c                 b   d| j                   _        d}t        |      }| j                  5  | j                  d   j                  |       | xj                  |z  c_        | xj                  |z  c_        d| _        | j                          ddd       d| j                   _
        y# 1 sw Y   xY w)a  
        Send a 100-Continue header to the client. This is either called from
        receive (if no requests are running and the client expects it) or at
        the end of service (if no more requests are queued and a request has
        been read partially that expects it).
        Fs   HTTP/1.1 100 Continue

TN)requestexpect_continuerK   r(   r   appendcurrent_outbuf_countr5   sent_continuer:   	completed)r.   outbuf_payload	num_bytess      r   send_continuezHTTPChannel.send_continue   s     (-$9'	 	LL##N3%%2%""i/"!%D	 "'	 	s   A B%%B.c                     |sy| j                   5  | j                  s| j                  r
	 ddd       y|rE| j                   | j	                  | j
                        | _        | j                  j                  |      }| j                  j                  r>| j                  j                  r(| j                  s| j                  s| j                          | j                  j                  r|d| _
        | j                  j                  sX| j                  j                  | j                         t        | j                        dk(  r| j                   j#                  |        d| _        |t        |      k\  rn	||d }|rEddd       y# 1 sw Y   yxY w)zh
        Receives input asynchronously and assigns one or more requests to the
        channel.
        FNr	   T)r&   r6   r7   rV   parser_classr   rQ   rW   headers_finishedr-   rZ   r^   r[   emptyrX   rK   r   add_task)r.   rR   ns      r   rQ   zHTTPChannel.received   sB     +	  $"9"9+	  +	  <<'#'#4#4TXX#>DLLL))$/ LL0055 MM ..&&(<<))).D&<<--,,T\\:t}}-2
 !KK006#'DLD	>ABxC +	 Z [+	 Z s   FEFFc                 N   | j                   j                  d      rk	 | j                  |       | j                  | j                  j
                  k  r| j                   j                          | j                   j                          y y # | j                   j                          w xY wNFrB   )r(   acquirer:   r5   r   outbuf_high_watermarknotifyrelease)r.   rC   s     r   r<   z#HTTPChannel._flush_some_if_lockable   s     ##E*+  ( 3))DHH,J,JJ$$++-  ((* +   ((*s   AB B$c                 N   d}d}	 | j                   d   }|j                         }|dkD  rk|j                  | j                        }| j	                  ||      }|r2|j                  |d       ||z  }||z  }| xj                  |z  c_        nd}nM|dkD  rkt        | j                         dkD  r-| j                   j                  d      }	 |j                          nd}|rn|rt        j                         | _        yy# t        $ r | j                  j                  d       Y Gw xY w)Nr   FTrB   r	   z'Unexpected error when closing an outbuf)r   __len__getr#   sendskipr5   rK   popcloserI   rG   rH   r   r   )	r.   rC   sentdobreakoutbuf	outbuflenchunknum_senttocloses	            r   r:   zHTTPChannel._flush_some  s+    \\!_F (Ia-

4#3#3499UX9>KK$/)IH$D**h6* #G a-" t||$q("ll..q1GY
 #GE H !%D % Y--.WXYs   	C= =$D$#D$c                 l   | j                   5  | j                  D ]  }	 |j                           d| _        d| _        | j                   j                          d d d        t        j                  j                  |        y # t        $ r | j                  j                  d       Y w xY w# 1 sw Y   SxY w)Nz.Unknown exception while trying to close outbufr   F)r(   r   rq   rI   rG   rH   r5   r+   ri   r
   r)   )r.   rt   s     r   r>   zHTTPChannel.handle_close3  s     
	&,, LLN &'D""DN##%
	& 	""4( ! KK))H	
	& 
	&s-   B*B *B* $B'$B*&B''B**B3c                     t         j                  j                  | |       | | j                  j                  | j
                  <   y)zTSee wasyncore.dispatcher

        This hook keeps track of opened channels.
        N)r
   r)   add_channelr   active_channels_fileno)r.   r   s     r   r{   zHTTPChannel.add_channelA  s2    
 	((s348##DLL1r   c                     | j                   }t        j                  j                  | |       | j                  j
                  }||v r||= yy)zTSee wasyncore.dispatcher

        This hook keeps track of closed channels.
        N)r}   r
   r)   del_channelr   r|   )r.   r   fdacs       r   r   zHTTPChannel.del_channelI  sE    
 \\((s3[[((82 r   c                 
   | j                   st        |r| j                  5  | j                          | j                   st        t	        |      }t        |t              r]| j                  j                  |       t        | j                  j                        }| j                  j                  |       d| _        n| j                  | j                  j                  k\  rAt        | j                  j                        }| j                  j                  |       d| _        | j                  d   j                  |       | xj                  |z  c_        | xj                  |z  c_        | j                  | j                  j                  k\  ra| j!                  | j"                  d      \  }}|s%|r#| j                  | j                  j                  k\  r| j$                  j'                          d d d        |S y# 1 sw Y   S xY w)Nr   rU   FrB   )r+   r   r(   #_flush_outbufs_below_high_watermarkrK   
isinstancer   r   rX   r   r   r   rY   rh   r5   r;   r=   r:   r   pull_trigger)r.   rR   r]   nextbufflushedrH   s         r   
write_soonzHTTPChannel.write_soonY  s   ~~ %$ !! "388:~~,,I	d$;<LL''-01I1IJGLL''001D-00DHH4R4RR #5TXX5M5M"N++G4451LL$++D1--:-&&)3&))TXX-@-@@+/+@+@((5 ,A ,(Wi
 "&11TXX5H5HH002E"3H M"3H s   GG88Hc                 v   | j                   | j                  j                  kD  r	| j                  5  | j	                  | j
                  d      \  }}|r>| j                  j                          | j                  j                          	 d d d        y | j                  r| j                   | j                  j                  kD  rd| j                  j                          | j                  j                          | j                  r$| j                   | j                  j                  kD  rdd d d        y y # 1 sw Y   y xY wrf   )
r5   r   rh   r(   r=   r:   r   r   waitr+   )r.   _rH   s      r   r   z/HTTPChannel._flush_outbufs_below_high_watermark  s     !!DHH$B$BB!! ,!%!6!6t7G7GRW!6!XI KK,,.$$))+, , NN..1O1OOKK,,.$$))+	 NN..1O1OO, , C, ,s   AD/BD//D8c                 L   | j                   d   }|j                  r| j                  | |      }n| j                  | |      }	 | j                  r|j                          nd|_        |j                  rF| j0                  5  d| _        | j                   D ]  }|j5                           g | _         ddd       n&t7        | j                         dkD  r| j9                          | j:                  dkD  r| j                  j<                  | _        |j5                          | j0                  5  | j                   j?                  d       | j                  r(| j                   r| j@                  jC                  |        n`| j                  rT| j                  H| j                  jD                  r2| j                  jF                  r| jH                  s| jK                          ddd       | j                  r| j@                  jM                          tO        jN                         | _(        y# t        $ r= | j                  j                  d|j                  j                  z         d|_        Y t        $ r | j                  j                  d|j                  j                  z         |j                  s| j                  j                   rt#        j$                         }nd}|j&                  }|j(                  }| j+                  | j                        }t-        |      |_        ||_        	 |d   |j(                  d<   n# t.        $ r Y nw xY w| j                  | |      }	 |j                          n# t        $ r
 d|_        Y nw xY wd|_        Y "w xY w# 1 sw Y   xY w# 1 sw Y   xY w)	z]Execute one request. If there are more, we add another task to the
        server at the end.r   Tz$Client disconnected while serving %szException while serving %sz:The server encountered an unexpected internal server error
CONNECTIONNr	   ))r-   errorerror_task_class
task_classr+   serviceclose_on_finishr   rG   inforV   pathrI   rH   wrote_headerr   expose_tracebacks	traceback
format_excversionheadersr`   r   KeyErrorr&   r7   rq   rK   r   rY   rh   rp   r   rc   rW   ra   rZ   r^   r   r   r   )r.   rV   taskbodyreq_versionreq_headerserr_requests          r   r   zHTTPChannel.service  s    --"==((w7D??41D!	,~~'+$> ## #*.'#}} $GMMO$ "# # 4==!A%88: ((1,,0HH,J,J)MMO ## )!!!$>>dmmKK((.NN04455 ..
 &&()" >>KK$$&!YY[c " 	(KKCdllFWFWWX#'D  	,KK!!">ARAR"RS$$88--$//1DWD%oo%oo"//9$7$=! '2#8CL8QK''5 ,,T;?0LLN) 0+/D(0 (,$3	,8# #<) )s{   $H >0N&B0NAN	#B>N	"L54N	5	M>N	 MN	M('N	(M;8N	:M;;
N	N	NN#c                 `    d| _         d| _        t        j                         | _        g | _        y)z%Cancels all pending / active requestsTFN)r6   r+   r   r   r-   r1   s    r   cancelzHTTPChannel.cancel  s%    !YY[r   r4   )T)#r   r   r   r   r   r   r   r   r   r`   rV   r   r6   r7   rZ   r5   rY   r*   r2   r8   r@   r=   rM   rS   r^   rQ   r<   r:   r>   r{   r   r   r   r   r   r   r   r   r   r      s     J $L GMJM$"T @% 
 #"'$6p+/b)9
 /b,.a)Fr   r   )r    r$   r   r   waitress.buffersr   r   waitress.parserr   waitress.taskr   r   waitress.utilitiesr    r
   rI   r   r)   r   r   r   r   <module>r      sB        H - - 2 > >j)&& jr   