
    vKgZ                        S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	J
r
  \R                  " S5      r\" / SQ5      r\" / S	Q5      r\" / S
Q5      r\" / SQ5      r\" SS/5      r\" SS/5      r\" \" \\5      5      rS rS rS rS rS r\R8                  " S/ SQ5      rS rS rS r S r!S r"S r#S r$S r%S r&S  r'S! r(S" r)S# r*S$ r+S% r,S& r-S' r.S( r/S) r0S* r1S+ r2 " S, S-\Rf                  5      r4g).zW
h2/utilities
~~~~~~~~~~~~

Utility functions that do not belong in a separate module.
    N)
whitespace)HeaderTupleNeverIndexedHeaderTuple   )ProtocolErrorFlowControlErrors   [A-Z])
s
   connection
connections   proxy-connectionzproxy-connections
   keep-alivez
keep-alives   transfer-encodingztransfer-encodings   upgradeupgrade)   :method:method   :scheme:scheme
   :authority
:authority   :path:path   :status:status	   :protocol	:protocol)s   authorizationauthorizations   proxy-authorizationzproxy-authorization)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   c              #      #    U  HG  nUS   [         ;   a  [        U6 v   M  US   S;   a  [        US   5      S:  a  [        U6 v   MC  Uv   MI     g7f)a^  
Certain headers are at risk of being attacked during the header compression
phase, and so need to be kept out of header compression contexts. This
function automatically transforms certain specific headers into HPACK
never-indexed fields to ensure they don't get added to header compression
contexts.

This function currently implements two rules:

- 'authorization' and 'proxy-authorization' fields are automatically made
  never-indexed.
- Any 'cookie' header field shorter than 20 bytes long is made
  never-indexed.

These fields are the most at-risk. These rules are inspired by Firefox
and nghttp2.
r   )   cookiecookier      N)_SECURE_HEADERSr   lenheadershdr_validation_flagsheaders      L/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/h2/utilities.py_secure_headersr#   B   sT     $ !9')622AY00S^b5H)622L s   AAc                 ~    U  H7  u  pUS;   d  M  [        U[        5      (       d  UR                  S5      s  $ Us  $    g)z4
Extracts the request method from the headers list.
r   r   utf-8N
isinstancebytesencode)r   kvs      r"   extract_method_headerr-   ]   s:     ((a''xx((     c                     U  HW  u  p[        U[        5      (       a  SnSnSnOSnSnSnUR                  U5      (       d    gX:w  a  MF  UR                  U5      s  $    g)	a  
Searches a header block for a :status header to confirm that a given
collection of headers are an informational response. Assumes the header
block is well formed: that is, that the HTTP/2 special headers are first
in the block, and so that it can stop looking when it finds the first
header field whose name does not begin with a colon.

:param headers: The HTTP/2 header block.
:returns: A boolean indicating if this is an informational response.
   :r      1:r   1FNr(   r)   
startswith)r   nr,   sigilstatusinformational_starts         r"   is_informational_responser:   i   sm     aEF"&EF"& ||E"" ; ||/00' r.   c                 8    SnX-   nX2:  a  [        SU-  5      eU$ )a  
Increments a flow control window, guarding against that window becoming too
large.

:param current: The current value of the flow control window.
:param increment: The increment to apply to that window.
:returns: The new value of the window.
:raises: ``FlowControlError``
iz-May not increment flow control window past %d)r   )current	incrementLARGEST_FLOW_CONTROL_WINDOWnew_sizes       r"   guard_increment_windowr@      s7     #,"H-;'(
 	

 Or.   c                 ~    U  H7  u  pUS;   d  M  [        U[        5      (       d  UR                  S5      s  $ Us  $    g)a  
Given a header set, searches for the authority header and returns the
value.

Note that this doesn't terminate early, so should only be called if the
headers are for a client request. Otherwise, will loop over the entire
header set, which is potentially unwise.

:param headers: The HTTP header set.
:returns: The value of the authority header, or ``None``.
:rtype: ``bytes`` or ``None``.
r   r   r&   Nr'   )r   r6   r,   s      r"   authority_from_headersrC      s@      ..,6q%,@,@188G$GaG  r.   HeaderValidationFlags)	is_client
is_traileris_response_headeris_push_promisec                     [        X5      n [        X5      n [        X5      n [        X5      n [	        X5      n [        X5      n [        X5      n [        X5      n U $ )z
Validates a header sequence against a set of constraints from RFC 7540.

:param headers: The HTTP header set.
:param hdr_validation_flags: An instance of HeaderValidationFlags.
)_reject_empty_header_names_reject_uppercase_header_fields_reject_surrounding_whitespace
_reject_te_reject_connection_header_reject_pseudo_header_fields_check_host_authority_header_check_path_headerr   r    s     r"   validate_headersrS      s|      )G .G -G G (G +G +G !?GNr.   c              #   `   #    U  H$  n[        US   5      S:X  a  [        S5      eUv   M&     g7f)z
Raises a ProtocolError if any header names are empty (length 0).
While hpack decodes such headers without errors, they are semantically
forbidden in HTTP, see RFC 7230, stating that they must be at least one
character long.
r   z&Received header name with zero length.N)r   r   r   s      r"   rJ   rJ      s2      vay>Q HII s   ,.c              #      #    U  H5  n[         R                  US   5      (       a  [        SUS   -  5      eUv   M7     g7f)zO
Raises a ProtocolError if any uppercase character is found in a header
block.
r   z"Received uppercase header name %s.N)UPPER_REsearchr   r   s      r"   rK   rK      sD     
 ??6!9%%4vay@B B	 s   =?c              #      #    U  Hs  nUS   S   [         ;   d  US   S   [         ;   a  [        SUS   -  5      eUS   (       a1  US   S   [         ;   d  US   S   [         ;   a  [        SUS   -  5      eUv   Mu     g7f)z\
Raises a ProtocolError if any header name or value is surrounded by
whitespace characters.
r   z0Received header name surrounded by whitespace %rr   z1Received header value surrounded by whitespace %rN)_WHITESPACEr   r   s      r"   rL   rL     s      !9Q<;&&)B-;*FBVAYNP P!96!9Q<;61IbM[(CfQiO   s   A;A=c              #      #    U  H8  nUS   S;   a(  US   R                  5       S;  a  [        SUS   -  5      eUv   M:     g7f)zw
Raises a ProtocolError if the TE header is present in a header block and
its value is anything other than "trailers".
r   )s   teter   )s   trailerstrailerszInvalid value for TE header: %sN)lowerr   r   s      r"   rM   rM     sT     
 !9&ay (BB#51I 
  s   A Ac              #   b   #    U  H%  nUS   [         ;   a  [        SUS   -  5      eUv   M'     g7f)zO
Raises a ProtocolError if the Connection header is present in a header
block.
r   z-Connection-specific header field present: %s.N)CONNECTION_HEADERSr   r   s      r"   rN   rN   *  s>     
 !9**?&)K   s   -/c                 p    [        U [        5      (       a  U R                  U5      $ U R                  U5      $ )zv
Given a string that might be a bytestring or a Unicode string,
return True if it starts with the appropriate prefix.
r4   )test_stringbytes_prefixunicode_prefixs      r"   _custom_startswithre   8  s3    
 +u%%%%l33%%n55r.   c                 6    X;   d  X;   d  [        SU -  5      egg)z
Given a set of header names, checks whether the string or byte version of
the header name is present. Raises a Protocol error with the appropriate
error if it's missing.
z(Header block missing mandatory %s headerNr   )string_headerbytes_header
header_sets      r"   _assert_header_in_setrk   C  s-     '<+E6F
 	
 ,F'r.   c              #     #    [        5       nSnSnU  H  n[        US   SS5      (       a  US   U;   a  [        SUS   -  5      eUR                  US   5        U(       a  [        SUS   -  5      eUS   [        ;  a  [        SUS   -  5      eUS   S	;   a2  [        US
   [        5      (       d  US
   R                  S5      nOUS
   nOSnUv   M     [        X$U5        g7f)z
Raises a ProtocolError if duplicate pseudo-header fields are found in a
header block or if a pseudo-header field appears in a block after an
ordinary header field.

Raises a ProtocolError if pseudo-header fields are found in trailers.
FNr   r0   r2   z)Received duplicate pseudo-header field %sz0Received pseudo-header field out of sequence: %sz&Received custom pseudo-header field %sr%   r   r&   T)	setre   r   add_ALLOWED_PSEUDO_HEADER_FIELDSr(   r)   r*   (_check_pseudo_header_field_acceptability)r   r    seen_pseudo_header_fieldsseen_regular_headermethodr!   s         r"   rO   rO   O  s     !$FfQit44ay55#?&)K  &))&)4"#F1I 
 ay ==#<vayH  ay44!&)U33#AY--g6F#AYF #'; @ -!+?s   C C"c                    UR                   (       a  U (       a  [        SU -  5      eUR                  (       a,  [        SSU 5        U [        -  nU(       a  [        SU-  5      egUR                  (       d}  UR                   (       dk  [        SSU 5        [        SSU 5        [        S	S
U 5        U [
        -  nU(       a  [        SU-  5      eUS:w  a  U [        -  nU(       a  [        SU-  5      egggg)zy
Given the set of pseudo-headers present in a header block and the
validation flags, confirms that RFC 7540 allows them.
z$Received pseudo-header in trailer %sr   r   z#Encountered request-only headers %sr   r   r   r   r   r   z$Encountered response-only headers %ss   CONNECTz+Encountered connect-request-only headers %sN)rF   r   rG   rk   _REQUEST_ONLY_HEADERS_RESPONSE_ONLY_HEADERS_CONNECT_REQUEST_ONLY_HEADERS)pseudo_headersrs   r    invalid_response_headersinvalid_request_headersinvalid_headerss         r"   rp   rp     s    &&>2^C
 	
 ..j*nE#14I#I #5()  $
 #55"-- 	h.Aj*nEj*nE"03I"I"6'(  Z,/LLO#A#$     . 6r.   c              #      #    SnSnU  H$  nUS   S;   a  US   nOUS   S;   a  US   nUv   M&     USLnUSLnU(       d  U(       d  [        S5      eU(       a!  U(       a  X:w  a  [        SU< SU< 35      eggg7f)	z
Given the :authority and Host headers from a request block that isn't
a trailer, check that:
 1. At least one of these headers is set.
 2. If both headers are set, they match.

:param headers: The HTTP header set.
:raises: ``ProtocolError``
Nr   rB   r   )s   hosthostz@Request header block does not have an :authority or Host header.zARequest header block has mismatched :authority and Host headers: z / rg   )r   authority_header_valhost_header_valr!   authority_presenthost_presents         r"   _validate_host_authority_headerr     s     "  O!966#)!9 AY,,$QiO  .T9#4/L \N
 	

 \2 (:  3 *s   A9A;c                 h    UR                   =(       d    UR                  nU(       a  U $ [        U 5      $ )z
Raises a ProtocolError if a header block arrives that does not contain an
:authority or a Host header, or if a header block contains both fields,
but their values do not match.
rG   rF   r   r   r    skip_validations      r"   rP   rP     5     	// 	(''  *733r.   c                 n   ^  U 4S jnUR                   =(       d    UR                  nU(       a  T $ U" 5       $ )za
Raise a ProtocolError if a header block arrives or is sent that contains an
empty :path header.
c               3   d   >#    T H%  n U S   S;   a  U S   (       d  [        S5      eU v   M'     g 7f)Nr   )r   r   r   z"An empty :path header is forbiddenrg   )r!   r   s    r"   inner!_check_path_header.<locals>.inner  s5     Fay00ay'(LMML s   -0)rG   rF   )r   r    r   r   s   `   r"   rQ   rQ     s7    
 	// 	(''  wr.   c              #      #    U  H\  n[        U[        5      (       a*  UR                  US   R                  5       US   5      v   MB  US   R                  5       US   4v   M^     g7f)z
Given an iterable of header two-tuples, rebuilds that iterable with the
header names lowercased. This generator produces tuples that preserve the
original type of the header tuple for tuple and any ``HeaderTuple``.
r   r   N)r(   r   	__class__r^   r   s      r"   _lowercase_header_namesr     sZ      fk**""6!9??#4fQi@@!9??$fQi00	 s   A$A&c              #     #    U  Hx  n[        U[        5      (       a8  UR                  US   R                  5       US   R                  5       5      v   MP  US   R                  5       US   R                  5       4v   Mz     g7f)z
Given an iterable of header two-tuples, strip both leading and trailing
whitespace from both header names and header values. This generator
produces tuples that preserve the original type of the header tuple for
tuple and any ``HeaderTuple``.
r   r   N)r(   r   r   stripr   s      r"   _strip_surrounding_whitespacer     sh      fk**""6!9??#4fQioo6GHH!9??$fQioo&788	 s   B Bc              #   D   #    U  H  nUS   [         ;  d  M  Uv   M     g7f)u9   
Strip any connection headers as per RFC7540 § 8.1.2.2.
r   N)r`   r   s      r"   _strip_connection_headersr   +  s#      !9..L s    	 c                 h    UR                   =(       d    UR                  nU(       a  U $ [        U 5      $ )z
Raises an InvalidHeaderBlockError if we try to send a header block
that does not contain an :authority or a Host header, or if
the header block contains both fields, but their values do not match.
r   r   s      r"   !_check_sent_host_authority_headerr   4  r   r.   c              #      #    / nU  H&  nUS   S:X  a  UR                  US   5        M"  Uv   M(     U(       a   SR                  U5      n[        SU5      v   gg7f)u[  
RFC 7540 § 8.1.2.5 allows HTTP/2 clients to split the Cookie header field,
which must normally appear only once, into multiple fields for better
compression. However, they MUST be joined back up again when received.
This normalization step applies that transform. The side-effect is that
all cookie fields now appear *last* in the header block.
r   r   r   s   ; N)appendjoinr   )r   r    cookiesr!   
cookie_vals        r"   _combine_cookie_fieldsr   G  s\      G!9	!NN6!9%L	 
 ZZ(
%i<< s   AAc                 ^    [        X5      n [        X5      n [        X5      n [        X5      n U $ )z
Normalizes a header sequence that we are about to send.

:param headers: The HTTP header set.
:param hdr_validation_flags: An instance of HeaderValidationFlags.
)r   r   r   r#   rR   s     r"   normalize_outbound_headersr   ^  s1     &gDG+GJG'FGg<GNr.   c                     [        X5      n U $ )z
Normalizes a header sequence that we have received.

:param headers: The HTTP header set.
:param hdr_validation_flags: An instance of HeaderValidationFlags
)r   rR   s     r"   normalize_inbound_headersr   m  s     %WCGNr.   c                 t    [        X5      n [        X5      n [        X5      n [        X5      n [	        X5      n U $ )z
Validates and normalizes a header sequence that we are about to send.

:param headers: The HTTP header set.
:param hdr_validation_flags: An instance of HeaderValidationFlags.
)rM   rN   rO   r   rQ   rR   s     r"   validate_outbound_headersr   x  sO     G (G +G 0G !?GNr.   c                   :   ^  \ rS rSrU 4S jrU 4S jrS rSrU =r$ )SizeLimitDicti  c                 |   > UR                  SS 5      U l        [        [        U ]  " U0 UD6  U R                  5         g )N
size_limit)pop_size_limitsuperr   __init___check_size_limit)selfargskwargsr   s      r"   r   SizeLimitDict.__init__  s6    !::lD9mT+T<V< r.   c                 L   > [         [        U ]  X5        U R                  5         g )N)r   r   __setitem__r   )r   keyvaluer   s      r"   r   SizeLimitDict.__setitem__  s    mT.s: r.   c                     U R                   bE  [        U 5      U R                   :  a+  U R                  SS9  [        U 5      U R                   :  a  M*  g g g )NF)last)r   r   popitem)r   s    r"   r   SizeLimitDict._check_size_limit  sG    'd)d...%( d)d... (r.   )r   )	__name__
__module____qualname____firstlineno__r   r   r   __static_attributes____classcell__)r   s   @r"   r   r     s    !!
) )r.   r   )5__doc__collectionsrestringr   hpackr   r   
exceptionsr   r   compilerV   	frozensetr`   ro   r   ru   rv   rw   mapordrZ   r#   r-   r:   r@   rC   
namedtuplerD   rS   rJ   rK   rL   rM   rN   re   rk   rO   rp   r   rP   rQ   r   r   r   r   r   r   r   r   OrderedDictr    r.   r"   <module>r      sY    	  6 7::h      !* + !     " #   #J
#;< 
 !*<*F G  C,-6	1B02 $..H 'T
	, 6	
.b.b/d4&4
194&=.0)K++ )r.   