U
    &%eC/                     @  s  d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZmZmZ ddlmZmZmZmZ ddlmZmZ dd	lmZmZmZmZ ed
Zeeeef df Zeedf ZG dd deZ eddG dd deZ!eddG dd dee! Z"dS )    )annotationsN)	dataclass)wraps)AnyCallableMappingTupleTypeVar   )BrokenResourceErrorEndOfStreamaclose_forcefullyget_cancelled_exc_class)TypedAttributeSettyped_attribute)AnyByteStream
ByteStreamListener	TaskGroupT_Retval.c                   @  s   e Zd ZU dZe Zded< e Zded< e Zded< e Z	ded	< e Z
d
ed< e Zded< e Zded< e Zded< e Zded< e Zded< dS )TLSAttributez5Contains Transport Layer Security related attributes.
str | Nonealpn_protocolbyteschannel_binding_tls_uniqueztuple[str, str, int]cipherz*dict[str, str | _PCTRTTT | _PCTRTT] | Nonepeer_certificatezbytes | Nonepeer_certificate_binaryboolserver_sidez!list[tuple[str, str, int]] | Noneshared_ciphersssl.SSLObject
ssl_objectstandard_compatiblestrtls_versionN)__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   r   r    r"   r#   r%    r+   r+   P/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/anyio/streams/tls.pyr      s   
r   F)eqc                	   @  s   e Zd ZU dZded< ded< ded< ded	< ded
< eddddddddddd dddZddddddZddddZdddd Z	d/d"d#d$d%d&Z
d#dd'd(d)Zddd*d+Zed,dd-d.ZdS )0	TLSStreama  
    A stream wrapper that encrypts all sent data and decrypts received data.

    This class has no public initializer; use :meth:`wrap` instead.
    All extra attributes from :class:`~TLSAttribute` are supported.

    :var AnyByteStream transport_stream: the wrapped stream

    r   transport_streamr   r#   r!   _ssl_objectzssl.MemoryBIO	_read_bio
_write_bioNT)r   hostnamessl_contextr#   zbool | Noner   zssl.SSLContext | None)r/   r   r3   r4   r#   returnc                  s   |dkr| }|sL|rt jjnt jj}t |}tt drL| jt j M  _t  }t  }|j	||||d}	| |||	||d}
|

|	jI dH  |
S )a  
        Wrap an existing stream with Transport Layer Security.

        This performs a TLS handshake with the peer.

        :param transport_stream: a bytes-transporting stream to wrap
        :param server_side: ``True`` if this is the server side of the connection,
            ``False`` if this is the client side (if omitted, will be set to ``False``
            if ``hostname`` has been provided, ``False`` otherwise). Used only to create
            a default context when an explicit context has not been provided.
        :param hostname: host name of the peer (if host name checking is desired)
        :param ssl_context: the SSLContext object to use (if not provided, a secure
            default will be created)
        :param standard_compatible: if ``False``, skip the closing handshake when closing the
            connection, and don't raise an exception if the peer does the same
        :raises ~ssl.SSLError: if the TLS handshake fails

        NOP_IGNORE_UNEXPECTED_EOF)r   server_hostname)r/   r#   r0   r1   r2   )sslPurposeCLIENT_AUTHSERVER_AUTHcreate_default_contexthasattroptionsr6   	MemoryBIOwrap_bio_call_sslobject_methoddo_handshake)clsr/   r   r3   r4   r#   purposeZbio_inZbio_outr"   wrapperr+   r+   r,   wrapF   s2    

   zTLSStream.wrapzCallable[..., T_Retval]objectr   )funcargsr5   c                   s  z|| }W n t jk
r   z4| jjrB| j| j I d H  | j I d H }W nX tk
rr   | j	
  Y nH tk
r } z| j	
  | j
  t|W 5 d }~X Y nX | j	| Y q  t jk
r   | j| j I d H  Y q  t jk
r( } z| j	
  | j
  t|W 5 d }~X Y q  t jk
r } zJ| j	
  | j
  t|t jsjd|jkr| jrzt|ntd  W 5 d }~X Y q X | jjr| j| j I d H  |S q d S )NZUNEXPECTED_EOF_WHILE_READING)r8   SSLWantReadErrorr2   pendingr/   sendreadreceiver   r1   	write_eofOSErrorr   writeSSLWantWriteErrorSSLSyscallErrorSSLError
isinstanceSSLEOFErrorstrerrorr#   )selfrH   rI   resultdataexcr+   r+   r,   rA   ~   sD    







z TLSStream._call_sslobject_methodztuple[AnyByteStream, bytes]r5   c                   s8   |  | jjI dH  | j  | j  | j| j fS )z
        Does the TLS closing handshake.

        :return: a tuple of (wrapped byte stream, bytes left in the read buffer)

        N)rA   r0   unwrapr1   rO   r2   r/   rM   rX   r+   r+   r,   r]      s    

zTLSStream.unwrapNonec                   sT   | j r@z|  I d H  W n& tk
r>   t| jI d H   Y nX | j I d H  d S N)r#   r]   BaseExceptionr   r/   acloser^   r+   r+   r,   rb      s    zTLSStream.aclose   intr   )	max_bytesr5   c                   s"   |  | jj|I d H }|st|S r`   )rA   r0   rM   r   )rX   re   rZ   r+   r+   r,   rN      s    zTLSStream.receive)itemr5   c                   s   |  | jj|I d H  d S r`   )rA   r0   rQ   )rX   rf   r+   r+   r,   rL      s    zTLSStream.sendc                   sd   |  tj}td|}|rXt|dt|dp6d }}||fdk rXtd| tdd S )NzTLSv(\d+)(?:\.(\d+))?   r
   r   )rg      z;send_eof() requires at least TLSv1.3; current session uses z7send_eof() has not yet been implemented for TLS streams)extrar   r%   rematchrd   groupNotImplementedError)rX   r%   rk   majorminorr+   r+   r,   send_eof   s    "zTLSStream.send_eofMapping[Any, Callable[[], Any]]c                   s    j jtj jjtj jjtj jjtj	 fddtj
 fddtj fddtj fddtj fddtj fddtj jji
S )Nc                     s    j dS )NFr0   getpeercertr+   r^   r+   r,   <lambda>       z,TLSStream.extra_attributes.<locals>.<lambda>c                     s    j dS )NTrr   r+   r^   r+   r,   rt      s   c                     s    j jS r`   )r0   r   r+   r^   r+   r,   rt      ru   c                     s    j jr j  S d S r`   )r0   r   r    r+   r^   r+   r,   rt      s    c                     s    j S r`   r#   r+   r^   r+   r,   rt      ru   c                     s    j S r`   )r0   r+   r^   r+   r,   rt      ru   )r/   extra_attributesr   r   r0   selected_alpn_protocolr   get_channel_bindingr   r   r   r   r    r#   r"   r%   versionr^   r+   r^   r,   rw      s,        
 
 
 
 
 
 zTLSStream.extra_attributes)rc   )r&   r'   r(   r)   r*   classmethodrF   rA   r]   rb   rN   rL   rp   propertyrw   r+   r+   r+   r,   r.   4   s(   

7/
r.   c                   @  s   e Zd ZU dZded< ded< dZded< d	Zd
ed< eddddddZdddddddZ	ddddZ
eddddZdS )TLSListenera  
    A convenience listener that wraps another listener and auto-negotiates a TLS session on every
    accepted connection.

    If the TLS handshake times out or raises an exception, :meth:`handle_handshake_error` is
    called to do whatever post-mortem processing is deemed necessary.

    Supports only the :attr:`~TLSAttribute.standard_compatible` extra attribute.

    :param Listener listener: the listener to wrap
    :param ssl_context: the SSL context object
    :param standard_compatible: a flag passed through to :meth:`TLSStream.wrap`
    :param handshake_timeout: time limit for the TLS handshake
        (passed to :func:`~anyio.fail_after`)
    zListener[Any]listenerzssl.SSLContextr4   Tr   r#      floathandshake_timeoutra   r   r_   )r[   streamr5   c                   sF   t |I dH  t| t s*ttd t| tr@t| t rB dS )a  
        Handle an exception raised during the TLS handshake.

        This method does 3 things:

        #. Forcefully closes the original stream
        #. Logs the exception (unless it was a cancellation exception) using the
           ``anyio.streams.tls`` logger
        #. Reraises the exception if it was a base exception or a cancellation exception

        :param exc: the exception
        :param stream: the original stream

        NzError during TLS handshake)r   rU   r   logging	getLoggerr&   	exception	Exception)r[   r   r+   r+   r,   handle_handshake_error  s
    z"TLSListener.handle_handshake_errorNzCallable[[TLSStream], Any]zTaskGroup | None)handler
task_groupr5   c                   s6   t  ddd fdd}j||I d H  d S )Nr   r_   )r   r5   c              
     s   ddl m} z4|j  tj| jjdI d H }W 5 Q R X W n4 tk
rt } z|| I d H  W 5 d }~X Y nX  |I d H  d S )Nr
   )
fail_after)r4   r#   )	 r   r   r.   rF   r4   r#   ra   r   )r   r   Zwrapped_streamr[   r   rX   r+   r,   handler_wrapper'  s    $z*TLSListener.serve.<locals>.handler_wrapper)r   r~   serve)rX   r   r   r   r+   r   r,   r   "  s    zTLSListener.server\   c                   s   | j  I d H  d S r`   )r~   rb   r^   r+   r+   r,   rb   9  s    zTLSListener.acloserq   c                   s   t j fddiS )Nc                     s    j S r`   rv   r+   r^   r+   r,   rt   ?  ru   z.TLSListener.extra_attributes.<locals>.<lambda>)r   r#   r^   r+   r^   r,   rw   <  s     
zTLSListener.extra_attributes)N)r&   r'   r(   r)   r*   r#   r   staticmethodr   r   rb   r|   rw   r+   r+   r+   r,   r}      s   
 r}   )#
__future__r   r   rj   r8   dataclassesr   	functoolsr   typingr   r   r   r   r	   r   r   r   r   r   Z_core._typedattrr   r   abcr   r   r   r   r   r$   Z_PCTRTTZ_PCTRTTTr   r.   r}   r+   r+   r+   r,   <module>   s$    =