B
    ]bA'                 @  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	 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ZG d4d dee Z!G d5d deZ"G d6d deZ#G d7d de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               @  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%   4lib/python3.7/site-packages/websockets/exceptions.pyr   D   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	   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.

    NzOptional[frames.Close]zOptional[bool]None)rcvdsentrcvd_then_sentreturnc             C  s   || _ || _|| _d S )N)r(   r)   r*   )selfr(   r)   r*   r%   r%   r&   __init__Z   s    zConnectionClosed.__init__str)r+   c             C  s   | j d krD| jd kr&| jd ks"tdS | jd ks4td| j dS nb| jd krj| jd ks\td| j  dS | jd k	sxt| jrd| 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*   AssertionError)r,   r%   r%   r&   __str__d   s    


zConnectionClosed.__str__intc             C  s   | j d krdS | j jS )Ni  )r(   code)r,   r%   r%   r&   r2   y   s    zConnectionClosed.codec             C  s   | j d krdS | j jS )N )r(   reason)r,   r%   r%   r&   r4   }   s    zConnectionClosed.reason)N)	r!   r"   r#   r$   r-   r0   propertyr2   r4   r%   r%   r%   r&   r	   K   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 code other than 1000 (OK) or 1001 (going away) was received or
    sent, or the closing handshake didn't complete properly.

    N)r!   r"   r#   r$   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 properly.

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

    N)r!   r"   r#   r$   r%   r%   r%   r&   r      s   c               @  s   e Zd ZdZdS )r   zK
    Raised during the handshake when the WebSocket connection fails.

    N)r!   r"   r#   r$   r%   r%   r%   r&   r      s   c               @  s   e Zd ZdZdS )r   zs
    Raised when a handshake request or response breaks a security rule.

    Security limits are hard coded.

    N)r!   r"   r#   r$   r%   r%   r%   r&   r      s   c               @  s   e Zd ZdZdS )r   zD
    Raised when a handshake request or response is malformed.

    N)r!   r"   r#   r$   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   zJ
    Raised when a HTTP header doesn't have a valid format or value.

    Nr.   zOptional[str]r'   )namevaluer+   c             C  s   || _ || _d S )N)r6   r7   )r,   r6   r7   r%   r%   r&   r-      s    zInvalidHeader.__init__)r+   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 headerr3   zempty zinvalid z	 header: )r7   r6   )r,   r%   r%   r&   r0      s
    

zInvalidHeader.__str__)N)r!   r"   r#   r$   r-   r0   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 a HTTP header cannot be parsed.

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

    r.   r1   r'   )r6   errorheaderposr+   c               s"   t  || d| d|  d S )Nz at z in )superr-   )r,   r6   r8   r9   r:   )	__class__r%   r&   r-      s    zInvalidHeaderFormat.__init__)r!   r"   r#   r$   r-   __classcell__r%   r%   )r<   r&   r      s   c               @  s   e Zd ZdZdS )r   z~
    Raised when a HTTP header has a wrong value.

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

    N)r!   r"   r#   r$   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.

    zOptional[str]r'   )originr+   c               s   t  d| d S )NZOrigin)r;   r-   )r,   r>   )r<   r%   r&   r-      s    zInvalidOrigin.__init__)r!   r"   r#   r$   r-   r=   r%   r%   )r<   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.

    N)r!   r"   r#   r$   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 )N)r?   )r,   r?   r%   r%   r&   r-      s    zInvalidStatus.__init__r.   )r+   c             C  s   d| j jdS )Nz+server rejected WebSocket connection: HTTP d)r?   status_code)r,   r%   r%   r&   r0      s    zInvalidStatus.__str__N)r!   r"   r#   r$   r-   r0   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   zC
    Raised when a handshake response status code is invalid.

    r1   zdatastructures.Headersr'   )rA   headersr+   c             C  s   || _ || _d S )N)rA   rB   )r,   rA   rB   r%   r%   r&   r-      s    zInvalidStatusCode.__init__r.   )r+   c             C  s   d| j  S )Nz+server rejected WebSocket connection: HTTP )rA   )r,   r%   r%   r&   r0      s    zInvalidStatusCode.__str__N)r!   r"   r#   r$   r-   r0   r%   r%   r%   r&   r      s   c               @  s   e Zd ZdZdS )r   z6
    Raised when negotiating an extension fails.

    N)r!   r"   r#   r$   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'   )r6   r+   c             C  s
   || _ d S )N)r6   )r,   r6   r%   r%   r&   r-     s    zDuplicateParameter.__init__)r+   c             C  s   d| j  S )Nzduplicate parameter: )r6   )r,   r%   r%   r&   r0     s    zDuplicateParameter.__str__N)r!   r"   r#   r$   r-   r0   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'   )r6   r+   c             C  s
   || _ d S )N)r6   )r,   r6   r%   r%   r&   r-     s    zInvalidParameterName.__init__)r+   c             C  s   d| j  S )Nzinvalid parameter name: )r6   )r,   r%   r%   r&   r0      s    zInvalidParameterName.__str__N)r!   r"   r#   r$   r-   r0   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.   zOptional[str]r'   )r6   r7   r+   c             C  s   || _ || _d S )N)r6   r7   )r,   r6   r7   r%   r%   r&   r-   *  s    zInvalidParameterValue.__init__)r+   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 r3   zempty value for parameter zinvalid value for parameter z: )r7   r6   )r,   r%   r%   r&   r0   .  s
    

zInvalidParameterValue.__str__N)r!   r"   r#   r$   r-   r0   r%   r%   r%   r&   r   $  s   c               @  s4   e Zd ZdZdddddddd	Zd
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.
        zhttp.HTTPStatuszdatastructures.HeadersLikebytesr'   )statusrB   bodyr+   c             C  s   || _ t|| _|| _d S )N)rE   r   ZHeadersrB   rF   )r,   rE   rB   rF   r%   r%   r&   r-   F  s    zAbortHandshake.__init__r.   )r+   c             C  s(   d| j ddt| j dt| j dS )NzHTTP r@   z, z
 headers, z bytes)rE   lenrB   rF   )r,   r%   r%   r&   r0   P  s    zAbortHandshake.__str__N)rC   )r!   r"   r#   r$   r-   r0   r%   r%   r%   r&   r   7  s   c               @  s.   e Zd ZdZdddddZdddd	Zd
S )r   zd
    Raised when a handshake gets redirected.

    This exception is an implementation detail.

    r.   r'   )urir+   c             C  s
   || _ d S )N)rH   )r,   rH   r%   r%   r&   r-   `  s    zRedirectHandshake.__init__)r+   c             C  s   d| j  S )Nzredirect to )rH   )r,   r%   r%   r&   r0   c  s    zRedirectHandshake.__str__N)r!   r"   r#   r$   r-   r0   r%   r%   r%   r&   r   X  s   c               @  s   e Zd ZdZdS )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.

    N)r!   r"   r#   r$   r%   r%   r%   r&   r   g  s   c               @  s0   e Zd ZdZddddddZdddd	Zd
S )r   zM
    Raised when connecting to an URI that isn't a valid WebSocket URI.

    r.   r'   )rH   msgr+   c             C  s   || _ || _d S )N)rH   rI   )r,   rH   rI   r%   r%   r&   r-   x  s    zInvalidURI.__init__)r+   c             C  s   | j  d| j S )Nz isn't a valid URI: )rH   rI   )r,   r%   r%   r&   r0   |  s    zInvalidURI.__str__N)r!   r"   r#   r$   r-   r0   r%   r%   r%   r&   r   r  s   c               @  s   e Zd ZdZdS )r   zS
    Raised when receiving a frame with a payload exceeding the maximum size.

    N)r!   r"   r#   r$   r%   r%   r%   r&   r     s   c               @  s   e Zd ZdZdS )r   z3
    Raised when a frame breaks the protocol.

    N)r!   r"   r#   r$   r%   r%   r%   r&   r     s   )&r$   Z
__future__r   Zhttptypingr   r3   r   r   r   __all__	Exceptionr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r/   r   r   r   r   r    r%   r%   r%   r&   <module>   sl   7
			
!