U
    gb)                     @  s  d Z ddlmZ ddlZddlZddlmZ dddd	d
dddddddddddddddddddddg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 d0d deZG d1d de	ZG d2d de	ZG d3d de	eZG d4d de	eZej rdd5l!m"Z"m#Z#m$Z$m%Z% eZ&nee' d6d6d6d6d6d7d8 dd9l(m)Z)m*Z* dS ):a  
:mod:`websockets.exceptions` defines the following hierarchy of exceptions.

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

    )annotationsN   )lazy_importWebSocketExceptionConnectionClosedConnectionClosedOKConnectionClosedError
InvalidURIInvalidHandshakeSecurityErrorInvalidMessageInvalidStatusInvalidStatusCodeInvalidHeaderInvalidHeaderFormatInvalidHeaderValueInvalidOriginInvalidUpgradeNegotiationErrorDuplicateParameterInvalidParameterNameInvalidParameterValueAbortHandshakeRedirectHandshakeProtocolErrorWebSocketProtocolErrorPayloadTooBigInvalidStateConcurrencyErrorc                   @  s   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$   9/tmp/pip-unpacked-wheel-dx_q7dq3/websockets/exceptions.pyr   F   s   c                   @  sX   e Zd ZdZddddddddZd	d
ddZedd
ddZed	d
ddZdS )r   a  
    Raised when trying to interact with a closed connection.

    Attributes:
        rcvd: If a close frame was received, its code and reason are available
            in ``rcvd.code`` and ``rcvd.reason``.
        sent: If a close frame was sent, its code and reason are available
            in ``sent.code`` and ``sent.reason``.
        rcvd_then_sent: If close frames were received and sent, this attribute
            tells in which order this happened, from the perspective of this
            side of the connection.

    Nzframes.Close | Nonezbool | NoneNone)rcvdsentrcvd_then_sentreturnc                 C  s8   || _ || _|| _| jd k| j d kp,| jd kks4td S N)r'   r(   r)   AssertionError)selfr'   r(   r)   r$   r$   r%   __init__\   s    zConnectionClosed.__init__strr*   c                 C  sr   | j d kr(| jd krdS d| j dS nF| jd kr@d| j  dS | jrZd| j  d| j S d| j d| j  S d 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 )r'   r(   r)   r-   r$   r$   r%   __str__g   s    


zConnectionClosed.__str__intc                 C  s&   t dt | jd krtjjS | jjS )NzZConnectionClosed.code is deprecated; use Protocol.close_code or ConnectionClosed.rcvd.code)warningswarnDeprecationWarningr'   framesZ	CloseCodeZABNORMAL_CLOSUREcoder1   r$   r$   r%   r8   x   s    
zConnectionClosed.codec                 C  s"   t dt | jd krdS | jjS )Nz`ConnectionClosed.reason is deprecated; use Protocol.close_reason or ConnectionClosed.rcvd.reason )r4   r5   r6   r'   reasonr1   r$   r$   r%   r:      s    
zConnectionClosed.reason)N)	r    r!   r"   r#   r.   r2   propertyr8   r:   r$   r$   r$   r%   r   M   s    
c                   @  s   e Zd ZdZdS )r   z
    Like :exc:`ConnectionClosed`, when the connection terminated properly.

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

    Nr   r$   r$   r$   r%   r      s   c                   @  s   e Zd ZdZdS )r   z
    Like :exc:`ConnectionClosed`, when the connection terminated with an error.

    A close frame with a 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      s   c                   @  s0   e Zd ZdZddddddZdddd	Zd
S )r	   zL
    Raised when connecting to a URI that isn't a valid WebSocket URI.

    r/   r&   )urimsgr*   c                 C  s   || _ || _d S r+   r<   r=   )r-   r<   r=   r$   r$   r%   r.      s    zInvalidURI.__init__r0   c                 C  s   | j  d| j S )Nz isn't a valid URI: r>   r1   r$   r$   r%   r2      s    zInvalidURI.__str__Nr    r!   r"   r#   r.   r2   r$   r$   r$   r%   r	      s   c                   @  s   e Zd ZdZdS )r
   zM
    Base class for exceptions raised when the opening handshake fails.

    Nr   r$   r$   r$   r%   r
      s   c                   @  s   e Zd ZdZdS )r   z
    Raised when a handshake request or response breaks a security rule.

    Security limits can be configured with :doc:`environment variables
    <../reference/variables>`.

    Nr   r$   r$   r$   r%   r      s   c                   @  s.   e Zd ZdZdddddZddd	d
ZdS )r   zJ
    Raised when a handshake response rejects the WebSocket upgrade.

    zhttp11.Responser&   )responser*   c                 C  s
   || _ d S r+   )r@   )r-   r@   r$   r$   r%   r.      s    zInvalidStatus.__init__r/   r0   c                 C  s   d| j jdS )Nz+server rejected WebSocket connection: HTTP d)r@   status_coder1   r$   r$   r%   r2      s    zInvalidStatus.__str__Nr?   r$   r$   r$   r%   r      s   c                   @  s2   e Zd ZdZdddddddZdd	d
dZdS )r   zK
    Raised when an HTTP header doesn't have a valid format or value.

    Nr/   
str | Noner&   namevaluer*   c                 C  s   || _ || _d S r+   rE   rF   r-   rE   rF   r$   r$   r%   r.      s    zInvalidHeader.__init__r0   c                 C  sH   | j d krd| j dS | j dkr0d| j dS d| j d| j  S d S )Nzmissing z headerr9   zempty zinvalid z	 header: rF   rE   r1   r$   r$   r%   r2      s
    

zInvalidHeader.__str__)Nr?   r$   r$   r$   r%   r      s   c                      s.   e Zd ZdZdddddd fddZ  ZS )r   z
    Raised when an HTTP header cannot be parsed.

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

    r/   r3   r&   )rE   errorheaderposr*   c                   s"   t  || d| d|  d S )Nz at z in superr.   )r-   rE   rJ   rK   rL   	__class__r$   r%   r.      s    zInvalidHeaderFormat.__init__r    r!   r"   r#   r.   __classcell__r$   r$   rO   r%   r      s   c                   @  s   e Zd ZdZdS )r   z
    Raised when an HTTP header has a wrong value.

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

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

    rC   r&   )originr*   c                   s   t  d| d S )NZOriginrM   )r-   rS   rO   r$   r%   r.      s    zInvalidOrigin.__init__rQ   r$   r$   rO   r%   r      s   c                   @  s   e Zd ZdZdS )r   zF
    Raised when the Upgrade or Connection header isn't correct.

    Nr   r$   r$   r$   r%   r     s   c                   @  s   e Zd ZdZdS )r   zG
    Raised when negotiating an extension or a subprotocol fails.

    Nr   r$   r$   r$   r%   r     s   c                   @  s.   e Zd ZdZdddddZdddd	Zd
S )r   zK
    Raised when a parameter name is repeated in an extension header.

    r/   r&   rE   r*   c                 C  s
   || _ d S r+   rE   r-   rE   r$   r$   r%   r.     s    zDuplicateParameter.__init__r0   c                 C  s   d| j  S )Nzduplicate parameter: rU   r1   r$   r$   r%   r2     s    zDuplicateParameter.__str__Nr?   r$   r$   r$   r%   r     s   c                   @  s.   e Zd ZdZdddddZdddd	Zd
S )r   zJ
    Raised when a parameter name in an extension header is invalid.

    r/   r&   rT   c                 C  s
   || _ d S r+   rU   rV   r$   r$   r%   r.   %  s    zInvalidParameterName.__init__r0   c                 C  s   d| j  S )Nzinvalid parameter name: rU   r1   r$   r$   r%   r2   (  s    zInvalidParameterName.__str__Nr?   r$   r$   r$   r%   r     s   c                   @  s0   e Zd ZdZddddddZddd	d
ZdS )r   zK
    Raised when a parameter value in an extension header is invalid.

    r/   rC   r&   rD   c                 C  s   || _ || _d S r+   rG   rH   r$   r$   r%   r.   2  s    zInvalidParameterValue.__init__r0   c                 C  sD   | j d krd| j S | j dkr,d| j S d| j d| j  S d S )Nzmissing value for parameter r9   zempty value for parameter zinvalid value for parameter z: rI   r1   r$   r$   r%   r2   6  s
    

zInvalidParameterValue.__str__Nr?   r$   r$   r$   r%   r   ,  s   c                   @  s   e Zd ZdZdS )r   a  
    Raised when receiving or sending a frame that breaks the protocol.

    The Sans-I/O implementation raises this exception when:

    * receiving or sending a frame that contains invalid data;
    * receiving or sending an invalid sequence of frames.

    Nr   r$   r$   r$   r%   r   ?  s   c                   @  s   e Zd ZdZdS )r   aU  
    Raised when parsing a frame with a payload that exceeds the maximum size.

    The Sans-I/O layer uses this exception internally. It doesn't bubble up to
    the I/O layer.

    The :meth:`~websockets.extensions.Extension.decode` method of extensions
    must raise :exc:`PayloadTooBig` if decoding a frame would exceed the limit.

    Nr   r$   r$   r$   r%   r   K  s   c                   @  s   e Zd ZdZdS )r   a  
    Raised when sending a frame is forbidden in the current state.

    Specifically, the Sans-I/O layer raises this exception when:

    * sending a data frame to a connection in a state other
      :attr:`~websockets.protocol.State.OPEN`;
    * sending a control frame to a connection in a state other than
      :attr:`~websockets.protocol.State.OPEN` or
      :attr:`~websockets.protocol.State.CLOSING`.

    Nr   r$   r$   r$   r%   r   X  s   c                   @  s   e Zd ZdZdS )r   z
    Raised when receiving or sending messages concurrently.

    WebSocket is a connection-oriented protocol. Reads must be serialized; so
    must be writes. However, reading and writing concurrently is possible.

    Nr   r$   r$   r$   r%   r   g  s   )r   r   r   r   z.legacy.exceptions)r   r   r   r   r   )aliases)r7   http11)+r#   
__future__r   typingr4   Zimportsr   __all__	Exceptionr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r   RuntimeErrorr   TYPE_CHECKINGZlegacy.exceptionsr   r   r   r   r   globalsr9   r7   rX   r$   r$   r$   r%   <module>   s   B


	
