o
    _cA'                     @  s  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 g dZ
G dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+eZG d,d- d-eZG d.d/ d/eZG d0d1 d1ee Z!G d2d3 d3eZ"G d4d5 d5eZ#G d6d7 d7eZ$e$Z%dS )8ay  
:mod:`websockets.exceptions` defines the following exception hierarchy:

* :exc:`WebSocketException`
    * :exc:`ConnectionClosed`
        * :exc:`ConnectionClosedError`
        * :exc:`ConnectionClosedOK`
    * :exc:`InvalidHandshake`
        * :exc:`SecurityError`
        * :exc:`InvalidMessage`
        * :exc:`InvalidHeader`
            * :exc:`InvalidHeaderFormat`
            * :exc:`InvalidHeaderValue`
            * :exc:`InvalidOrigin`
            * :exc:`InvalidUpgrade`
        * :exc:`InvalidStatus`
        * :exc:`InvalidStatusCode` (legacy)
        * :exc:`NegotiationError`
            * :exc:`DuplicateParameter`
            * :exc:`InvalidParameterName`
            * :exc:`InvalidParameterValue`
        * :exc:`AbortHandshake`
        * :exc:`RedirectHandshake`
    * :exc:`InvalidState`
    * :exc:`InvalidURI`
    * :exc:`PayloadTooBig`
    * :exc:`ProtocolError`

    )annotationsN)Optional   )datastructuresframeshttp11)WebSocketExceptionConnectionClosedConnectionClosedErrorConnectionClosedOKInvalidHandshakeSecurityErrorInvalidMessageInvalidHeaderInvalidHeaderFormatInvalidHeaderValueInvalidOriginInvalidUpgradeInvalidStatusInvalidStatusCodeNegotiationErrorDuplicateParameterInvalidParameterNameInvalidParameterValueAbortHandshakeRedirectHandshakeInvalidState
InvalidURIPayloadTooBigProtocolErrorWebSocketProtocolErrorc                   @     e Zd ZdZdS )r   z?
    Base class for all exceptions defined by websockets.

    N__name__
__module____qualname____doc__ r'   r'   @/usr/local/lib/python3.10/dist-packages/websockets/exceptions.pyr   D       r   c                   @  sD   e Zd ZdZ	ddd
dZdddZedddZedddZdS )r	   aG  
    Raised when trying to interact with a closed connection.

    Attributes:
        rcvd (Optional[Close]): if a close frame was received, its code and
            reason are available in ``rcvd.code`` and ``rcvd.reason``.
        sent (Optional[Close]): if a close frame was sent, its code and reason
            are available in ``sent.code`` and ``sent.reason``.
        rcvd_then_sent (Optional[bool]): if close frames were received and
            sent, this attribute tells in which order this happened, from the
            perspective of this side of the connection.

    NrcvdOptional[frames.Close]sentrcvd_then_sentOptional[bool]returnNonec                 C  s   || _ || _|| _d S Nr*   r,   r-   )selfr*   r,   r-   r'   r'   r(   __init__Z   s   
zConnectionClosed.__init__strc                 C  s   | j d u r!| jd u r| jd u sJ dS | jd u sJ d| j dS | jd u r4| jd u s-J d| j  dS | jd us;J | jrHd| j  d| j S d| j d| j  S )Nzno close frame received or sentzsent z; no close frame receivedz	received z; no close frame sentz; then sent z; then received r2   r3   r'   r'   r(   __str__d   s   


zConnectionClosed.__str__intc                 C     | j d u rdS | j jS )Ni  )r*   coder6   r'   r'   r(   r:   y      zConnectionClosed.codec                 C  r9   )N )r*   reasonr6   r'   r'   r(   r=   }   r;   zConnectionClosed.reasonr1   )r*   r+   r,   r+   r-   r.   r/   r0   r/   r5   )r/   r8   )	r#   r$   r%   r&   r4   r7   propertyr:   r=   r'   r'   r'   r(   r	   K   s    

r	   c                   @  r!   )r
   z
    Like :exc:`ConnectionClosed`, when the connection terminated with an error.

    A close code other than 1000 (OK) or 1001 (going away) was received or
    sent, or the closing handshake didn't complete properly.

    Nr"   r'   r'   r'   r(   r
      r)   r
   c                   @  r!   )r   z
    Like :exc:`ConnectionClosed`, when the connection terminated properly.

    A close code 1000 (OK) or 1001 (going away) was received and sent.

    Nr"   r'   r'   r'   r(   r      r)   r   c                   @  r!   )r   zK
    Raised during the handshake when the WebSocket connection fails.

    Nr"   r'   r'   r'   r(   r      r)   r   c                   @  r!   )r   zs
    Raised when a handshake request or response breaks a security rule.

    Security limits are hard coded.

    Nr"   r'   r'   r'   r(   r      r)   r   c                   @  r!   )r   zD
    Raised when a handshake request or response is malformed.

    Nr"   r'   r'   r'   r(   r      r)   r   c                   @  s&   e Zd ZdZddd	d
ZdddZdS )r   zJ
    Raised when a HTTP header doesn't have a valid format or value.

    Nnamer5   valueOptional[str]r/   r0   c                 C     || _ || _d S r1   r@   rA   r3   r@   rA   r'   r'   r(   r4         
zInvalidHeader.__init__c                 C  sD   | j d u rd| j dS | j dkrd| j dS d| j d| j  S )Nzmissing z headerr<   zempty zinvalid z	 header: rA   r@   r6   r'   r'   r(   r7      s
   

zInvalidHeader.__str__r1   r@   r5   rA   rB   r/   r0   r>   r#   r$   r%   r&   r4   r7   r'   r'   r'   r(   r      s    r   c                      s"   e Zd ZdZd fd
dZ  ZS )r   z
    Raised when a HTTP header cannot be parsed.

    The format of the header doesn't match the grammar for that header.

    r@   r5   errorheaderposr8   r/   r0   c                   s"   t  || d| d|  d S )Nz at z in superr4   )r3   r@   rJ   rK   rL   	__class__r'   r(   r4      s   "zInvalidHeaderFormat.__init__)
r@   r5   rJ   r5   rK   r5   rL   r8   r/   r0   r#   r$   r%   r&   r4   __classcell__r'   r'   rO   r(   r      s    r   c                   @  r!   )r   z~
    Raised when a HTTP header has a wrong value.

    The format of the header is correct but a value isn't acceptable.

    Nr"   r'   r'   r'   r(   r      r)   r   c                      s"   e Zd ZdZd fddZ  ZS )	r   zD
    Raised when the Origin header in a request isn't allowed.

    originrB   r/   r0   c                   s   t  d| d S )NZOriginrM   )r3   rS   rO   r'   r(   r4         zInvalidOrigin.__init__)rS   rB   r/   r0   rQ   r'   r'   rO   r(   r      s    r   c                   @  r!   )r   zF
    Raised when the Upgrade or Connection header isn't correct.

    Nr"   r'   r'   r'   r(   r      r)   r   c                   @  s$   e Zd ZdZdddZdd	d
ZdS )r   zJ
    Raised when a handshake response rejects the WebSocket upgrade.

    responsehttp11.Responser/   r0   c                 C  
   || _ d S r1   )rU   )r3   rU   r'   r'   r(   r4         
zInvalidStatus.__init__r5   c                 C  s   d| j jdS )N+server rejected WebSocket connection: HTTP d)rU   status_coder6   r'   r'   r(   r7      s   zInvalidStatus.__str__N)rU   rV   r/   r0   r>   rI   r'   r'   r'   r(   r          
r   c                   @  s$   e Zd ZdZddd	ZdddZdS )r   zC
    Raised when a handshake response status code is invalid.

    r[   r8   headersdatastructures.Headersr/   r0   c                 C  rC   r1   )r[   r]   )r3   r[   r]   r'   r'   r(   r4      rF   zInvalidStatusCode.__init__r5   c                 C     d| j  S )NrY   )r[   r6   r'   r'   r(   r7         zInvalidStatusCode.__str__N)r[   r8   r]   r^   r/   r0   r>   rI   r'   r'   r'   r(   r          
r   c                   @  r!   )r   z6
    Raised when negotiating an extension fails.

    Nr"   r'   r'   r'   r(   r     r)   r   c                   @  $   e Zd ZdZdddZddd	Zd
S )r   zK
    Raised when a parameter name is repeated in an extension header.

    r@   r5   r/   r0   c                 C  rW   r1   r@   r3   r@   r'   r'   r(   r4     rX   zDuplicateParameter.__init__c                 C  r_   )Nzduplicate parameter: rc   r6   r'   r'   r(   r7     r`   zDuplicateParameter.__str__Nr@   r5   r/   r0   r>   rI   r'   r'   r'   r(   r   
  r\   r   c                   @  rb   )r   zJ
    Raised when a parameter name in an extension header is invalid.

    r@   r5   r/   r0   c                 C  rW   r1   rc   rd   r'   r'   r(   r4     rX   zInvalidParameterName.__init__c                 C  r_   )Nzinvalid parameter name: rc   r6   r'   r'   r(   r7      r`   zInvalidParameterName.__str__Nre   r>   rI   r'   r'   r'   r(   r     r\   r   c                   @  s$   e Zd ZdZddd	Zdd
dZdS )r   zK
    Raised when a parameter value in an extension header is invalid.

    r@   r5   rA   rB   r/   r0   c                 C  rC   r1   rD   rE   r'   r'   r(   r4   *  rF   zInvalidParameterValue.__init__c                 C  s@   | j d u rd| j S | j dkrd| j S d| j d| j  S )Nzmissing value for parameter r<   zempty value for parameter zinvalid value for parameter z: rG   r6   r'   r'   r(   r7   .  s
   

zInvalidParameterValue.__str__NrH   r>   rI   r'   r'   r'   r(   r   $  ra   r   c                   @  s(   e Zd ZdZ	ddddZdddZdS )r   a  
    Raised to abort the handshake on purpose and return a HTTP response.

    This exception is an implementation detail.

    The public API
    is :meth:`~websockets.server.WebSocketServerProtocol.process_request`.

    Attributes:
        status (~http.HTTPStatus): HTTP status code.
        headers (Headers): HTTP response headers.
        body (bytes): HTTP response body.
        statushttp.HTTPStatusr]   datastructures.HeadersLikebodybytesr/   r0   c                 C  s   || _ t|| _|| _d S r1   )rg   r   ZHeadersr]   rj   )r3   rg   r]   rj   r'   r'   r(   r4   F  s   
zAbortHandshake.__init__r5   c                 C  s(   d| j ddt| j dt| j dS )NzHTTP rZ   z, z
 headers, z bytes)rg   lenr]   rj   r6   r'   r'   r(   r7   P  s   zAbortHandshake.__str__N)rf   )rg   rh   r]   ri   rj   rk   r/   r0   r>   rI   r'   r'   r'   r(   r   7  s
    
r   c                   @  rb   )r   zd
    Raised when a handshake gets redirected.

    This exception is an implementation detail.

    urir5   r/   r0   c                 C  rW   r1   rm   )r3   rm   r'   r'   r(   r4   `  rX   zRedirectHandshake.__init__c                 C  r_   )Nzredirect to rn   r6   r'   r'   r(   r7   c  r`   zRedirectHandshake.__str__N)rm   r5   r/   r0   r>   rI   r'   r'   r'   r(   r   X  s    
r   c                   @  r!   )r   z
    Raised when an operation is forbidden in the current state.

    This exception is an implementation detail.

    It should never be raised in normal circumstances.

    Nr"   r'   r'   r'   r(   r   g  r)   r   c                   @  s$   e Zd ZdZdddZdd	d
ZdS )r   zM
    Raised when connecting to an URI that isn't a valid WebSocket URI.

    rm   r5   msgr/   r0   c                 C  rC   r1   rm   ro   )r3   rm   ro   r'   r'   r(   r4   x  rF   zInvalidURI.__init__c                 C  s   | j  d| j S )Nz isn't a valid URI: rp   r6   r'   r'   r(   r7   |  rT   zInvalidURI.__str__N)rm   r5   ro   r5   r/   r0   r>   rI   r'   r'   r'   r(   r   r  ra   r   c                   @  r!   )r   zS
    Raised when receiving a frame with a payload exceeding the maximum size.

    Nr"   r'   r'   r'   r(   r     r)   r   c                   @  r!   )r   z3
    Raised when a frame breaks the protocol.

    Nr"   r'   r'   r'   r(   r     r)   r   )&r&   
__future__r   httptypingr   r<   r   r   r   __all__	Exceptionr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   AssertionErrorr   r   r   r   r    r'   r'   r'   r(   <module>   s>    7
			
!