
    vKgX                     R   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       K/var/www/highfloat_scraper/venv/lib/python3.13/site-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    
 II('?{7'A
 	
 Q+/9KG
 	
 )5J'##<nOOWs]c12MG 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+/9KG
 	
 )5JEEQ_%D
a4 K	
#3588F500F
   II-v==  
 Cd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      k4((((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:     fj))V..6JFMM'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 
 gt$$'0G $$ > > @A(-D% FI&+.. & 0 00	Vq"1I	q	*IfQi,@AFG DE  xx-		.=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  pn
U
(       a  U R                  U	5      nU$ U R                  XXc5      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JG!!%%d2N
  %W**51G  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(     ua(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2D&&--e4E!#d)Q/"3u:q1	QK4KaLD LxxuXeI.>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    
 ((#E1-F#E1-Fq	S]"	&&--e4E"3u:q1	aLD Lxx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1J'
A6JqMT!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  pOaU
(       a  U R                  X7S 5      u  pODU(       a(  U(       a  [        S	5      eU R                  X7S 5      nSnOU R                  X7S 5      u  pU(       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[        X5      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#t9& )G%nd%G %,dNDM '.nd%O#'#7#7^,$  #'#=#=^,$  ! ,I   88^,  $(#@#@^,$  v&!16!:: #<#<<2H112 
 %Me &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  pU R                  R                  U5      S   n
U	nSnORUSS	 n[        US
5      u  pX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  pXX-    n[        U5      U:w  a  [	        S5      eUS   S-  (       a  [        U5      nX;U	-   -  nU(       a  [        X5      nO[        X5      nU(       a  U R                  R                  X5        [        R                  SUUU5        X4$ )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     7T>LH!MQI$t+LH%,M,T<OE$$11%8;D%NF 8D-dA6F!23D4yF"()ABBAw~%d+%.2Nv%&' *$2h/0u:$%=>>7T>"5)E 	8++ ,T9F -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!
 
 27q:AAFa<: J  ' 
)#D&R$KZ Zz@& @&w
 ;  uJs   B,B 
B&%B&