Team Ai
Datasetpublic

codekingpro/portable-devtools

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

2��j�_���SrSSKJr SSKrSSKJrJr SSKJrJ	r	J3r4 SSKJr SSK
JrJr SS	KJr SS5KJrJrJr SSKJrJr \(aSSKJr \R6"\5rS
rSrSr \!"S5Vs/sHnSU-S-6PM
 snr"Sr#SSjr$SSjr%SSjr&SSjr'S Sjr("SS5r)"SS5r*gs snf)!zL7Implements the HPACK header compression algorithm as detailed by RFC 7541.8�)�annotationsN)�
TYPE_CHECKING�Any�)�HPACKDecodingError�InvalidTableSizeError�OversizedHeaderListError)�HuffmanEncoder)�
REQUEST_CODES�REQUEST_CODES_LENGTH)�decode_huffman)�HeaderTuple�HeaderWeaklyTyped�NeverIndexedHeaderTuple)�HeaderTable�table_entry_size)�Iterable���@�	�ic���[US5n[US5nU(d0URURS5URS55$URX#5$)z^9Provides a header as a unicode string if raw is False, otherwise returns10it as a bytestring.11rr�utf-8)�bytes�	__class__�decode)�header�raw�name�values    �QD:\code\apps\devtools\python\user_packages\Python313\site-packages\hpack/hpack.py�_unicode_if_neededr##sY��12�����D��&��)��E��������G� 4�e�l�l�7�6K�L�L����D�(�(�c�`�[RSX5 US:aSU3n[U5eUS:dUS:�aSU3n[U5e[UnX:a[	U/5$U/nX-nUS:�a$URUS-S-5 US	-nUS:�aM$URU5 [	U5$)13z]14Encodes an integer according to the wacky integer encoding rules15defined in the HPACK spec.16zEncoding %d with %d bitsrz'Can only encode positive integers, got r��)Prefix bits must be between 1 and 8, got ���)�log�debug�17ValueError�_PREFIX_BIT_MAX_NUMBERS�	bytearray�append)�integer�prefix_bits�msg�18max_number�elementss     r"�encode_integerr60s���19�I�I�(�'�?���{�7��y�A����o���Q��+��/�9�+��G����o��(��5�J����'��#�#��|�H���G�20�S�.�����3��#�-�.��A�
���S�.�
�O�O�G���X��r$c�N�US:dUS:�aSU3n[U5e[UnSnSnSSU-21-	nUSU-nXs:Xa*XnUS-
nUS:�aXxS-22U--
nOXxU--
nOUS-
nM)[RS23Xt5 Xt4$![an	SU<3n[U5U	eS	n	A	ff=f)z�24Decodes an integer according to the wacky integer encoding rules25defined in the HPACK spec. Returns a tuple of the decoded integer and the26number of bytes that were consumed from ``data`` in order to get that27integer.28rr&r'r��r(r*z3Unable to decode HPACK integer representation from NzDecoded %d, consumed %d bytes)r-r.�29IndexErrorrr+r,)30�datar2r3r4�index�shift�mask�number�	next_byte�errs31          r"�decode_integerrAOs����Q��+��/�9�+��G����o��(��5�J�
�E�
�E��Q��_�%�D�/��a��4������ �K�	���32����#��3��5�8�8�F��5�0�0�F����33����I�I�-�v�=��=���
�/�C�D�8�L�� ��%�3�.��/�s�/B�#B�34B$�
B�B$c#�# �[U[5(dS[U53n[U5e[	UR5SS9nUHnX0U4v� M
 g7f)z�35Converts a dictionary to an iterable of key-value tuples. This is a36HPACK-specific function because it pulls "special-headers" out first and37then emits them.38zheader_dict not a dict, but c�@�[U5RS5(+$)N�:)�	_to_bytes�39startswith)�ks r"�<lambda>�#_dict_to_iterable.<locals>.<lambda>�s��)�A�,�1�1�$�7�7r$)�keyN)�40isinstance�dict�type�	TypeError�sorted�keys)�header_dictr3rPrJs    r"�_dict_to_iterablerRvsc����k�4�(�(�,�T�+�->�,?�@����n�������7��D����s�#�#�#��s�AAc�x�[U5nU[LaU$U[La[U5nURS5$)z:41Convert anything to bytes through a UTF-8 encoded string42r)rMr�str�encode)r!�ts  r"rErE�s8��	
�U��A��E�z�����|��E�43���<�<�� � r$c��\rSrSrSrSSjr\SSj5r\RSSj5rSSSjjr	SSSjjr44SSjrSSS	jjrSSS45jjr
SSjrSrg
)�Encoder�za46An HPACK encoder object. This object takes HTTP headers and emits encoded47HTTP/2 header blocks.48c�b�[5Ul[[[5Ul/Ulg�N)r�header_tabler49rr�
huffman_coder�table_size_changes��selfs r"�__init__�Encoder.__init__�s)��'�M���+��/�50���.0��r$c�.�URR$�z.51Controls the size of the HPACK header table.52�r\�maxsizer_s r"�header_table_size�Encoder.header_table_size����53� � �(�(�(r$c��XRlURR(aURR	U5 ggr[)r\rf�resizedr^r0�r`r!s  r"rgrh�s6��$)���!����$�$��#�#�*�*�5�1�%r$c�N�/nURR(a0URUR55 SURl[	U[545(a[
U5nO[U5nUH}nSn[	U[5(aUR(+nO[U5S:�aUSn[US5[US54nURURXvU55 M SRU5n[RSU5 U$)ah55Takes a set of headers and encodes them into a HPACK-encoded header56block.57 58:param headers: The headers to encode. Must be either an iterable of59                tuples, an iterable of :class:`HeaderTuple60                <hpack.HeaderTuple>`, or a ``dict``.61 62                If an iterable of tuples, the tuples may be either63                two-tuples or three-tuples. If they are two-tuples, the64                tuples must be of the format ``(name, value)``. If they65                are three-tuples, they must be of the format66                ``(name, value, sensitive)``, where ``sensitive`` is a67                boolean value indicating whether the header should be68                added to header tables anywhere. If not present,69                ``sensitive`` defaults to ``False``.70 71                If an iterable of :class:`HeaderTuple72                <hpack.HeaderTuple>`, the tuples must always be73                two-tuples. Instead of using ``sensitive`` as a third74                tuple entry, use :class:`NeverIndexedHeaderTuple75                <hpack.NeverIndexedHeaderTuple>` to request that76                the field never be indexed.77 78                .. warning:: HTTP/2 requires that all special headers79                    (headers whose names begin with ``:`` characters)80                    appear at the *start* of the header block. While81                    this method will ensure that happens for ``dict``82                    subclasses, callers using any other iterable of83                    tuples **must** ensure they place their special84                    headers at the start of the iterable.85 86                    For efficiency reasons users should prefer to use87                    iterables of two-tuples: fixing the ordering of88                    dictionary headers is an expensive operation that89                    should be avoided if possible.90 91:param huffman: (optional) Whether to Huffman-encode any header sent as92                a literal value. Except for use when debugging, it is93                recommended that this be left enabled.94 95:returns: A bytestring containing the HPACK-encoded header block.96Frrrr$zEncoded header block to %s)r\rkr0�_encode_table_size_changerKrLrR�iterr�	indexable�lenrE�add�joinr+r,)	r`�headers�huffman�header_block�
hpack_headersr�	sensitive�97new_header�encodeds	         r"rU�Encoder.encode�s��p�����$�$����� >� >� @�A�(-�D���%��g�t�$�$�.�g�6�M�
�&!��M�M�$�F��I��&�+�.�.� &� 0� 0�0�	��V��q��"�1�I�	�#�F�1�I�.�	�&��)�0D�E�J��������� H�I�$��(�(�<�(���	�	�.��8��r$c��[RSUUU5 UupEU(d[O[nURRXE5nUc6UR
XEXc5nU(dURRXE5 U$Uup�n98U99(aURU	5nU$URX�Xc5nU(dURRXE5 U$)z&100Serializes a header key-value tuple.101z7Adding %s to the header table, sensitive:%s, huffman:%s)102r+r,�INDEX_INCREMENTAL�INDEX_NEVERr\�search�_encode_literalrr�_encode_indexed�_encode_indexed_literal)r`�to_addrxrur r!�indexbit�matchrzr;�perfects           r"rr�Encoder.adds���	�	�	�E����		103����-6�$�;���!�!�(�(��5���=��*�*�4��J�G���!�!�%�%�d�2��N�104 %���W���*�*�5�1�G���
�2�2��h��G���!�!�%�%�d�2��r$c�J�[US5nUS==S-ss'[U5$)z4105Encodes a header using the indexed representation.106r*rr()r6r)r`r;�fields   r"r��Encoder._encode_indexedMs(���u�a�(��
�a��D����U�|�r$c�b�U(a6URRU5nURRU5n[[U5S5n[[U5S5nU(aUS==S-ss'US==S-ss'SR	U[U5U[U5U/5$)z�107Encodes a header with a literal name and literal value. If ``indexing``108is True, the header will be added to the header table: otherwise it109will not.110r*rr(r$)r]rUr6rqrsr)r`r r!r�ru�name_len�	value_lens       r"r��Encoder._encode_literalUs�����%�%�,�,�T�2�D��&�&�-�-�e�4�E�!�#�d�)�Q�/��"�3�u�:�q�1�	���Q�K�4��K��a�L�D� �L��x�x�
�u�X���e�I�.>��F�111�	112r$c�V�U[:wa
[US5nO[US5nUS==[U5-ss'U(aURR	U5n[[U5S5nU(a
US==S-ss'SR
[U5[U5U/5$)z^113Encodes a header with an indexed name and a literal value and performs114incremental indexing.115��rr*r(r$)r}r6�ordr]rUrqrsr)r`r;r!r�ru�prefixr�s       r"r��Encoder._encode_indexed_literaljs���116�(�(�#�E�1�-�F�#�E�1�-�F��q�	�S��]�"�	���&�&�-�-�e�4�E�"�3�u�:�q�1�	���a�L�D� �L��x�x��v���i�(8�%�@�A�Ar$c��SnURH*n[US5nUS==S-ss'U[U5-
nM, /UlU$)zL117Produces the encoded form of all header table size change context118updates.119r$�r� )r^r6r)r`�block�120size_bytes�bs    r"rn�!Encoder._encode_table_size_change�sO��121���1�1�J��z�1�-�A�
�a�D�D�L�D��U�1�X��E�2�#%����r$)r\r]r^N��return�None�r��int�r!r�r�r�)T)rtz�Iterable[HeaderTuple | tuple[bytes | str, bytes | str] | tuple[bytes | str, bytes | str, bool | None]] | dict[bytes | str, bytes | str]ru�boolr�r�F)r�ztuple[bytes, bytes]rxr�rur�r�r)r;r�r�r)122r rr!rr�rrur�r�r)123r;r�r!rr�rrur�r�r)r�r)�__name__�124__module__�__qualname__�__firstlineno__�__doc__ra�propertyrg�setterrUrrr�r�r�rn�__static_attributes__�r$r"rXrX�s����1250��)��)����2��2� $�
k�2�k��
k�).�
k�Z/�b�126�*B�,r$rXc��\rSrSrSr\4SSjjr\SSj5r\RSSj5rSSSjjr127SSjrSSjrSS	jr
SS128jrSSjrSSjrS
rg)�Decoderi�a�129An HPACK decoder object.130 131.. versionchanged:: 2.3.0132   Added ``max_header_list_size`` argument.133 134:param max_header_list_size: The maximum decompressed size we will allow135    for any single header block. This is a protection against DoS attacks136    that attempt to force the application to expand a relatively small137    amount of data into a really large header list, allowing enormous138    amounts of memory to be allocated.139 140    If this amount of data is exceeded, a `OversizedHeaderListError141    <hpack.OversizedHeaderListError>` exception will be raised. At this142    point the connection should be shut down, as the HPACK state will no143    longer be usable.144 145    Defaults to 64kB.146:type max_header_list_size: ``int``147c�d�[5UlXlURRUlgr[)rr\�max_header_list_sizerf�max_allowed_table_size)r`r�s  r"ra�Decoder.__init__�s*��'�M��� %9�!�'+�&7�&7�&?�&?��#r$c�.�URR$rdrer_s r"rg�Decoder.header_table_size�rir$c�$�XRlgr[rerls  r"rgr��s��$)���!r$c�8�[RSU5 [U5n/n[U5nSnSnXu:GaXn[	US-5n	[	US-5n148[	US-5nU	(aURX7S5up�OcU149(aUR
X7S5up�OFU(a*U(a
Sn[U5eURX7S5n
SnOURX7S5up�U(aPURU5 U[USUS5-
nX`R:�aS	URS1503n[U5eX}-
nXu:aGMUR5 UVs/sHn[X�5PM sn$s snf![ anSn[U5UeSnAff=f)a�151Takes an HPACK-encoded header block and decodes it into a header set.152 153:param data: A bytestring representing a complete HPACK-encoded header154             block.155:param raw: (optional) Whether to return the headers as tuples of raw156            byte strings or to decode them as UTF-8 before returning157            them. The default value is False, which returns tuples of158            Unicode strings159:returns: A list of two-tuples of ``(name, value)`` representing the160          HPACK-encoded headers, in the order they were decoded.161:raises HPACKDecodingError: If an error is encountered while decoding162                            the header block.163zDecoding %srr(�@r�Nz/Table size update not at the start of the blockrzA header list larger than z has been receivedz!Unable to decode headers as UTF-8)r+r,�164memoryviewrqr��_decode_indexed�_decode_literal_indexr�_update_encoding_context�_decode_literal_no_indexr0rr�r	�_assert_valid_table_sizer#�UnicodeDecodeError)r`r:r�data_memrt�data_len�
inflated_size�
current_index�current�indexed�
literal_index�encoding_updater�consumedr3�hr@s                 r"r�Decoder.decode�s���	�	�	�-��&��d�#��%'���t�9���
��
��&��)�G��7�T�>�*�G�!��4��0�M�#�7�T�>�2�O��#'�#7�#7��^�,�$� ����#'�#=�#=��^�,�$� ���!��K�C�,�S�1�1��8�8��^�,�����$(�#@�#@��^�,�$� ������v�&��!1�&��)�V�A�Y�!G�G�
� �#<�#<�<�6�t�7P�7P�6Q�Qc�d�C�2�3�7�7��%�M�_�&�h	
�%�%�'�	3�8?�@��1�&�q�.��@�@��@��!�	3�5�C�$�S�)�s�2��	3�s*�E<�E7�4E<�7E<�<165F�F�Fc�R�URUR:�a
Sn[U5eg)z[166Check that the table size set by the encoder is lower than the maximum167we expect to have.168z3Encoder did not shrink table size to within the maxN)rgr�r)r`r3s  r"r�� Decoder._assert_valid_table_sizes.��169�!�!�D�$?�$?�?�G�C�'��,�,�@r$c�f�[US5up#X R:�a
Sn[U5eX lU$)z3170Handles a byte that updates the encoding context.171r�z)Encoder exceeded max allowable table size)rAr�rrg)r`r:�new_sizer�r3s     r"r�� Decoder._update_encoding_context's:��172,�D�!�4����1�1�1�=�C�'��,�,�!)���r$c��[US5up#[URRU56n[RSXC5 XC4$)z@173Decodes a header represented using the indexed representation.174r*zDecoded %s, consumed %d)rArr\�get_by_indexr+r,)r`r:r;r�rs     r"r��Decoder._decode_indexed3sE��)��q�1����d�/�/�<�<�U�C�D���	�	�+�V�>���r$c�"�URUSS9$)NF��should_index��_decode_literal�r`r:s  r"r�� Decoder._decode_literal_no_index<s���#�#�D�u�#�=�=r$c�"�URUSS9$)NTr�r�r�s  r"r��Decoder._decode_literal_index?s���#�#�D�t�#�<�<r$c���SnU(a
USS-nSnSnOUSnUS-nSn[US-5nU(a0[X5up�URRU5Sn175U	nSnOTUSS	n[US1765up�XX�-n177[	U1785U:wa
Sn[U5eUSS-(a[
U1795n180X�-S-nXU-S	n[US1815up�XX�-n
[	U
5U:wa
Sn[U5eUSS-(a[
U
5n
X;U	--
nU(a[X�5nO[X�5nU(aURRX�5 [RS
UUU5 X�4$)z.182Decodes a header represented with a literal.183r�?r�F�r��rNr*zTruncated header blockr(z/Decoded %s, total consumed %d bytes, indexed %s)r�rAr\r�rqrr
rrrrr+r,)r`r:r��total_consumed�indexed_namer��
not_indexable�	high_byter;r�r �lengthr3r!rs               r"r��Decoder._decode_literalBs��������7�T�>�L��H�!�M��Q��I�$�t�+�L��H� ��T�!1�2�M��,�T�<�O�E��$�$�1�1�%�8��;�D�%�N��F����8�D�-�d�A�6��F���!2�3�D��4�y�F�"�.��(��-�-��A�w��~�%�d�+��%�.��2�N��v�%�&�'��*�$��2����h�/�0���u�:���*�C�$�S�)�)���7�T�>�"�5�)�E�	�8�+�+��184�,�T�9�F� ��-�F�����!�!�$�.��	�	�=����		185��%�%r$)r\rgr�r�N)r�r�r�r�r�r�r�)r:rrr�r�zIterable[HeaderTuple]r�)r:rr�r�)r:rr��tuple[HeaderTuple, int])r:rr�r�r�r�)r�r�r�r�r��DEFAULT_MAX_HEADER_LIST_SIZErar�rgr�rr�r�r�r�r�r�r�r�r$r"r�r��sg���*4P�@�8�)��)����*��*�Q3�f-�186� �>�=�L&r$r�)rrrr�r�r)r1r�r2r�r�r/)r:rr2r�r�ztuple[int, int])rQzdict[bytes | str, bytes | str]r�z)Iterable[tuple[bytes | str, bytes | str]])r!zbytes | str | Anyr�r)+r��187__future__r�logging�typingrr�188exceptionsrrr	rur189�huffman_constantsrr�
huffman_tabler
�structrrr�tablerr�collections.abcr�	getLoggerr�r+�190INDEX_NONEr~r}�ranger.r�r#r6rArRrErXr�)�is0r"�<module>r�s����#��%�[�[�#�B�)�K�K�0��(�
����!��
�191�����19227�q��:��A�A��F�a�<��:�� '��193)��>$�N$�4�$�&	!�v�v�r@&�@&��e;s�4B;
codekingpro/portable-devtools · Team Ai