ó
    ^Ñ7_›X  ã                   óT  • S r SSKrSSKJrJr  SSKJrJrJr  SSK	J
r
  SSKJrJr  SSKJr  SS	KJrJr  \R&                  " \5      rS
rSrSr\" S5       V s/ s H  n SU -  S-
  PM     sn r \rSrS r S r!S r"S r#S r$ " S S5      r% " S S5      r&gs  sn f ! \ a    \\4r N7f = f)ze
hpack/hpack
~~~~~~~~~~~

Implements the HPACK header compression algorithm as detailed by the IETF.
é    Né   )ÚHeaderTableÚtable_entry_size)ÚHPACKDecodingErrorÚOversizedHeaderListErrorÚInvalidTableSizeError)ÚHuffmanEncoder)ÚREQUEST_CODESÚREQUEST_CODES_LENGTH)Údecode_huffman)ÚHeaderTupleÚNeverIndexedHeaderTupleó    ó   ó   @é	   é   i   c                 ó®   • [        U S   5      n[        U S   5      nU(       d"  UR                  S5      nUR                  S5      nU R                  X#5      $ )z^
Provides a header as a unicode string if raw is False, otherwise returns
it as a bytestring.
r   r   úutf-8)ÚbytesÚdecodeÚ	__class__)ÚheaderÚrawÚnameÚvalues       Ú-/usr/lib/python3/dist-packages/hpack/hpack.pyÚ_unicode_if_neededr   +   sP   € ô
 �˜‘Ó€DÜ�&˜‘)Ó€EÞØ�{‰{˜7Ó#ˆØ—‘˜WÓ%ˆØ×Ñ˜DÓ(Ð(ó    c                 óX  • [         R                  SX5        U S:  a  [        SU -  5      eUS:  d  US:”  a  [        SU-  5      e[        U   nX:  a  [	        U /5      $ U/nX-  n U S:¼  a$  UR                  U S-  S-   5        U S	-  n U S:¼  a  M$  UR                  U 5        [	        U5      $ )
zb
This encodes an integer according to the wacky integer encoding rules
defined in the HPACK spec.
zEncoding %d with %d bitsr   z)Can only encode positive integers, got %sr   é   ú+Prefix bits must be between 1 and 8, got %sé€   é   é   )ÚlogÚdebugÚ
ValueErrorÚ_PREFIX_BIT_MAX_NUMBERSÚ	bytearrayÚappend)ÚintegerÚprefix_bitsÚ
max_numberÚelementss       r   Úencode_integerr0   8   sÈ   € ô
 ‡I�IÐ(¨'Ô?à�ƒ{ÜØ7¸'ÑAó
ð 	
ð �Qƒ˜+¨›/ÜØ9¸KÑGó
ð 	
ô )¨Ñ5€JàÓÜ˜'˜Ó#Ð#à�<ˆØÑˆà˜‹nØ�O‰O˜W s™]¨cÑ1Ô2Ø˜‰MˆGð ˜�nð 	�‰˜Ô ä˜Ó"Ð"r   c                 ó:  • US:  d  US:”  a  [        SU-  5      e[        U   nSnSnSSU-
  -	  n U S   U-  nXb:X  a*   X   nUS-  nUS:¼  a  XgS-
  U-  -  nOXgU-  -  nOUS-  nM)  [        R                  S	Xc5        Xc4$ ! [         a    [        SU -  5      ef = f)
zà
This decodes an integer according to the wacky integer encoding rules
defined in the HPACK spec. Returns a tuple of the decoded integer and the
number of bytes that were consumed from ``data`` in order to get that
integer.
r   r!   r"   r   éÿ   r#   r%   z5Unable to decode HPACK integer representation from %rzDecoded %d, consumed %d bytes)r(   r)   Ú
IndexErrorr   r&   r'   )Údatar-   r.   ÚindexÚshiftÚmaskÚnumberÚ	next_bytes           r   Údecode_integerr:   Z   së   € ð �Qƒ˜+¨›/ÜØ9¸KÑGó
ð 	
ô )¨Ñ5€JØ€EØ€EØ�Q˜‘_Ñ%€Dð
Ø�a‘˜4‘ˆØÓØØ ™K�	Ø˜‘
�à Ó#Ø¨3™°5Ñ8Ñ8‘Fà¨5Ñ0Ñ0�FØØ˜‘
�ñ ô  ‡I�IÐ-¨vÔ=àˆ=Ðøô ó 
Ü ØCÀdÑJó
ð 	
ð
ús   ±/B Á!B ÂBc              #   óŽ   #   • [        U [        5      (       d   e[        U R                  5       S S9nU H  nX U   4v •  M     g7f)zž
This converts a dictionary to an iterable of two-tuples. This is a
HPACK-specific function because it pulls "special-headers" out first and
then emits them.
c                 ó@   • [        U 5      R                  S5      (       + $ )Nó   :)Ú	_to_bytesÚ
startswith)Úks    r   Ú<lambda>Ú#_dict_to_iterable.<locals>.<lambda>Œ   s   € œ) A›,×1Ñ1°$Ó7Õ7r   )ÚkeyN)Ú
isinstanceÚdictÚsortedÚkeys)Úheader_dictrG   rC   s      r   Ú_dict_to_iterablerI   ƒ   sM   é € ô �k¤4×(Ñ(Ð(Ð(ÜØ×ÑÓÙ7ñ€Dó ˆØ˜sÑ#Ð#Ô#ò ùs   ‚AAc                 ó’   • [        U [        5      (       d  [        U 5      n [        U [        5      (       a  U $ U R	                  S5      $ )z
Convert string to bytes.
r   )rD   Ú
basestringÚstrr   Úencode)Ústrings    r   r>   r>   ’   s:   € ô �fœj×)Ñ)Ü�V“ˆä ¬×.Ñ.ˆ6ÐJ°F·M±MÀ'Ó4JÐJr   c                   ó†   • \ rS rSrSrS r\S 5       r\R                  S 5       rSS jr	SS jr
S rSS	 jrSS
 jrS rSrg)ÚEncoderéœ   za
An HPACK encoder object. This object takes HTTP headers and emits encoded
HTTP/2 header blocks.
c                 ób   • [        5       U l        [        [        [        5      U l        / U l        g ©N)r   Úheader_tabler	   r
   r   Úhuffman_coderÚtable_size_changes©Úselfs    r   Ú__init__ÚEncoder.__init__¢   s)   € Ü'›MˆÔÜ+ÜÔ/ó
ˆÔð #%ˆÕr   c                 ó.   • U R                   R                  $ ©z.
Controls the size of the HPACK header table.
©rT   ÚmaxsizerW   s    r   Úheader_table_sizeÚEncoder.header_table_size©   ó   € ð
 × Ñ ×(Ñ(Ð(r   c                 ó’   • XR                   l        U R                   R                  (       a  U R                  R	                  U5        g g rS   )rT   r^   ÚresizedrV   r+   ©rX   r   s     r   r_   r`   °   s6   € à$)×ÑÔ!Ø×Ñ×$×$Ø×#Ñ#×*Ñ*¨5Õ1ð %r   c                 ó4  • / n[        U[        5      (       a  [        U5      nU R                  R                  (       a0  UR                  U R                  5       5        SU R                  l        U H}  nSn[        U[        5      (       a  UR                  (       + nO[        U5      S:”  a  US   n[        US   5      [        US   5      4nUR                  U R                  XEU5      5        M     SR                  U5      n[        R                  SU5        U$ )ah  
Takes a set of headers and encodes them into a HPACK-encoded header
block.

:param headers: The headers to encode. Must be either an iterable of
                tuples, an iterable of :class:`HeaderTuple
                <hpack.HeaderTuple>`, or a ``dict``.

                If an iterable of tuples, the tuples may be either
                two-tuples or three-tuples. If they are two-tuples, the
                tuples must be of the format ``(name, value)``. If they
                are three-tuples, they must be of the format
                ``(name, value, sensitive)``, where ``sensitive`` is a
                boolean value indicating whether the header should be
                added to header tables anywhere. If not present,
                ``sensitive`` defaults to ``False``.

                If an iterable of :class:`HeaderTuple
                <hpack.HeaderTuple>`, the tuples must always be
                two-tuples. Instead of using ``sensitive`` as a third
                tuple entry, use :class:`NeverIndexedHeaderTuple
                <hpack.NeverIndexedHeaderTuple>` to request that
                the field never be indexed.

                .. warning:: HTTP/2 requires that all special headers
                    (headers whose names begin with ``:`` characters)
                    appear at the *start* of the header block. While
                    this method will ensure that happens for ``dict``
                    subclasses, callers using any other iterable of
                    tuples **must** ensure they place their special
                    headers at the start of the iterable.

                    For efficiency reasons users should prefer to use
                    iterables of two-tuples: fixing the ordering of
                    dictionary headers is an expensive operation that
                    should be avoided if possible.

:param huffman: (optional) Whether to Huffman-encode any header sent as
                a literal value. Except for use when debugging, it is
                recommended that this be left enabled.

:returns: A bytestring containing the HPACK-encoded header block.
Fr   r   r   r   zEncoded header block to %s)rD   rE   rI   rT   rc   r+   Ú_encode_table_size_changer   Ú	indexableÚlenr>   ÚaddÚjoinr&   r'   )rX   ÚheadersÚhuffmanÚheader_blockr   Ú	sensitives         r   rM   ÚEncoder.encode¶   só   € ðd ˆô
 �gœt×$Ñ$Ü'¨Ó0ˆGð ×Ñ×$×$Ø×Ñ × >Ñ >Ó @ÔAØ(-ˆD×ÑÔ%ó ˆFØˆIÜ˜&¤+×.Ñ.Ø &× 0Ñ 0Ô0‘	Ü�V“˜q“Ø" 1™I�	ä  q¡	Ó*¬I°f¸Q±iÓ,@ÐAˆFØ×Ñ §¡¨¸GÓ DÖEñ ð —x‘x Ó-ˆä�	‰	Ð.°Ô=àÐr   c                 ó²  • [         R                  SUUU5        Uu  pEU(       d  [        O[        nU R                  R                  XE5      nUc6  U R                  XEXc5      nU(       d  U R                  R                  XE5        U$ Uu  p”n
U
(       a  U R                  U	5      nU$ U R                  X•Xc5      nU(       d  U R                  R                  XE5        U$ )zA
This function takes a header key-value tuple and serializes it.
z7Adding %s to the header table, sensitive:%s, huffman:%s)
r&   r'   ÚINDEX_INCREMENTALÚINDEX_NEVERrT   ÚsearchÚ_encode_literalri   Ú_encode_indexedÚ_encode_indexed_literal)rX   Úto_addrn   rl   r   r   ÚindexbitÚmatchÚencodedr5   Úperfects              r   ri   ÚEncoder.add  sÙ   € ô 	�	‰	ØEØØØô		
ð ‰ˆö -6Õ$¼;ˆð ×!Ñ!×(Ñ(¨Ó5ˆà‰=ð ×*Ñ*¨4¸ÓJˆGÞØ×!Ñ!×%Ñ% dÔ2ØˆNð
  %Ñˆ�Wæà×*Ñ*¨5Ó1ˆGð ˆð ×2Ñ2Ø˜hóˆGö Ø×!Ñ!×%Ñ% dÔ2àˆr   c                 óJ   • [        US5      nUS==   S-  ss'   [        U5      $ )z4
Encodes a header using the indexed representation.
r%   r   r#   )r0   r   )rX   r5   Úfields      r   ru   ÚEncoder._encode_indexed8  s(   € ô ˜u aÓ(ˆØˆa‹�DÑ‹Ü�U‹|Ðr   c                 ób  • U(       a6  U R                   R                  U5      nU R                   R                  U5      n[        [        U5      S5      n[        [        U5      S5      nU(       a  US==   S-  ss'   US==   S-  ss'   SR	                  U[        U5      U[        U5      U/5      $ )z—
Encodes a header with a literal name and literal value. If ``indexing``
is True, the header will be added to the header table: otherwise it
will not.
r%   r   r#   r   )rU   rM   r0   rh   rj   r   )rX   r   r   rx   rl   Úname_lenÚ	value_lens          r   rt   ÚEncoder._encode_literal@  sš   € ö Ø×%Ñ%×,Ñ,¨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ó
ð 	
r   c                 óV  • U[         :w  a  [        US5      nO[        US5      nUS==   [        U5      -  ss'   U(       a  U R                  R	                  U5      n[        [        U5      S5      nU(       a  US==   S-  ss'   SR                  [        U5      [        U5      U/5      $ )z^
Encodes a header with an indexed name and a literal value and performs
incremental indexing.
é   é   r   r%   r#   r   )rq   r0   ÚordrU   rM   rh   rj   r   )rX   r5   r   rx   rl   Úprefixr‚   s          r   rv   ÚEncoder._encode_indexed_literalU  s�   € ð
 Ô(Ó(Ü# 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                 óŒ   • SnU R                    H*  n[        US5      nUS==   S-  ss'   U[        U5      -  nM,     / U l         U$ )zL
Produces the encoded form of all header table size change context
updates.
r   é   r   é    )rV   r0   r   )rX   ÚblockÚ
size_bytess      r   rf   Ú!Encoder._encode_table_size_changek  sQ   € ð
 ˆØ×1Ô1ˆJÜ'¨
°AÓ6ˆJØ�q‹M˜TÑ!‹MØ”U˜:Ó&Ñ&ŠEñ 2ð #%ˆÔØˆr   )rT   rU   rV   N)T©F)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rY   Úpropertyr_   ÚsetterrM   ri   ru   rt   rv   rf   Ú__static_attributes__© r   r   rP   rP   œ   s\   † ñò
%ð ñ)ó ð)ð ×Ññ2ó ð2ô
Oôb/òbô
ô*Bõ,r   rP   c                   ó†   • \ rS rSrSr\4S jr\S 5       r\R                  S 5       rSS jr
S rS rS	 rS
 rS rS rSrg)ÚDecoderiy  aÐ  
An HPACK decoder object.

.. versionchanged:: 2.3.0
   Added ``max_header_list_size`` argument.

:param max_header_list_size: The maximum decompressed size we will allow
    for any single header block. This is a protection against DoS attacks
    that attempt to force the application to expand a relatively small
    amount of data into a really large header list, allowing enormous
    amounts of memory to be allocated.

    If this amount of data is exceeded, a `OversizedHeaderListError
    <hpack.OversizedHeaderListError>` exception will be raised. At this
    point the connection should be shut down, as the HPACK state will no
    longer be usable.

    Defaults to 64kB.
:type max_header_list_size: ``int``
c                 ód   • [        5       U l        Xl        U R                  R                  U l        g rS   )r   rT   Úmax_header_list_sizer^   Úmax_allowed_table_size)rX   r�   s     r   rY   ÚDecoder.__init__Ž  s*   € Ü'›MˆÔð  %9Ô!ð '+×&7Ñ&7×&?Ñ&?ˆÕ#r   c                 ó.   • U R                   R                  $ r\   r]   rW   s    r   r_   ÚDecoder.header_table_sizeª  ra   r   c                 ó$   • XR                   l        g rS   r]   rd   s     r   r_   r¡   ±  s   € à$)×ÑÕ!r   c                 ó  • [         R                  SU5        [        U5      n/ n[        U5      nSnSnXu:  Ga  X   nUS-  (       a  SOSn	US-  (       a  SOSn
US-  (       a  SOSnU	(       a  U R	                  X7S 5      u  pÍOaU
(       a  U R                  X7S 5      u  pÍODU(       a(  U(       a  [        S	5      eU R                  X7S 5      nSnOU R                  X7S 5      u  pÍU(       aC  UR                  U5        U[        U6 -  nX`R                  :”  a  [        S
U R                  -  5      eX}-  nXu:  a  GM  U R                  5          U Vs/ s H  n[        Xâ5      PM     sn$ s  snf ! [         a    [        S5      ef = f)aŠ  
Takes an HPACK-encoded header block and decodes it into a header set.

:param data: A bytestring representing a complete HPACK-encoded header
             block.
:param raw: (optional) Whether to return the headers as tuples of raw
            byte strings or to decode them as UTF-8 before returning
            them. The default value is False, which returns tuples of
            Unicode strings
:returns: A list of two-tuples of ``(name, value)`` representing the
          HPACK-encoded headers, in the order they were decoded.
:raises HPACKDecodingError: If an error is encountered while decoding
                            the header block.
zDecoding %sr   r#   TFé@   rŒ   Nz/Table size update not at the start of the blockz.A header list larger than %d has been receivedz"Unable to decode headers as UTF-8.)r&   r'   Ú
memoryviewrh   Ú_decode_indexedÚ_decode_literal_indexr   Ú_update_encoding_contextÚ_decode_literal_no_indexr+   r   r�   r   Ú_assert_valid_table_sizer   ÚUnicodeDecodeError)rX   r4   r   Údata_memrk   Údata_lenÚinflated_sizeÚcurrent_indexÚcurrentÚindexedÚliteral_indexÚencoding_updater   ÚconsumedÚhs                  r   r   ÚDecoder.decodeµ  s·  € ô 	�	‰	�- Ô&ä˜dÓ#ˆØˆÜ�t“9ˆØˆØˆàÔ&ð Ñ)ˆGØ%¨Ÿn‘d°%ˆGð %,¨d§N™D¸ˆMð '.°§n™d¸%ˆOæØ#'×#7Ñ#7Ø˜^Ð,ó$Ñ �˜ö à#'×#=Ñ#=Ø˜^Ð,ó$Ñ �˜ö !ö Ü,ØIóð ð  ×8Ñ8Ø˜^Ð,ó�ð ‘ð $(×#@Ñ#@Ø˜^Ð,ó$Ñ �ö Ø—‘˜vÔ&ØÔ!1°6Ð!:Ñ:�à ×#<Ñ#<Ó<Ü2ØHØ×1Ñ1ñ2óð ð
 Ñ%ˆMðe Ö&ðn 	×%Ñ%Ô'ð	KÙ8?Ó@º°1Ô& qÖ.¹Ñ@Ð@ùÒ@øÜ!ó 	KÜ$Ð%IÓJÐJð	Kús   ÅE- ÅE(Å%E- Å(E- Å-Fc                 óN   • U R                   U R                  :”  a  [        S5      eg)z[
Check that the table size set by the encoder is lower than the maximum
we expect to have.
z3Encoder did not shrink table size to within the maxN)r_   rž   r   rW   s    r   rª   Ú Decoder._assert_valid_table_size
  s.   € ð
 ×!Ñ! D×$?Ñ$?Ó?Ü'ØEóð ð @r   c                 ób   • [        US5      u  p#X R                  :”  a  [        S5      eX l        U$ )z3
Handles a byte that updates the encoding context.
r‹   z)Encoder exceeded max allowable table size)r:   rž   r   r_   )rX   r4   Únew_sizer´   s       r   r¨   Ú Decoder._update_encoding_context  s<   € ô
 ,¨D°!Ó4ÑˆØ×1Ñ1Ó1Ü'Ø;óð ð "*ÔØˆr   c                 ó’   • [        US5      u  p#[        U R                  R                  U5      6 n[        R                  SXC5        XC4$ )z@
Decodes a header represented using the indexed representation.
r%   zDecoded %s, consumed %d)r:   r   rT   Úget_by_indexr&   r'   )rX   r4   r5   r´   r   s        r   r¦   ÚDecoder._decode_indexed!  sE   € ô )¨¨qÓ1‰ˆÜ˜d×/Ñ/×<Ñ<¸UÓCÐDˆÜ�	‰	Ð+¨VÔ>ØÐÐr   c                 ó&   • U R                  US5      $ )NF©Ú_decode_literal©rX   r4   s     r   r©   Ú Decoder._decode_literal_no_index*  s   € Ø×#Ñ# D¨%Ó0Ð0r   c                 ó&   • U R                  US5      $ )NTrÀ   rÂ   s     r   r§   ÚDecoder._decode_literal_index-  s   € Ø×#Ñ# D¨$Ó/Ð/r   c                 óÀ  • SnU(       a  US   S-  nSnSnOUS   nUS-  nSnUS-  nU(       a0  [        X5      u  p‰U R                  R                  U5      S   n
U	nSnORUSS	 n[        US
5      u  p¹XX›-    n
[        U
5      U:w  a  [	        S5      eUS   S-  (       a  [        U
5      n
X›-   S-   nXU-   S	 n[        US
5      u  p¹XX›-    n[        U5      U:w  a  [	        S5      eUS   S-  (       a  [        U5      nX;U	-   -  nU(       a  [        X¬5      nO[        X¬5      nU(       a  U R                  R                  X¬5        [        R                  SUUU5        XÓ4$ )z.
Decodes a header represented with a literal.
r   é?   r†   Fé   r…   é   r   Nr%   zTruncated header blockr#   z/Decoded %s, total consumed %d bytes, indexed %s)r:   rT   r½   rh   r   r   r   r   ri   r&   r'   )rX   r4   Úshould_indexÚtotal_consumedÚindexed_namer�   Únot_indexableÚ	high_byter5   r´   r   Úlengthr   r   s                 r   rÁ   ÚDecoder._decode_literal0  s�  € ð ˆö Ø ™7 T™>ˆLØˆHØ!‰Mà˜Q™ˆIØ$ tÑ+ˆLØˆHØ%¨Ñ,ˆMæä,¨TÓ<‰OˆEØ×$Ñ$×1Ñ1°%Ó8¸Ñ;ˆDà%ˆNØ‰Fð ˜˜�8ˆDä-¨d°AÓ6ÑˆFØ Ñ!2Ð3ˆDÜ�4‹y˜FÓ"Ü(Ð)AÓBÐBà�A‰w˜�~Ü% dÓ+�Ø%Ñ.°Ñ2ˆNà˜vÑ%Ð&Ð'ˆô *¨$°Ó2ÑˆØ˜hÑ/Ð0ˆÜˆu‹:˜ÓÜ$Ð%=Ó>Ð>à�‰7�T�>Ü" 5Ó)ˆEð 	 8Ñ+Ñ+ˆö Ü,¨TÓ9‰Fä  Ó-ˆFö Ø×Ñ×!Ñ! $Ô.ä�	‰	Ø=ØØØô		
ð Ð%Ð%r   )rT   r_   rž   r�   Nr�   )r‘   r’   r“   r”   r•   ÚDEFAULT_MAX_HEADER_LIST_SIZErY   r–   r_   r—   r   rª   r¨   r¦   r©   r§   rÁ   r˜   r™   r   r   r›   r›   y  sh   † ñð( -Iô @ð8 ñ)ó ð)ð ×Ññ*ó ð*ôSKòjòò ò1ò0õI&r   r›   )'r•   ÚloggingÚtabler   r   Ú
exceptionsr   r   r   rl   r	   Úhuffman_constantsr
   r   Úhuffman_tabler   Ústructr   r   Ú	getLoggerr‘   r&   Ú
INDEX_NONErr   rq   Úranger)   rK   Ú	NameErrorrL   r   rÑ   r   r0   r:   rI   r>   rP   r›   )Úis   0r   Ú<module>rÝ      sØ   ðñó ç 0÷ñ õ $÷õ *ß 8à×Ò˜Ó!€à€
Ø€ØÐ ñ
 27°q´Ó:²¨A˜A ™F aœ<±Ñ:Ð ðØ€Jð  'Ð ò
)ò#òD&òR$òK÷Zñ Z÷z@&ò @&ùòw
 ;øð ó Ø�u�‚Jðús   ÁBÁ-B Â
B'Â&B'