
    @OOf&4                     F   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d Z
d Z e j                  d	      Zd
eddfdZ	 ddededz  dedz  fdZdeddfdZdedeeeeeeeef   fdZdedeeeef   fdZdee   defdZdee   defdZdee   defdZy)    N)Iterable)Headers)Request)Response)urlc                 |    || vrg S | |   j                  d      }|D cg c]  }|j                          c}S c c}w )z
    Retrieve all tokens for a header key. A number of different headers
    follow a pattern where each header line can containe comma-separated
    tokens, and headers can be set multiple times.
    ,)splitstrip)headerskeytokenstokens       ^/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/net/http/http1/read.pyget_header_tokensr      s?     '	S\$F'-.eEKKM...s   9c                 >    d|v rt        |d      }d|v ryd|v ry| dvS )z
    Checks the message to see if the client connection should be closed
    according to RFC 2616 Section 8.1.
    If we don't have a Connection header, HTTP 1.1 connections are assumed
    to be persistent.
    
connectioncloseTz
keep-aliveF)zHTTP/1.1s   HTTP/1.1zHTTP/2.0s   HTTP/2.0)r   )http_versionr   r   s      r   connection_closer      s@     w"7L9fV#        s   ^[!#$%&'*+\-.^_`|~0-9a-zA-Z]+$r   returnc                     d}d}| j                   D ]K  \  }}t        j                  |      st        d|d      |j	                         }|xs |dk(  }|xs |dk(  }M |r|rt        d      yy)zi
    Validate headers to avoid request smuggling attacks. Raises a ValueError if they are malformed.
    Fz!Received an invalid header name: z. Invalid header names may introduce request smuggling vulnerabilities. Disable the validate_inbound_headers option to skip this security check.s   transfer-encodings   content-lengthzReceived both a Transfer-Encoding and a Content-Length header, refusing as recommended in RFC 7230 Section 3.3.3. See https://github.com/mitmproxy/mitmproxy/issues/4799 for details. Disable the validate_inbound_headers option to skip this security check.N)fields_valid_header_namematch
ValueErrorlower)r   te_foundcl_foundnamevalue
name_lowers         r   validate_headersr$   2   s    
 HH~~ 
?e!''-3D8 </ 0  ZZ\
Az-AA>z->>
? HW
 	
 xr   requestresponsec                 <   |s| j                   }n|j                   }| j                  j                         dk(  ryd|j                  cxk  rdk  ry |j                  dv ryd|j                  cxk  rdk  r!n n| j                  j                         dk(  ryd	|v r|d	   }|j	                         st        d
|      |j                         j                  d      }t        j                  dd|      }|dv ry|dv r|ryt        d      t        d|d	         d|v r\|j                  d      t        fdD              }|rt        d      	 t        d         }|dk  rt        dd         |S |syy# t
        $ r t        dd         w xY w)a>  
    Returns:
        The expected body length:
        - a positive integer, if the size is known in advance
        - None, if the size in unknown in advance (chunked encoding)
        - -1, if all data should be read until end of stream.

    Raises:
        ValueError, if the content length header is invalid
    HEADr   d      )   i0     i+  CONNECTztransfer-encodingzInvalid transfer encoding: z	 z[\t ]*,[\t ]*r	   )chunkedzcompress,chunkedzdeflate,chunkedzgzip,chunkedN)compressdeflategzipidentityzNInvalid request transfer encoding, message body cannot be determined reliably.zUnknown transfer encoding: zcontent-lengthc              3   .   K   | ]  }|d    k7    yw)r   N ).0xsizess     r   	<genexpr>z*expected_http_body_size.<locals>.<genexpr>   s     .LqE!H}.Ls   z$Conflicting Content-Length headers: zInvalid Content-Length header: z Negative Content-Length header: )r   methodupperstatus_codeisasciir   r   r   resubget_allanyint)r%   r&   r   te different_content_length_headerssizer8   s         @r   expected_http_body_sizerF   O   s    //"" >>!V+(&&-#- .:- (&&-#-'..2F2F2HI2U0 g% -.zz|:2&ABBXXZe$VV$c2. 
 
  
 
  d  -g6I.J-MN . 7" 01+..Le.L+L(+CE9MNN	MuQx=D !8?a|LMM    	M>uQxlKLL	Ms   F   Fr   c                 L    t        j                  d|       st        d|       y )Ns   ^HTTP/\d\.\d$zUnknown HTTP version: )r>   r   r   )r   s    r   raise_if_http_version_unknownrH      s*    88%|41,1ABCC 5r   linec                 2   	 | j                         \  }}}|dk(  s|j                  d      rdd|}}}d\  }}n|dk(  r(d|d}}}t        j                  |d      \  }}|st        |j                  dd	
      \  }}	|	j                  d      \  }}
}d|z   }t        j                  |d      \  }}|xs t        j                  |      }|st        t        j                  |       t        |       |||||||fS # t        $ r}t	        d|       |d }~ww xY w)N   *   /r   ) r   s   CONNECTT)checks   ://   )maxsplitzBad HTTP request line: )	r
   
startswithr   parse_authorityr   	partitiondefault_portparserH   )rI   r:   targetr   scheme	authoritypathhostportrest_path_es                r   _read_request_liner`      s6   D'+zz|$ T>V..t4&)3tIFJD$z!&)63tIF,,YdCJD$  !<<<;LFD"&.."6Iq%%<D,,YdCJD$33++F3D  IIf%l3 vvy$DD  D24(;<!CDs   C.C9 9	DDDc                     	 | j                  d d      }t        |      dk(  r|j                  d       |\  }}}t        |      }t	        |       |||fS # t
        $ r}t        d|       |d }~ww xY w)N   r   zBad HTTP response line: )r
   lenappendrB   rH   r   )rI   partsr   status_code_strreasonr<   r_   s          r   _read_response_linerh      s    	E

4#u:?LL05-ov/*%l3 f,,  E3D8<=1DEs   AA 	A1A,,A1linesc                 b   g }| D ]  }|d   dv r5|st        d      |d   d   |d   d   dz   |j                         z   f|d<   ?	 |j                  dd      \  }}|j                         }|s
t               |j                  ||f        t	        |      S # t         $ r t        d|      w xY w)	z
    Read a set of headers.
    Stop once a blank line is reached.

    Returns:
        A headers object

    Raises:
        exceptions.HttpSyntaxException
    r   s    	zInvalid headersr3   rO   s   
    :zInvalid header line: )r   r   r
   rd   r   )ri   retrI   r!   r"   s        r   _read_headersrm     s     &(C C7f !2332wqz3r71:#84::<#GHCGC"jjq1e$,&

D%=)C 3<  C #8!ABBCs   ABB.c                     t        | d         \  }}}}}}}t        | dd       }t        ||||||||ddt        j                         d      S )z
    Parse an HTTP request head (request line + headers) from an iterable of lines

    Args:
        lines: The input lines

    Returns:
        The HTTP request object (without body)

    Raises:
        ValueError: The input is malformed.
    r   rO   N)rZ   r[   r:   rW   rX   rY   r   r   contenttrailerstimestamp_starttimestamp_end)r`   rm   r   time)	ri   rZ   r[   r:   rW   rX   rY   r   r   s	            r   read_request_headrt   -  sp     ASaA=D$	4 E!"I&G!		 r   c           
          t        | d         \  }}}t        | dd       }t        ||||ddt        j                         d      S )z
    Parse an HTTP response head (response line + headers) from an iterable of lines

    Args:
        lines: The input lines

    Returns:
        The HTTP response object (without body)

    Raises:
        ValueError: The input is malformed.
    r   rO   N)r   r<   rg   r   ro   rp   rq   rr   )rh   rm   r   rs   )ri   r   r<   rg   r   s        r   read_response_headrv   O  sT     )<E!H(E%L+vE!"I&G!			 	r   )N)r>   rs   collections.abcr   mitmproxy.httpr   r   r   mitmproxy.net.httpr   r   r   compiler   r$   rB   rF   bytesrH   tuplestrr`   rh   rm   listrt   rv   r5   r   r   <module>r      s"   	  $ " " # "	/0  RZZ BC 
g 
$ 
< 37HH (4H4ZHVD D$ D
E
E
3UE5%67EB-e -eS%.?(@ -% W <T%[ W Dd5k h r   