/ lib / tornado / iostream.pyc
iostream.pyc
  1  o

  2  ��c
  3  �@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
  4  Z
  5  ddlmZm
Z
ddlmZddlmZddlmZmZmZddlmZddlZddlmZmZmZmZmZmZmZm Z m!Z!dd	l"m#Z#ej$r{dd
  6  lm%Z%m&Z&m'Z'e ddd
�Z(ej)ej*ej+ej,fZ-e.ed�r�e-ej/ej0ej1f7Z-e	j2dkr�e-ej3f7Z-e	j2�4d�Z5Gdd�de6�Z7Gdd�de8�Z9Gdd�de8�Z:Gdd�de;�Z<Gdd�de;�Z=Gdd�de=�Z>Gdd�de>�Z?Gdd�de=�Z@d efd!d"�ZAdS)#aBUtility classes to write to and read from non-blocking files and sockets.
  7  
  8  Contents:
  9  
 10  * `BaseIOStream`: Generic interface for reading and writing.
 11  * `IOStream`: Implementation of BaseIOStream using non-blocking sockets.
 12  * `SSLIOStream`: SSL-aware version of IOStream.
 13  * `PipeIOStream`: Pipe-based IOStream implementation.
 14  �N)�Future�"future_set_result_unless_cancelled)�ioloop)�gen_log)�ssl_wrap_socket�_client_ssl_defaults�_server_ssl_defaults)�errno_from_exception)	�Union�Optional�	Awaitable�Callable�Pattern�Any�Dict�TypeVar�Tuple)�
TracebackType)�Deque�List�Type�
_IOStreamType�IOStream)�bound�
WSAECONNRESET�darwin�wincs0eZdZdZddeeddf�fdd�
Z�ZS)�StreamClosedErrora�Exception raised by `IOStream` methods when the stream is closed.
 15  
 16      Note that the close callback is scheduled to run *after* other
 17      callbacks on the stream (to allow for buffered data to be processed),
 18      so you may see this error before you see the close callback.
 19  
 20      The ``real_error`` attribute contains the underlying error that caused
 21      the stream to close (if any).
 22  
 23      .. versionchanged:: 4.3
 24         Added the ``real_error`` attribute.
 25      N�
 26  real_error�returncst��d�||_dS)NzStream is closed)�super�__init__r)�selfr��	__class__���C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tornado\iostream.pyr!bs
 27  zStreamClosedError.__init__�N)�__name__�
 28  __module__�__qualname__�__doc__r�
BaseExceptionr!�
__classcell__r%r%r#r&rTs$
rc@�eZdZdZdS)�UnsatisfiableReadErrorz�Exception raised when a read cannot be satisfied.
 29  
 30      Raised by ``read_until`` and ``read_until_regex`` with a ``max_bytes``
 31      argument.
 32      N�r(r)r*r+r%r%r%r&r/gsr/c@r.)�StreamBufferFullErrorz?Exception raised by `IOStream` methods when the buffer is full.Nr0r%r%r%r&r1qsr1c@sleZdZdZddd�Zdefdd�ZdZd	ee	e
 33  efddfd
 34  d�Zdedefd
d�Z
deddfdd�ZdS)�
_StreamBufferzh
 35      A specialized buffer that tries to avoid copies when large pieces
 36      of data are encountered.
 37      rNcCst��|_d|_d|_dS�Nr)�collections�deque�_buffers�
 38  _first_pos�_size�r"r%r%r&r!{s�
 39  z_StreamBuffer.__init__cC�|jSr')r8r9r%r%r&�__len__��z_StreamBuffer.__len__i�datacCs�t|�}||jkrt|t�st|�}|j�d|f�n+|dkrF|jr3|jd\}}|p1t|�|jk}nd}|rB|j�dt|�f�n||7}|j|7_dS)zX
 40          Append the given piece of data (should be a buffer-compatible object).
 41          Tr�����FN)�len�_large_buf_threshold�
 42  isinstance�
 43  memoryviewr6�append�	bytearrayr8)r"r=�size�
 44  is_memview�bZnew_bufr%r%r&rC�s
 45  
 46  z_StreamBuffer.appendrEcCsp|dksJ�z	|jd\}}Wn
tytd�YSw|j}|r.t�t||||��St|�|||�S)zq
 47          Get a view over at most ``size`` bytes (possibly fewer) at the
 48          current buffer position.
 49          r�)r6�
 50  IndexErrorrBr7�typing�cast)r"rErFrG�posr%r%r&�peek�s�z_StreamBuffer.peekcCs�d|kr|jksJ�J�|j|8_|j}|j}|rk|dkrk|d\}}t|�||}|dkrC|��|t|�|8}d}n"|rL||7}d}n||7}t|�d|krct�t|�d|�=d}d}|rk|dks"|dksqJ�||_dS)zH
 51          Advance the current buffer position by ``size`` bytes.
 52          r�N)r8r7r6r?�popleftrJrKrD)r"rErL�buffersZis_largerGZb_remainr%r%r&�advance�s,�
 53  z_StreamBuffer.advance�rN)r(r)r*r+r!�intr;r@r
 54  �bytesrDrBrCrMrQr%r%r%r&r2us
 55  	r2c@s�eZdZdZ			dcdeedeedeeddfdd�Zdeeej	ffd	d
 56  �Z
 57  dddd�Zd
edefdd�Z
deeefdeefdd�Zdeefdd�Z	dededeedeefdd�Z	dededeedeefdd�Zdfdededeefdd �Zdfdededeefd!d"�Zdeefd#d$�Zd
eeefdd%fd&d'�Zd(eegdfddfd)d*�Z	dfd+edeeed,eeeeffddfd-d.�Z ddd/d0�Z!defd1d2�Z"defd3d4�Z#defd5d6�Z$d7eddfd8d9�Z%ddd:d;�Z&d<eeej	fd=eddfd>d?�Z'deefd@dA�Z(dddBdC�Z)de*fdDdE�Z+dFeddfdGdH�Z,dddIdJ�Z-deefdKdL�Z.dMeddfdNdO�Z/deefdPdQ�Z0deee1fdFeddfdRdS�Z2dddTdU�Z3dVedefdWdX�Z4dddYdZ�Z5ddd[d\�Z6d]eddfd^d_�Z7d`edefdadb�Z8dS)g�BaseIOStreama�A utility class to write to and read from a non-blocking file or socket.
 58  
 59      We support a non-blocking ``write()`` and a family of ``read_*()``
 60      methods. When the operation completes, the ``Awaitable`` will resolve
 61      with the data read (or ``None`` for ``write()``). All outstanding
 62      ``Awaitables`` will resolve with a `StreamClosedError` when the
 63      stream is closed; `.BaseIOStream.set_close_callback` can also be used
 64      to be notified of a closed stream.
 65  
 66      When a stream is closed due to an error, the IOStream's ``error``
 67      attribute contains the exception object.
 68  
 69      Subclasses must implement `fileno`, `close_fd`, `write_to_fd`,
 70      `read_from_fd`, and optionally `get_fd_error`.
 71  
 72      N�max_buffer_size�read_chunk_size�max_write_buffer_sizercCs�tj��|_|p	d|_t|pd|jd�|_||_d|_t	�|_
 73  d|_d|_d|_
d|_t�|_d|_d|_d|_d|_d|_d|_d|_d|_d|_t��|_d|_d|_d|_d|_ d|_!d|_"dS)a�`BaseIOStream` constructor.
 74  
 75          :arg max_buffer_size: Maximum amount of incoming data to buffer;
 76              defaults to 100MB.
 77          :arg read_chunk_size: Amount of data to read at one time from the
 78              underlying transport; defaults to 64KB.
 79          :arg max_write_buffer_size: Amount of outgoing data to buffer;
 80              defaults to unlimited.
 81  
 82          .. versionchanged:: 4.0
 83             Add the ``max_write_buffer_size`` parameter.  Changed default
 84             ``read_chunk_size`` to 64KB.
 85          .. versionchanged:: 5.0
 86             The ``io_loop`` argument (deprecated since version 4.1) has been
 87             removed.
 88          i@irNNrF)#r�IOLoop�current�io_looprV�minrWrX�errorrD�_read_buffer�_read_buffer_pos�_read_buffer_size�_user_read_buffer�_after_user_read_bufferr2�
_write_buffer�_total_write_index�_total_write_done_index�_read_delimiter�_read_regex�_read_max_bytes�_read_bytes�
_read_partial�_read_until_close�_read_futurer4r5�_write_futures�_close_callback�_connect_future�_ssl_connect_future�_connecting�_state�_closed)r"rVrWrXr%r%r&r!�s8
 89  �
 90  zBaseIOStream.__init__cC�t��)z,Returns the file descriptor for this stream.��NotImplementedErrorr9r%r%r&�fileno�zBaseIOStream.filenocCrt)z�Closes the file underlying this stream.
 91  
 92          ``close_fd`` is called by `BaseIOStream` and should not be called
 93          elsewhere; other users should call `close` instead.
 94          rur9r%r%r&�close_fd!szBaseIOStream.close_fdr=cCrt)ziAttempts to write ``data`` to the underlying file.
 95  
 96          Returns the number of bytes written.
 97          ru�r"r=r%r%r&�write_to_fd)szBaseIOStream.write_to_fd�bufcCrt)a�Attempts to read from the underlying file.
 98  
 99          Reads up to ``len(buf)`` bytes, storing them in the buffer.
100          Returns the number of bytes read. Returns None if there was
101          nothing to read (the socket returned `~errno.EWOULDBLOCK` or
102          equivalent), and zero on EOF.
103  
104          .. versionchanged:: 5.0
105  
106             Interface redesigned to take a buffer and return a number
107             of bytes instead of a freshly-allocated object.
108          ru�r"r|r%r%r&�read_from_fd0s
zBaseIOStream.read_from_fdcC�dS)a>Returns information about any error on the underlying file.
109  
110          This method is called after the `.IOLoop` has signaled an error on the
111          file descriptor, and should return an Exception (such as `socket.error`
112          with additional information, or None if no such information is
113          available.
114          Nr%r9r%r%r&�get_fd_error?szBaseIOStream.get_fd_error�regex�	max_bytesc
115  Cs�|��}t�|�|_||_z|��W|Sty5}zt�d|�|j	|d�|WYd}~Sd}~w|�
116  dd���)a�Asynchronously read until we have matched the given regex.
117  
118          The result includes the data that matches the regex and anything
119          that came before it.
120  
121          If ``max_bytes`` is not None, the connection will be closed
122          if more than ``max_bytes`` bytes have been read and the regex is
123          not satisfied.
124  
125          .. versionchanged:: 4.0
126              Added the ``max_bytes`` argument.  The ``callback`` argument is
127              now optional and a `.Future` will be returned if it is omitted.
128  
129          .. versionchanged:: 6.0
130  
131             The ``callback`` argument was removed. Use the returned
132             `.Future` instead.
133  
134          �*Unsatisfiable read, closing connection: %s��exc_infoNcS�|��Sr'��	exception��fr%r%r&�<lambda>l�z/BaseIOStream.read_until_regex.<locals>.<lambda>)�_start_read�re�compilergrh�_try_inline_readr/r�info�close�add_done_callback)r"r�r��future�er%r%r&�read_until_regexIs
135  ��zBaseIOStream.read_until_regex�	delimiterc
136  Csz|��}||_||_z|��W|Sty2}zt�d|�|j|d�|WYd}~Sd}~w|�dd���)anAsynchronously read until we have found the given delimiter.
137  
138          The result includes all the data read including the delimiter.
139  
140          If ``max_bytes`` is not None, the connection will be closed
141          if more than ``max_bytes`` bytes have been read and the delimiter
142          is not found.
143  
144          .. versionchanged:: 4.0
145              Added the ``max_bytes`` argument.  The ``callback`` argument is
146              now optional and a `.Future` will be returned if it is omitted.
147  
148          .. versionchanged:: 6.0
149  
150             The ``callback`` argument was removed. Use the returned
151             `.Future` instead.
152          r�r�NcSr�r'r�r�r%r%r&r��r�z)BaseIOStream.read_until.<locals>.<lambda>)	r�rfrhr�r/rr�r�r�)r"r�r�r�r�r%r%r&�
153  read_untilps
154  	��zBaseIOStream.read_untilF�	num_bytes�partialcCsJ|��}t|tj�sJ�||_||_z|��W|S|�dd���)aJAsynchronously read a number of bytes.
155  
156          If ``partial`` is true, data is returned as soon as we have
157          any bytes to return (but never more than ``num_bytes``)
158  
159          .. versionchanged:: 4.0
160              Added the ``partial`` argument.  The callback argument is now
161              optional and a `.Future` will be returned if it is omitted.
162  
163          .. versionchanged:: 6.0
164  
165             The ``callback`` and ``streaming_callback`` arguments have
166             been removed. Use the returned `.Future` (and
167             ``partial=True`` for ``streaming_callback``) instead.
168  
169          cSr�r'r�r�r%r%r&r��r�z)BaseIOStream.read_bytes.<locals>.<lambda>)r�rA�numbers�Integralrirjr�r�)r"r�r�r�r%r%r&�
170  read_bytes�s
171  �zBaseIOStream.read_bytescCs�|��}|j}t|�}||kr-|j|}t|j�|j|�|dd�<|jd|�=|j|_n|dkr?t|j�|jd�|d|�<d|_||_d|_||_||_||_	z|�
172  �W|S|�dd���)a�Asynchronously read a number of bytes.
173  
174          ``buf`` must be a writable buffer into which data will be read.
175  
176          If ``partial`` is true, the callback is run as soon as any bytes
177          have been read.  Otherwise, it is run when the ``buf`` has been
178          entirely filled with read data.
179  
180          .. versionadded:: 5.0
181  
182          .. versionchanged:: 6.0
183  
184             The ``callback`` argument was removed. Use the returned
185             `.Future` instead.
186  
187          NrTcSr�r'r�r�r%r%r&r��r�z(BaseIOStream.read_into.<locals>.<lambda>)r�r`r?r_rBr^rbrarirjr�r�)r"r|r�r�Zavailable_bytes�n�endr%r%r&�	read_into�s0
188  
189  �
190  �zBaseIOStream.read_intocCsL|��}|��r|�|j�|Sd|_z|��W|S|�dd���)a�Asynchronously reads all data from the socket until it is closed.
191  
192          This will buffer all available data until ``max_buffer_size``
193          is reached. If flow control or cancellation are desired, use a
194          loop with `read_bytes(partial=True) <.read_bytes>` instead.
195  
196          .. versionchanged:: 4.0
197              The callback argument is now optional and a `.Future` will
198              be returned if it is omitted.
199  
200          .. versionchanged:: 6.0
201  
202             The ``callback`` and ``streaming_callback`` arguments have
203             been removed. Use the returned `.Future` (and `read_bytes`
204             with ``partial=True`` for ``streaming_callback``) instead.
205  
206          TcSr�r'r�r�r%r%r&r��r�z/BaseIOStream.read_until_close.<locals>.<lambda>)r��closed�_finish_readr`rkr�r��r"r�r%r%r&�read_until_close�s
207  �zBaseIOStream.read_until_closezFuture[None]cCs�|��|r6t|t�rt|��d�}|jdur't|j�t|�|jkr'td��|j�|�|j	t|�7_	t
208  �}|�dd��|j�|j	|f�|j
s^|��|jrZ|�|jj�|��|S)a:Asynchronously write the given data to this stream.
209  
210          This method returns a `.Future` that resolves (with a result
211          of ``None``) when the write has been completed.
212  
213          The ``data`` argument may be of type `bytes` or `memoryview`.
214  
215          .. versionchanged:: 4.0
216              Now returns a `.Future` if no callback is given.
217  
218          .. versionchanged:: 4.5
219              Added support for `memoryview` arguments.
220  
221          .. versionchanged:: 6.0
222  
223             The ``callback`` argument was removed. Use the returned
224             `.Future` instead.
225  
226          �BNz!Reached maximum write buffer sizecSr�r'r�r�r%r%r&r�r�z$BaseIOStream.write.<locals>.<lambda>)�
_check_closedrArBrKrXr?rcr1rCrdrr�rmrq�
_handle_write�
_add_io_stater[�WRITE�_maybe_add_error_listener)r"r=r�r%r%r&�write�s$
227  
228  zBaseIOStream.write�callbackcCs||_|��dS)a�Call the given callback when the stream is closed.
229  
230          This mostly is not necessary for applications that use the
231          `.Future` interface; all outstanding ``Futures`` will resolve
232          with a `StreamClosedError` when the stream is closed. However,
233          it is still useful as a way to signal that the stream has been
234          closed while no other read or write is in progress.
235  
236          Unlike other callback-based interfaces, ``set_close_callback``
237          was not removed in Tornado 6.0.
238          N)rnr�)r"r�r%r%r&�set_close_callback(szBaseIOStream.set_close_callbackr�zOptional[Type[BaseException]]cCs�|��si|r't|t�r|d|_nt|t�r||_n
t��}t|�r'|d|_|jr4d|_|�	|j
239  �n|jdurRz|��}Wn	t
yHYn
240  w|durR|�|�|jdurb|j�|���d|_|��d|_|��dS)z�Close this stream.
241  
242          If ``exc_info`` is true, set the ``error`` attribute to the current
243          exception from `sys.exc_info` (or if ``exc_info`` is a tuple,
244          use that instead of `sys.exc_info`).
245          �FNT)r�rA�tupler]r,�sysr��anyrkr�r`rl�_find_read_posr/�_read_from_bufferrrr[�remove_handlerrwryrs�_signal_closed)r"r�rLr%r%r&r�7s4
246  
247  
248  
249  �
250  
251  zBaseIOStream.closec	Csg}|jdur|�|j�d|_|dd�|jD�7}|j��|jdur-|�|j�d|_|D]}|��s>|�t|jd��z|�	�Wq/t
252  jyNYq/w|jduru|j��sm|jdurf|j�|j�n|j�t��|j�	�d|_|j
dur�|j
}d|_
|j�|�d|_dS)NcSsg|]\}}|�qSr%r%)�.0�_r�r%r%r&�
253  <listcomp>lsz/BaseIOStream._signal_closed.<locals>.<listcomp>�r)rlrCrm�clearro�done�
set_exceptionrr]r��asyncio�CancelledErrorrprnr[�add_callbackrc)r"�futuresr��cbr%r%r&r�gs:
254  
255  
256  �
257  
258  
259  
260  
261  
262  zBaseIOStream._signal_closedcCs
263  |jduS)z=Returns ``True`` if we are currently reading from the stream.N)rlr9r%r%r&�reading��
264  zBaseIOStream.readingcCs
265  t|j�S)z;Returns ``True`` if we are currently writing to the stream.)�boolrcr9r%r%r&�writing�r�zBaseIOStream.writingcCr:)z/Returns ``True`` if the stream has been closed.)rsr9r%r%r&r��rxzBaseIOStream.closed�valuecCr)a�Sets the no-delay flag for this stream.
266  
267          By default, data written to TCP streams may be held for a time
268          to make the most efficient use of bandwidth (according to
269          Nagle's algorithm).  The no-delay flag requests that data be
270          written as soon as possible, even if doing so would consume
271          additional bandwidth.
272  
273          This flag is currently defined only for TCP-based ``IOStreams``.
274  
275          .. versionadded:: 3.1
276          Nr%)r"r�r%r%r&�set_nodelay�s
zBaseIOStream.set_nodelaycCrtr'rur9r%r%r&�_handle_connect�r<zBaseIOStream._handle_connect�fd�eventsc
277  Cs�|��rt�d|�dSz�|jr|��|��rWdS||jj@r%|��|��r,WdS||jj@r6|�	�|��r=WdS||jj
278  @rR|��|_|j�
|j�WdS|jj
279  }|��r`||jjO}|��rj||jjO}||jj
280  kr{|jdkr{||jjO}||jkr�|jdus�Jd��||_|j�|��|j�WdSWdSty�}zt�d|�|j|d�WYd}~dSd}~wty�}ztjddd�|j|d��d}~ww)NzGot events for closed stream %srz4shouldn't happen: _handle_events without self._stater�r�z'Uncaught exception, closing connection.T)r�r�warningrqr�r[�READ�_handle_readr�r��ERRORr�r]r�r�r�r�r`rr�update_handlerrwr/r��	Exception)r"r�r��stater�r%r%r&�_handle_events�sX
281  
282  �����zBaseIOStream._handle_eventscCs�|jdur	|j}n|jdur|j}n	|��rd}nd}d}|��sR|��dkr,	|��S|dur:|j|kr:	|��S|j|krN|��}|durI|S|jd}|��r!|��S)NrrN)rirhr�r��_read_to_bufferr`r�)r"Ztarget_bytesZ
next_find_posrLr%r%r&�_read_to_buffer_loop�s,
283  
284  �
285  �
286  �z!BaseIOStream._read_to_buffer_loopc
287  Cs�z|��}Wn/ty�tjy�ty5}zt�d|�|j|d�WYd}~dSd}~ww|durA|�|�dSdS)Nzerror on read: %sr�)	r�r/r�r�r�rr�r�r�)r"rLr�r%r%r&r�
288  s���zBaseIOStream._handle_readcCs2|jdur|��|jdusJd��t�|_|jS)NzAlready reading)rlr�rr9r%r%r&r�s
289  
290  zBaseIOStream._start_readrEcCsn|jr|jpt�|_d|_d|_t|j�|_d|_|}n|�|�}|jdur1|j}d|_t	||�|�
291  �dS)NrF)rarbrDr^r_r?r`�_consumerlrr�)r"rE�resultr�r%r%r&r�/s
292  
293  
294  zBaseIOStream._finish_readcCsb|��}|dur|�|�dS|��|��}|dur"|�|�dS|��s/|�tjj�dSdS)aAttempt to complete the current read operation from buffered data.
295  
296          If the read can be completed without blocking, schedules the
297          read callback on the next IOLoop iteration; otherwise starts
298          listening for reads on the socket.
299          N)	r�r�r�r�r�r�rrYr��r"rLr%r%r&r�?s
300  
301  �zBaseIOStream._try_inline_readc
302  Cszv	z|jrt|j�|jd�}nt|j�}|�|�}Wn+tjt	t
303  fyG}z|�|�r<|j|d�WYd}~W~dS|j|d��d}~ww	|durQW~dS|dkr]|��W~dS|jsm|jt|�d|�7_|j|7_W~n~w|j|j
kr�t�d�|��td��|S)aReads from the socket and appends the result to the read buffer.
304  
305          Returns the number of bytes read.  Returns 0 if there is nothing
306          to read (i.e. the read returns EWOULDBLOCK or equivalent).  On
307          error closes the socket and raises an exception.
308          TNr�rz Reached maximum read buffer size)rarBr^r`rDrWr~�socketr]�IOError�OSError�
_is_connresetr�rVrr1)r"r|�
309  bytes_readr�r%r%r&r�UsF�
310  
311  ���
312  
313  ��
314  zBaseIOStream._read_to_bufferrLcCs&d|_|_|_d|_|�|�dS)z�Attempts to complete the currently-pending read from the buffer.
315  
316          The argument is either a position in the read buffer or None,
317          as returned by _find_read_pos.
318          NF)rirfrgrjr�r�r%r%r&r��szBaseIOStream._read_from_buffercCs|jdur|j|jks|jr|jdkrt|j|j�}|S|jdurR|jrP|j�|j|j�}|dkrH||j8}t|j�}|�	|j||�||S|�	|j|j�dS|j
319  dur|jr|j
320  �|j|j�}|durw|��|j}|�	|j
321  |�|S|�	|j
322  |j�dS)z�Attempts to find a position in the read buffer that satisfies
323          the currently-pending read.
324  
325          Returns a position in the buffer if the current read can be satisfied,
326          or None if it cannot.
327          Nrr>)
rir`rjr\rfr^�findr_r?�_check_max_bytesrg�searchr�)r"r��locZ
delimiter_len�mr%r%r&r��s8
328  �
329  
330  	�
331  
332  	
333  �zBaseIOStream._find_read_poscCs.|jdur||jkrtd||jf��dSdS)Nz&delimiter %r not found within %d bytes)rhr/)r"r�rEr%r%r&r��s���zBaseIOStream._check_max_bytesc
334  Cs	t|j�}|s	n]|dksJ�z"trd}|�|j�|��}|dkr#WnC|j�|�|j|7_Wn3ty:Yn,tj	t
335  tfyd}z|�|�sSt
�d|��|�|j|d�WYd}~dSd}~wwq|jr�|jd\}}||jkrwdS|j��t|d�|jsidSdS)NTrizWrite error on %s: %sr�)r?rc�_WINDOWSr{rMrQre�BlockingIOErrorr�r]r�r�r�rr�rwr�rmrOr)r"rEr�r��indexr�r%r%r&r��s<
336  
337  ���
338  
339  
340  �zBaseIOStream._handle_writer�cCsx|dkrdS||jks
J�t|j�|j|j|���}|j|7_|j|8_|j|jkr:|jd|j�=d|_|S)NrrH)r`rBr^r_�tobytes)r"r�rGr%r%r&r��s��zBaseIOStream._consumecCs|��r
341  t|jd��dS)Nr�)r�rr]r9r%r%r&r��s�zBaseIOStream._check_closedcCsV|jdus|jtjjkr%|��s#|jdkr'|jdur)|�tjj�dSdSdSdSdSr3)	rrrrYr�r�r`rnr�r�r9r%r%r&r��s�
342  
343  ��z&BaseIOStream._maybe_add_error_listenerr�cCsr|��rdS|jdur tjj|B|_|j�|��|j|j�dS|j|@s7|j|B|_|j�	|��|j�dSdS)aAdds `state` (IOLoop.{READ,WRITE} flags) to our event handler.
344  
345          Implementation notes: Reads and writes have a fast path and a
346          slow path.  The fast path reads synchronously from socket
347          buffers, while the slow path uses `_add_io_state` to schedule
348          an IOLoop callback.
349  
350          To detect closed connections, we must have called
351          `_add_io_state` at some point, but we want to delay this as
352          much as possible so we don't have to set an `IOLoop.ERROR`
353          listener that will be overwritten by the next slow-path
354          operation. If a sequence of fast-path ops do not end in a
355          slow-path op, (e.g. for an @asynchronous long-poll request),
356          we must add the error handler.
357  
358          TODO: reevaluate this now that callbacks are gone.
359  
360          N)
361  r�rrrrYr�r[�add_handlerrwr�r�)r"r�r%r%r&r�s
362  
363  �zBaseIOStream._add_io_state�exccCst|tjtf�o
t|�tvS)zfReturn ``True`` if exc is ECONNRESET or equivalent.
364  
365          May be overridden in subclasses.
366          )rAr�r]r�r	�_ERRNO_CONNRESET)r"r�r%r%r&r�)s
367  �zBaseIOStream._is_connreset)NNNrRr')F)9r(r)r*r+rrSr!r
368  r�_SelectablerwryrBr{rDr~r�r�rTrr�r�r�r�r�r�r�r
r�r,rrr�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�rr�r�r�r�r�r�r�r%r%r%r&rU�s�����
369  �:
370  ���
371  �(���
372  �#1*������
373374  0'
375   5
376  )
377  .
378  *
379  &
380  
381  rUc
382  seZdZdZdejdededdf�fdd�Zdeee	j
383  ffd	d
384  �Zd%dd�Zde
efd
d�Zdeeefde
efdd�Zdedefdd�Z	d&dedede
eddfdd�Z		d'dede
eeeefejfde
ededfdd�Zd%d d!�Zd"eddfd#d$�Z�ZS)(ra�Socket-based `IOStream` implementation.
385  
386      This class supports the read and write methods from `BaseIOStream`
387      plus a `connect` method.
388  
389      The ``socket`` parameter may either be connected or unconnected.
390      For server operations the socket is the result of calling
391      `socket.accept <socket.socket.accept>`.  For client operations the
392      socket is created with `socket.socket`, and may either be
393      connected before passing it to the `IOStream` or connected with
394      `IOStream.connect`.
395  
396      A very simple (and broken) HTTP client using this class:
397  
398      .. testcode::
399  
400          import tornado.ioloop
401          import tornado.iostream
402          import socket
403  
404          async def main():
405              s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
406              stream = tornado.iostream.IOStream(s)
407              await stream.connect(("friendfeed.com", 80))
408              await stream.write(b"GET / HTTP/1.0\r\nHost: friendfeed.com\r\n\r\n")
409              header_data = await stream.read_until(b"\r\n\r\n")
410              headers = {}
411              for line in header_data.split(b"\r\n"):
412                  parts = line.split(b":")
413                  if len(parts) == 2:
414                      headers[parts[0].strip()] = parts[1].strip()
415              body_data = await stream.read_bytes(int(headers[b"Content-Length"]))
416              print(body_data)
417              stream.close()
418  
419          if __name__ == '__main__':
420              asyncio.run(main())
421  
422      .. testoutput::
423         :hide:
424  
425      r��args�kwargsrNcs(||_|j�d�t�j|i|��dS)NF)r��setblockingr r!)r"r�r�r�r#r%r&r!`szIOStream.__init__cCr:r')r�r9r%r%r&rwer<zIOStream.filenocCs|j��d|_dSr')r�r�r9r%r%r&ryhs
426  
427  zIOStream.close_fdcCs$|j�tjtj�}t�|t�|��Sr')r��
428  getsockopt�
429  SOL_SOCKET�SO_ERRORr]�os�strerror)r"�errnor%r%r&r�lszIOStream.get_fd_errorr|cCs8zz|j�|t|��WW~StyYW~dSw~wr')r��	recv_intor?r�r}r%r%r&r~ps��zIOStream.read_from_fdr=cCsz|j�|�W~S~wr')r��sendrzr%r%r&r{xszIOStream.write_to_fdr"�address�server_hostnamezFuture[_IOStreamType]c
430  Cs�d|_t�}t�d|�|_z|j�|�Wn1tyYn)tjyF}z|dur4t	�
431  d|j��|�|j|d�|WYd}~Sd}~ww|�
|jj�|S)a�Connects the socket to a remote address without blocking.
432  
433          May only be called if the socket passed to the constructor was
434          not previously connected.  The address parameter is in the
435          same format as for `socket.connect <socket.socket.connect>` for
436          the type of socket passed to the IOStream constructor,
437          e.g. an ``(ip, port)`` tuple.  Hostnames are accepted here,
438          but will be resolved synchronously and block the IOLoop.
439          If you have a hostname instead of an IP address, the `.TCPClient`
440          class is recommended instead of calling this method directly.
441          `.TCPClient` will do asynchronous DNS resolution and handle
442          both IPv4 and IPv6.
443  
444          If ``callback`` is specified, it will be called with no
445          arguments when the connection is completed; if not this method
446          returns a `.Future` (whose result after a successful
447          connection will be the stream itself).
448  
449          In SSL mode, the ``server_hostname`` parameter will be used
450          for certificate validation (unless disabled in the
451          ``ssl_options``) and SNI (if supported; requires Python
452          2.7.9+).
453  
454          Note that it is safe to call `IOStream.write
455          <BaseIOStream.write>` while the connection is pending, in
456          which case the data will be written as soon as the connection
457          is ready.  Calling `IOStream` read methods before the socket is
458          connected works on some platforms but is non-portable.
459  
460          .. versionchanged:: 4.0
461              If no callback is given, returns a `.Future`.
462  
463          .. versionchanged:: 4.2
464             SSL certificates are validated by default; pass
465             ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
466             suitably-configured `ssl.SSLContext` to the
467             `SSLIOStream` constructor to disable.
468  
469          .. versionchanged:: 6.0
470  
471             The ``callback`` argument was removed. Use the returned
472             `.Future` instead.
473  
474          TzFuture[IOStream]N�Connect error on fd %s: %sr�)rqrrJrKror��connectr�r]rr�rwr�r�r[r�)r"r�r�r�r�r%r%r&r��s /��	zIOStream.connect�server_side�ssl_options�SSLIOStreamcCs�|js|js|js|js|js|jrtd��|dur!|rt}nt}|j	}|j
475  �|�d|_	t||||dd�}|j
}d|_
t�}t||d�}|�|�||_|j|_|j|_|S)a�Convert this `IOStream` to an `SSLIOStream`.
476  
477          This enables protocols that begin in clear-text mode and
478          switch to SSL after some initial negotiation (such as the
479          ``STARTTLS`` extension to SMTP and IMAP).
480  
481          This method cannot be used if there are outstanding reads
482          or writes on the stream, or if there is any data in the
483          IOStream's buffer (data in the operating system's socket
484          buffer is allowed).  This means it must generally be used
485          immediately after reading or writing the last clear-text
486          data.  It can also be used immediately after connecting,
487          before any reads or writes.
488  
489          The ``ssl_options`` argument may be either an `ssl.SSLContext`
490          object or a dictionary of keyword arguments for the
491          `ssl.wrap_socket` function.  The ``server_hostname`` argument
492          will be used for certificate validation unless disabled
493          in the ``ssl_options``.
494  
495          This method returns a `.Future` whose result is the new
496          `SSLIOStream`.  After this method has been called,
497          any other operation on the original stream is undefined.
498  
499          If a close callback is defined on this stream, it will be
500          transferred to the new stream.
501  
502          .. versionadded:: 4.0
503  
504          .. versionchanged:: 4.2
505             SSL certificates are validated by default; pass
506             ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
507             suitably-configured `ssl.SSLContext` to disable.
508          z+IOStream is not idle; cannot convert to SSLNF)r�r��do_handshake_on_connect)r�)rlrmrorsr^rc�
509  ValueErrorrrr�r[r�rrnrr�r�rprVrW)r"r�r�r�r�Zorig_close_callbackr�Z
510  ssl_streamr%r%r&�	start_tls�sH)�������
511  zIOStream.start_tlsc
512  Cs�z|j�tjtj�}Wntjy'}zt|�tjkrd}WYd}~nd}~ww|dkrNt�|t�	|��|_|j
513  durHt�d|j�
�tj|�|��dS|j
514  dur^|j
515  }d|_
516  t||�d|_dS)Nrr�F)r�r�r�r�r]r	r��ENOPROTOOPTr�r�rorr�rw�	errorcoder�rrq)r"�errr�r�r%r%r&r�s,��
517  �
518  
519  
520  zIOStream._handle_connectr�c
521  Cs�|jdurH|jjtjtjfvrJz|j�tjtj|rdnd�WdStjyG}z|jtj	kr5|�
522  |�s<�WYd}~dSWYd}~dSd}~wwdSdS)Nr�r)r��family�AF_INET�AF_INET6�
523  setsockopt�IPPROTO_TCP�TCP_NODELAYr]r��EINVALr�)r"r�r�r%r%r&r�,s�
524  �����zIOStream.set_nodelayrRr')NN) r(r)r*r+r�rr!r
525  rSrr�rwryrr�r�rDrBr~r{r�strr�r�r�ssl�
526  SSLContextrr�r�r�r-r%r%r#r&r4s> +
527  	����
528  �G����
529530  LcseZdZdZdZdededdf�fdd�Zdef�fdd	�Zdef�fd
531  d�Z	d)dd
�Z
532  d)dd�Zdedefdd�Zd)�fdd�Z
d)�fdd�Z	d*dedeeddf�fdd�
Zd)�fdd�Zd+dd�Zd edefd!d"�Zd#eeefdeefd$d%�Zd&edef�fd'd(�Z�ZS),r�anA utility class to write to and read from a non-blocking SSL socket.
533  
534      If the socket passed to the constructor is already connected,
535      it should be wrapped with::
536  
537          ssl.wrap_socket(sock, do_handshake_on_connect=False, **kwargs)
538  
539      before constructing the `SSLIOStream`.  Unconnected sockets will be
540      wrapped when `IOStream.connect` is finished.
541      Nr�r�rcsp|�dt�|_t�j|i|��d|_d|_d|_d|_z|j	�
542  �Wnt	jy.YdSw|�|j
j�dS)z�The ``ssl_options`` keyword argument may either be an
543          `ssl.SSLContext` object or a dictionary of keywords arguments
544          for `ssl.wrap_socket`
545          r�TFN)�popr�_ssl_optionsr r!�_ssl_accepting�_handshake_reading�_handshake_writing�_server_hostnamer��getpeernamer]r�r[r�)r"r�r�r#r%r&r!Ks�zSSLIOStream.__init__c�|jpt���Sr')rr r�r9r#r%r&r�b�zSSLIOStream.readingcrr')rr r�r9r#r%r&r�erzSSLIOStream.writingcCs�z
d|_d|_|j��Wn�tjy�}zn|jdtjkr(d|_WYd}~dS|jdtjkr:d|_WYd}~dS|jdtj	tj
546  fvrQ|j|d�WYd}~S|jdtjkr�z|j�
�}Wntykd}Ynwt�d|j��||�|j|d�WYd}~S�d}~wtjy�}z|j|d�WYd}~Sd}~wtjy�}z|�|�s�|jddtjtjfvr�|j|d�WYd}~S�d}~wty�}z|j|d�WYd}~Sd}~wwd|_|�|j���s�|��dS|��dS)NFrTr�z(not connected)zSSL Error on %s %s: %s)rrr��do_handshaker
547  �SSLErrorr��SSL_ERROR_WANT_READ�SSL_ERROR_WANT_WRITE�
SSL_ERROR_EOF�SSL_ERROR_ZERO_RETURNr��
SSL_ERROR_SSLrr�rr�rw�CertificateErrorr]r�r��EBADF�ENOTCONN�AttributeErrorr�_verify_cert�getpeercert�_finish_ssl_connect)r"r�peerr%r%r&�_do_ssl_handshakehs\��������zSSLIOStream._do_ssl_handshakecCs(|jdur|j}d|_t||�dSdSr')rprr�r%r%r&r"�s
548  
549  �zSSLIOStream._finish_ssl_connect�peercertc
550  Cs�t|jt�r|j�dtj�}nt|jtj�r|jj}|tjtjtj	fvs&J�|tjks0|j
551  dur2dS|j��}|durG|tjkrGt
�d�dSz
552  t�||j
553  �WdStjyl}zt
�d|�WYd}~dSd}~ww)a
554  Returns ``True`` if peercert is valid according to the configured
555          validation mode and hostname.
556  
557          The ssl handshake already tested the certificate for a valid
558          CA signature; the only thing that remains is to check
559          the hostname.
560          �	cert_reqsNTzNo SSL certificate givenFzInvalid SSL certificate: %s)rAr
�dict�getr
561  �	CERT_NONEr�verify_mode�
CERT_REQUIRED�
CERT_OPTIONALrr�r!rr��match_hostnamer)r"r%r*�certr�r%r%r&r �s&
562  
563  ���zSSLIOStream._verify_certc� |jr	|��dSt���dSr')rr$r r�r9r#r%r&r���zSSLIOStream._handle_readcr/r')rr$r r�r9r#r%r&r��r0zSSLIOStream._handle_writer�r��Future[SSLIOStream]cs(||_t��|�}|�dd��|��S)NcSr�r'r�r�r%r%r&r��r�z%SSLIOStream.connect.<locals>.<lambda>)rr r�r��wait_for_handshake)r"r�r��futr#r%r&r��szSSLIOStream.connectcsdt���|��rdS|j�|j�|j}|dusJ�d|_t|j|j|j	ddd�|_|�
564  |�dS)NF)r�r�r�)r r�r�r[r�r�rrrr
rr�)r"�	old_stater#r%r&r��s
565  �zSSLIOStream._handle_connectcCs0|jdur	td��t�}|_|js|��|S)avWait for the initial SSL handshake to complete.
566  
567          If a ``callback`` is given, it will be called with no
568          arguments once the handshake is complete; otherwise this
569          method returns a `.Future` which will resolve to the
570          stream itself after the handshake is complete.
571  
572          Once the handshake is complete, information such as
573          the peer's certificate and NPN/ALPN selections may be
574          accessed on ``self.socket``.
575  
576          This method is intended for use on server-side streams
577          or after using `IOStream.start_tls`; it should not be used
578          with `IOStream.connect` (which already waits for the
579          handshake to complete). It may only be called once per stream.
580  
581          .. versionadded:: 4.2
582  
583          .. versionchanged:: 6.0
584  
585             The ``callback`` argument was removed. Use the returned
586             `.Future` instead.
587  
588          NzAlready waiting)rp�RuntimeErrorrrr"r�r%r%r&r2�s
589  zSSLIOStream.wait_for_handshaker=c
590  Cstt|�d?rt|�dd�}z)z	|j�|�WW~Stjy7}z|jdtjkr2WYd}~W~dS�d}~ww~w�N�i@r)r?rBr�r�r
591  rr�r)r"r=r�r%r%r&r{s����
zSSLIOStream.write_to_fdr|c
592  Cs�zL|jrW~dSt|�d?rt|�dd�}z|j�|t|��WW~StjyA}z|jdtjkr<WYd}~W~dS�d}~wt	yLYW~dSw~wr6)
593  rr?rBr�r�r
594  rr�rr��r"r|r�r%r%r&r~4s(�����zSSLIOStream.read_from_fdr�cs,t|tj�r|jdtjkrdSt��|�S)NrT)rAr
595  rr�rr r�)r"r�r#r%r&r�NszSSLIOStream._is_connresetrRr')rr1)r(r)r*r+r�rr!r�r�r�r$r"r r�r�rrr	r�r�r2rBrSr{r
596  rDr~r,r�r-r%r%r#r&r�=s0
597  
598  9����
599   r�cszeZdZdZdedededdf�fdd�Zdefd	d
600  �Zddd�Zd
e	defdd�Z
601  deee	fde
efdd�Z�ZS)�PipeIOStreamaUPipe-based `IOStream` implementation.
602  
603      The constructor takes an integer file descriptor (such as one returned
604      by `os.pipe`) rather than an open file object.  Pipes are generally
605      one-way, so a `PipeIOStream` can be used for reading or writing but not
606      both.
607  
608      ``PipeIOStream`` is only available on Unix-based platforms.
609      r�r�r�rNcsJ||_t�|jd�|_tjdkrtd��t�|d�t	�j
610  |i|��dS)Nzr+�win32z(PipeIOStream is not supported on WindowsF)r��io�FileIO�_fior��platform�AssertionErrorr��set_blockingr r!)r"r�r�r�r#r%r&r!_s
611  zPipeIOStream.__init__cCr:r')r�r9r%r%r&rwlr<zPipeIOStream.filenocCs|j��dSr')r=r�r9r%r%r&ryoszPipeIOStream.close_fdr=cCsz	t�|j|�W~S~wr')r�r�r�rzr%r%r&r{rszPipeIOStream.write_to_fdr|c
612  Csdz/z	|j�|�WW~Sttfy/}zt|�tjkr*|j|d�WYd}~W~dS�d}~ww~w)Nr�)r=�readintor�r�r	r�rr�r8r%r%r&r~zs
613  ����	zPipeIOStream.read_from_fdrR)r(r)r*r+rSrr!rwryrBr{r
614  rDrr~r-r%r%r#r&r9Ts
615  
616  &r9rcCsddl}|��Sr3)�doctest�DocTestSuite)rBr%r%r&�doctests�srD)Br+r�r4r�r;r�r�r�r
617  r�r��tornado.concurrentrr�tornador�tornado.logr�tornado.netutilrrr�tornado.utilr	rJr
618  rrr
rrrrr�typesr�
TYPE_CHECKINGrrrr�
619  ECONNRESET�ECONNABORTED�EPIPE�	ETIMEDOUTr��hasattrrZWSAECONNABORTEDZWSAETIMEDOUTr>�
620  EPROTOTYPE�
621  startswithr�r�rr�r/r1�objectr2rUrr�r9rDr%r%r%r&�<module>sf
622  ,
623  �
624  
625  \i5