Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
server.cpython-313.pyc274 linesDownload Raw Back to __pycache__
1�

2p�jkW�	��SSKJr SSKrSSKrSSKrSSKrSSKrSSKrSSK	J3r4Jr SSKJ
r
JrJr SSKJrJr SSKJrJrJrJrJrJrJr SSKJrJr SS	KJrJ r J!r!J"r"J#r# SS5K$J%r%J&r& SSK'J(r( SSK)J*r*J+r+J,r,J-r-J.r. SS
KJ/r/J0r0J1r1J2r2J3r3J4r4J5r5 SSK6J7r7 S/r8"SS\-5r9"SS\95r:\("\;"5SSSSSS.S9 g)�)�annotationsN)�	Generator�Sequence)�Any�Callable�cast�)�Headers�MultipleValuesError)�InvalidHandshake�
InvalidHeader�InvalidHeaderValue�InvalidMessage�
InvalidOrigin�InvalidUpgrade�NegotiationError)�	Extension�ServerExtensionFactory)�build_extension�parse_connection�parse_extension�parse_subprotocol�
parse_upgrade)�Request�Response)�lazy_import)�6CONNECTING�OPEN�SERVER�Protocol�State)�ConnectionOption�ExtensionHeader�7LoggerLike�Origin�8StatusLike�Subprotocol�UpgradeProtocol)�9accept_key�ServerProtocolc��^�\rSrSrSrSSSS\SSS.SU4SjjjrSSjrSSjrSS	jr	SS10jr11SSjrSSjrSS
jr
SSjrSU4SjjrSrU=r$)r*�0a�12Sans-I/O implementation of a WebSocket server connection.13 14Args:15    origins: Acceptable values of the ``Origin`` header. Values can be16        :class:`str` to test for an exact match or regular expressions17        compiled by :func:`re.compile` to test against a pattern. Include18        :obj:`None` in the list if the lack of an origin is acceptable.19        This is useful for defending against Cross-Site WebSocket20        Hijacking attacks.21    extensions: List of supported extensions, in order in which they22        should be tried.23    subprotocols: List of supported subprotocols, in order of decreasing24        preference.25    select_subprotocol: Callback for selecting a subprotocol among26        those supported by the client and the server. It has the same27        signature as the :meth:`select_subprotocol` method, including a28        :class:`ServerProtocol` instance as first argument.29    state: Initial state of the WebSocket connection.30    max_size: Maximum size of incoming messages in bytes.31        :obj:`None` disables the limit. You may pass a ``(max_message_size,32        max_fragment_size)`` tuple to set different limits for messages and33        fragments when you expect long messages sent in short fragments.34    logger: Logger for this connection;35        defaults to ``logging.getLogger("websockets.server")``;36        see the :doc:`logging guide <../../topics/logging>` for details.37 38Ni)�origins�39extensions�subprotocols�select_subprotocol�state�max_size�loggerc�>�[TU][UUUS9 XlX lX0lUb'[
USURXR55 gg)N)�sider1r2r3r0)	�super�__init__rr-�available_extensions�available_subprotocols�setattr�__get__�	__class__)	�selfr-r.r/r0r1r2r3r<s	        ��WD:\code\apps\devtools\python\user_packages\Python313\site-packages\websockets/server.pyr7�ServerProtocol.__init__Nsc���"	�������		�	40���$.�!�&2�#��)�
��$�"�*�*�4���@�
�*�c��URU5unnn[+5n	[,R.R1SS9U	S'S	U	S41'S42U	S'X)S'UbX9S'UbXIS'[3SSU	5$![aonXQlXPlUR(aUR43R	SSS9 UR
[RRSUS35sSnA$SnAf[a�nXQlXPlUR(aUR44R	SSS9 UR
[RRSUS35nS	URS45'UsSnA$SnAf[a�nXQlXPlUR(aUR46R	SSS9 [[U5nUnUR b#UR nUSU3-
nUR bM#UR
[RR"SUS35sSnA$SnAf[$aZnXQlXPlUR47R'S
SS9 UR
[RR(S5sSnA$SnAff=f)a]48Create a handshake response to accept the connection.49 50If the handshake request is valid and the handshake successful,51:meth:`accept` returns an HTTP response with status code 101.52 53Else, it returns an HTTP response with another status code. This rejects54the connection, like :meth:`reject` would.55 56You must send the handshake response with :meth:`send_response`.57 58You may modify the response before sending it, typically by adding HTTP59headers.60 61Args:62    request: WebSocket handshake request received from the client.63 64Returns:65    WebSocket handshake response or HTTP response to send to the client.66 67z! invalid originT)�exc_infoz'Failed to open a WebSocket connection: z.68Nz! invalid upgradez^.69 70You cannot access a WebSocket server directly with a browser. You need a WebSocket client.71�	websocket�Upgradez! invalid handshakez; zopening handshake failedzLFailed to open a WebSocket connection.72See server log for more information.73��usegmt�Date�74ConnectionzSec-WebSocket-Accept�Sec-WebSocket-Extensions�Sec-WebSocket-Protocol�ezSwitching Protocols)�process_requestr�75_exception�
handshake_exc�debugr3�reject�http�76HTTPStatus�	FORBIDDENr�UPGRADE_REQUIRED�headersrr�
BaseException�	__cause__�BAD_REQUEST�	Exception�error�INTERNAL_SERVER_ERRORr77�email�utils�78formatdater)79r=�request�
accept_header�extensions_header�protocol_header�exc�response�	exc_chain�exc_strrUs80          r>�accept�ServerProtocol.acceptqsv��,9	�81�$�$�W�-�	
��!��n�)���+�+�0�0��0�=����(��	�� )����*7�&�'��(�2C�.�/��&�0?�,�-���2�G�<�<��}�	�!$��!$���z�z����!�!�"4�t�!�D��;�;����)�)�9�#��c�B��
���	�!$��!$���z�z����!�!�"5��!�E��{�{����0�0�=�c�U�CE�F��H�+6�H���Y�'��O���
	�!$��!$���z�z����!�!�"7�$�!�G��]�C�0�I�"��G��%�%�1�%�/�/�	��R�	�{�+�+���%�%�1��;�;����+�+�9�'��#�F��
���	�"%��!$���K�K���8�4��H��;�;����5�5�=��
��
	�s]�A*�*82J�4A$C�J�
J�+A5E&� J�&
J�3A8H �--H �J� 
J�-AJ�<J�Jc��URn[URS5Vs/sHn[U5PM sn/5n[	SU55(d([SU(aSR
U55eS5e[URS5Vs/sHn[U5PM sn/5n[U5S:XaUSR5S:Xd([SU(aSR
U55eS5eUS	n[R"UR5SS9n[U5S
:wa[%S	U5e['U5n	USn83U84S:wa[%SU855eUR)U5UlUR-U5uo�lUR1U5=o�lX�U4$s snfs snf![a
 [S	5Se[a [S	S865Sef=f![ R"an[%S	U5UeSnAff=f![a
 [S5Se[a [SS875Sef=f)a�88Check a handshake request and negotiate extensions and subprotocol.89 90This function doesn't verify that the request is an HTTP/1.1 or higher91GET request and doesn't check the ``Host`` header. These controls are92usually performed earlier in the HTTP request handling code. They're93the responsibility of the caller.94 95Args:96    request: WebSocket handshake request received from the client.97 98Returns:99    ``Sec-WebSocket-Accept``, ``Sec-WebSocket-Extensions``, and100    ``Sec-WebSocket-Protocol`` headers for the handshake response.101 102Raises:103    InvalidHandshake: If the handshake request is invalid;104        then the server must return 400 Bad Request error.105 106rHc3�F# �UHoR5S:Hv� M g7f)�upgradeN)�lower)�.0�values  r>�	<genexpr>�1ServerProtocol.process_request.<locals>.<genexpr>�s���F�:�%�;�;�=�I�-�:�s�!�, NrDr	rrCzSec-WebSocket-Key�multiple valuesT)�validate�zSec-WebSocket-Version�13)rU�sum�get_allr�anyr�joinr�lenrl�KeyErrorr
r�base64�	b64decode�encode�binascii�Errorrr)�process_origin�origin�process_extensionsr.�process_subprotocol�subprotocol)
r=r_rUrn�107connectionrk�key�raw_keyrcr`�versionrarbs
             r>rL�ServerProtocol.process_request�sZ��0�/�/��-0�29�/�/�,�2O�P�2O��
�e�
$�2O�P�RT�.108�109��F�:�F�F�F� ��z�d�i�i�110�3��
�GK��
�*-�/6���y�/I�J�/I�e�]�5�
!�/I�J�B�*111���G���!�g�a�j�&6�&6�&8�K�&G� ��'�D�I�I�g�,>�T�T�t�T�T�	R��-�.�C�112	H��&�&�s�z�z�|�d�C�G��w�<�2��$�%8�#�>�>�"�3��
�	V��5�6�G�113�d�?�$�%<�g�F�F��)�)�'�2���-1�-D�-D�W�-M�*��?�-1�-E�-E�g�-N�N��*��/�B�B��W
Q��
K���	?�� 3�4�$�>�"�	R�� 3�5F�G�T�Q�	R���~�~�	H�$�%8�#�>�C�G��	H���	C�� 7�8�d�B�"�	V�� 7�9J�K�QU�U�	V�s;�F5�F:�F?�#G/�H�?-G,�/H�
H�H�-Ic�\�URS5nUb[[U5nUR114b^UR115HCnX2:Xd:[
U[R5(dM)UcM.URU5cMB U$ [U5eU$![a [SS5Sef=f)z�116Handle the Origin HTTP request header.117 118Args:119    headers: WebSocket handshake request headers.120 121Returns:122   origin, if it is acceptable.123 124Raises:125    InvalidHandshake: If the Origin header is invalid.126    InvalidOrigin: If the origin isn't acceptable.127 128r%rrN)�getrr
rr%r-�129isinstance�re�Pattern�	fullmatchr)r=rUr��origin_or_regexs    r>r��ServerProtocol.process_origins���"	G��[�[��*�F����&�&�)�F��<�<�#�#'�<�<��"�,����130�131�;�;��*�'�1�1�&�9�E���
�$0�$�F�+�+��
��#�	G���*;�<�$�F�	G�s�B�B+c���Sn/n/nURS5nU(a�UR(a�[UVs/sHn[U5PM sn/5nUHbup�URHMn132U133RU:waMU134RX�5up�URX�45 URU5  M` Md U(a[U5nX$4$s snf![a M|f=f)a�135Handle the Sec-WebSocket-Extensions HTTP request header.136 137Accept or reject each extension proposed in the client request.138Negotiate parameters for accepted extensions.139 140Per :rfc:`6455`, negotiation rules are defined by the specification of141each extension.142 143To provide this level of flexibility, for each extension proposed by144the client, we check for a match with each extension available in the145server configuration. If no match is found, the extension is ignored.146 147If several variants of the same extension are proposed by the client,148it may be accepted several times, which won't make sense in general.149Extensions must implement their own requirements. For this purpose,150the list of previously accepted extensions is provided.151 152This process doesn't allow the server to reorder extensions. It can153only select a subset of the extensions proposed by the client.154 155Other requirements, for example related to mandatory extensions or the156order of extensions, may be implemented by overriding this method.157 158Args:159    headers: WebSocket handshake request headers.160 161Returns:162    ``Sec-WebSocket-Extensions`` HTTP response header and list of163    accepted extensions.164 165Raises:166    InvalidHandshake: If the Sec-WebSocket-Extensions header is invalid.167 168NrI)	rwr8rvr�name�process_request_paramsr�appendr)
r=rU�response_header_value�extension_headers�accepted_extensions�
header_values�header_value�parsed_header_valuesr��request_params�ext_factory�response_params�	extensions
             r>r��!ServerProtocol.process_extensions8s���N-1��35��/1�����(B�C�
��T�6�6�:=�CP�Q�=�<���.�=�Q�SU�;� �)=�$��#'�#<�#<�K�"�'�'�4�/� �!�5@�5W�5W�*�6�2��&�,�,�d�-D�E�'�.�.�y�9��%$=�)=�2�$3�4E�$F�!�$�9�9��?R��,�!� �!�s�C�?C�169C&�%C&c��[URS5Vs/sHn[U5PM sn/5nURU5$s snf)aA170Handle the Sec-WebSocket-Protocol HTTP request header.171 172Args:173    headers: WebSocket handshake request headers.174 175Returns:176   Subprotocol, if one was selected; this is also the value of the177   ``Sec-WebSocket-Protocol`` response header.178 179Raises:180    InvalidHandshake: If the Sec-WebSocket-Subprotocol header is invalid.181 182rJ)rvrwrr0)r=rUr�r/s    r>r��"ServerProtocol.process_subprotocol�sW��/2�%,�O�O�4L�$M�
�$M�L�"�,�/�$M�
�
�/183���&�&�|�4�4��

s�Ac���UR(dgU(d[S5e[U5nURHnX2;dM184Us $ [SSRUR5-5e)a�185Pick a subprotocol among those offered by the client.186 187If several subprotocols are supported by both the client and the server,188pick the first one in the list declared the server.189 190If the server doesn't support any subprotocols, continue without a191subprotocol, regardless of what the client offers.192 193If the server supports at least one subprotocol and the client doesn't194offer any, abort the handshake with an HTTP 400 error.195 196You provide a ``select_subprotocol`` argument to :class:`ServerProtocol`197to override this logic. For example, you could accept the connection198even if client doesn't offer a subprotocol, rather than reject it.199 200Here's how to negotiate the ``chat`` subprotocol if the client supports201it and continue without a subprotocol otherwise::202 203    def select_subprotocol(protocol, subprotocols):204        if "chat" in subprotocols:205            return "chat"206 207Args:208    subprotocols: List of subprotocols offered by the client.209 210Returns:211    Selected subprotocol, if a common subprotocol was found.212 213    :obj:`None` to continue without a subprotocol.214 215Raises:216    NegotiationError: Custom implementations may raise this exception217        to abort the handshake with an HTTP 400 error.218 219Nzmissing subprotocolz%invalid subprotocol; expected one of rq)r9r�setry)r=r/�proposed_subprotocolsr�s    r>r0�!ServerProtocol.select_subprotocol�st��R�*�*���"�#8�9�9�!$�L� 1���6�6�K��3�"�"�7�220�3��i�i��3�3�4�
5�221�	222r@c223��[R"U5nUR5n[S[R224R
SS94SS[[U554S/5n[URURXC5$)a�225Create a handshake response to reject the connection.226 227A short plain text response is the best fallback when failing to228establish a WebSocket connection.229 230You must send the handshake response with :meth:`send_response`.231 232You may modify the response before sending it, for example by changing233HTTP headers.234 235Args:236    status: HTTP status code.237    text: HTTP response body; it will be encoded to UTF-8.238 239Returns:240    HTTP response to send to the client.241 242rGTrE)rH�closezContent-Length)zContent-Typeztext/plain; charset=utf-8)rQrRr~r243r\r]r^�strrzrrn�phrase)r=�status�text�bodyrUs     r>rP�ServerProtocol.reject�su��*����(���{�{�}�������/�/�t�/�<�=�'�!�3�s�4�y�>�2�=�	
�244������f�m�m�W�C�Cr@c�J�UR(a�URURp2URRSX#5 URR5H!upEURRSXE5 M# UR(a/URRS[UR55 URRUR55 URS:Xa<UR[Lde[UlURRS5 gURRSURUR5 UR5 UR!5Ul[%UR"5 g)zl245Send a handshake response to the client.246 247Args:248    response: WebSocket handshake response event to send.249 250z> HTTP/1.1 %d %sz> %s: %sz> [body] (%d bytes)rKzconnection openzconnection rejected (%d %s)N)rO�status_code�
reason_phraser3rU�	raw_itemsr�rz�writesr��	serializer1rr�info�send_eof�discard�parser�next)r=rd�coder�r�rns      r>�
send_response�ServerProtocol.send_response�s)���:�:�#�/�/��1G�1G�&��K�K���0�$�?�&�.�.�8�8�:�251�����!�!�*�c�9�;��}�}����!�!�"7��X�]�]�9K�L������8�-�-�/�0����3�&��:�:��+�+�+��D�J��K�K���.�/�
�K�K���-��$�$��&�&�
�
�M�M�O��,�,�.�D�K�����r@c#��># �UR[La�[R"URR2525Shv�NnUR(aeURRSWR 5 UR"R%5H!up4URRSX45 M# UR&R)W5 [*TU]
5Shv�N gN�![ain[S5UlX Rl	UR5 UR5Ul[UR5 Sv� SnAGNSnAff=fN}7f)Nz$did not receive a valid HTTP requestz< GET %s HTTP/1.1z< %s: %s)r1rr�parse�reader�	read_linerYrrNrWr�r�r�r�rOr3�pathrUr��eventsr�r6)r=r_rcr�rnr<s     �r>r��ServerProtocol.parse s253�����:�:��#�
�%,�]�]��K�K�)�)�&����z�z����!�!�"5�w�|�|�D�")�/�/�";�";�"=�J�C��K�K�%�%�j�#�=�#>�
�K�K���w�'��7�=�?�"�"�)���
�%3�:�&��"�03�"�"�,��
�
��"�l�l�n����T�[�[�!����
��"	#�sN�E,�-C4�C2�C4�254B"E,�,E*�-E,�2C4�4255E'�>AE"�E,�"E'�'E,)	r8r9r.rNr�r-r�r1r�)r-z0Sequence[Origin | re.Pattern[str] | None] | Noner.z'Sequence[ServerExtensionFactory] | Noner/zSequence[Subprotocol] | Noner0zLCallable[[ServerProtocol, Sequence[Subprotocol]], Subprotocol | None] | Noner1r!r2z*int | None | tuple[int | None, int | None]r3zLoggerLike | None�return�None)r_rr�r)r_rr�z"tuple[str, str | None, str | None])rUr256r�z
Origin | None)rUr257r�z"tuple[str | None, list[Extension]])rUr258r��Subprotocol | None)r/zSequence[Subprotocol]r�r�)r�r&r�r�r�r)rdrr�r�)r�zGenerator[None])�__name__�259__module__�__qualname__�__firstlineno__�__doc__rr7rgrLr�r�r�r0rPr�r��__static_attributes__�
__classcell__�r<s@r>r*r*0s�����@EI�>B�59�
�!�?D�$(�!�B�!�<�	!�2603�!�
�!��!�=�!�"�!� 261�!!�!�FZ=�xFC��FC�262,�FC�P!�FO:��O:�263,�O:�b5�0:264�+�:265�266�:267�xD�B �D#�#r@c�,^�\rSrSrSU4SjjrSrU=r$)�ServerConnectioni:c�\>�[R"S[5 [TU]"U0UD6 g)Nz.ServerConnection was renamed to ServerProtocol)�warnings�warn�DeprecationWarningr6r7)r=�args�kwargsr<s   �r>r7�ServerConnection.__init__;s)����
�
�<��	268�	���$�)�&�)r@�)r�rr�rr�r�)r�r�r�r�r7r�r�r�s@r>r�r�:s
���*�*r@r�z.legacy.server)�WebSocketServer�WebSocketServerProtocol�	broadcast�serve�269unix_serve)�deprecated_aliases)<�270__future__rr|r�email.utilsr\rQr�r��collections.abcrr�typingrrr�datastructuresr271r�272exceptionsrr
rrrrrr.rrrUrrrrr�http11rr�importsr�protocolrrrr r!r"r#r$r%r&r'r(r]r)�__all__r*r��globalsr�r@r>�<module>r�s���"�
����	��/�&�&�8����:���&� �?�?������273��G#�X�G#�T*�~�*���I�,�#3�%�!�&�
�274r@
codekingpro/portable-devtools · Team Ai