
    Eg1                         d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d ZddZd ZddZd Zdd
Zd Z	 	 ddej        dej        e	j                 defdZd Zd Zd Zd Zd Zej        fdZdS )    N)
exceptions)headersrequestresponseurlc                 \    || vrg S | |                              d          }d |D             S )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.
    ,c                 6    g | ]}|                                 S  )strip).0tokens     j/var/www/sysmax/venv/lib/python3.11/site-packages/seleniumwire/thirdparty/mitmproxy/net/http/http1/read.py
<listcomp>z%get_header_tokens.<locals>.<listcomp>   s     ...eEKKMM...    )split)r   keytokenss      r   get_header_tokensr      s?     '	S\$$F..v....r   c                     t          |           }t          |          }d                    t          | ||                    |j        _        t          j                    |_        |S )Nr   )limit)read_request_headexpected_http_body_sizejoin	read_bodydatacontenttimetimestamp_end)rfilebody_size_limitr   expected_body_sizes       r   read_requestr#      sV    &&G09988Ie5G$_$_$_``GL IKKGNr   c                 (   t          j                     }t          | d          r|                                  t          |           \  }}}}}}}t	          |           }	t          | d          r| j        }t          j        ||||||||	dd|d          S )a  
    Parse an HTTP request head (request line + headers) from an input stream

    Args:
        rfile: The input stream

    Returns:
        The HTTP request object (without body)

    Raises:
        exceptions.HttpReadDisconnect: No bytes can be read from rfile.
        exceptions.HttpSyntaxException: The input is malformed HTTP.
        exceptions.HttpException: Any other error occurred.
    reset_timestampsfirst_byte_timestampN)r   hasattrr%   _read_request_line_read_headersr&   r   Request)
r    timestamp_starthostportmethodscheme	authoritypathhttp_versionr   s
             r   r   r      s     ikkOu()) !   @RSX@Y@Y=D$	4E""Gu,-- 54?dFFIt\7DRVXgim  r   c                     t          |           }t          ||          }d                    t          | ||                    |j        _        t          j                    |_        |S )Nr   )read_response_headr   r   r   r   r   r   r   )r    r   r!   r   r"   s        r   read_responser5   >   sU    !%((H0(CCHHYu6H/%Z%Z[[HM!Y[[HOr   c           
         t          j                     }t          | d          r|                                  t          |           \  }}}t	          |           }t          | d          r| j        }t          j        ||||dd|d          S )a  
    Parse an HTTP response head (response line + headers) from an input stream

    Args:
        rfile: The input stream

    Returns:
        The HTTP request object (without body)

    Raises:
        exceptions.HttpReadDisconnect: No bytes can be read from rfile.
        exceptions.HttpSyntaxException: The input is malformed HTTP.
        exceptions.HttpException: Any other error occurred.
    r%   r&   N)r   r'   r%   _read_response_liner)   r&   r   Response)r    r+   r2   status_codemessager   s         r   r4   r4   F   s      ikkOu()) !   )<U)C)C&L+wE""Gu,-- 54\;$PTVegklllr      c              #     K   |r|dk     rt           j        }|s|}|t          | |          D ]}|V  dS |dk    r|.||k    r(t          j        d                    ||                    |}|rYt          ||          }|                     |          }t          |          |k     rt          j        d          |V  ||z  }|WdS dS |}|r4t          ||          }|                     |          }|sdS |V  ||z  }|4|                     d          }|r't          j        d                    |                    dS )a  
        Read an HTTP message body

        Args:
            rfile: The input stream
            expected_size: The expected body size (see :py:meth:`expected_body_size`)
            limit: Maximum body size
            max_chunk_size: Maximium chunk size that gets yielded

        Returns:
            A generator that yields byte chunks of the content.

        Raises:
            exceptions.HttpException, if an error occurs

        Caveats:
            max_chunk_size is not considered if the transfer encoding is chunked.
    r   NzEHTTP Body too large. Limit is {}, content length was advertised as {}zUnexpected EOF   z!HTTP body too large. Limit is {}.)	sysmaxsize_read_chunkedr   HttpExceptionformatminreadlen)	r    expected_sizer   max_chunk_sizex
bytes_left
chunk_sizer   not_dones	            r   r   r   d   s     &  EAII ue,, 	 	AGGGG	 		!		!6!6*CCI6%Q^C_C_   #
 	%Z88Jjj,,G7||j(( ./?@@@MMM*$J  	% 	% 	% 	% 	% 
 	%Z88Jjj,,G MMM*$J  	% ::a== 	^*+N+U+UV[+\+\]]]	^ 	^r   c                 Z    d|v rt          |d          }d|v rdS d|v rdS | dk    o| dk    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-aliveFzHTTP/1.1s   HTTP/1.1)r   )r2   r   r   s      r   connection_closerO      sS     w"7L99f4V##5:%E,+*EEr   Tr   r   expect_continue_as_0c                    |s3| j         }|r) |j        dd                                          dk    rdS nr|j         }| j                                        dk    rdS d|j        cxk    rdk    rn ndS |j        dk    r| j                                        d	k    rdS |j        d
v rdS d |j        dd                                          v rdS d|v r	  |j        d          t          fdD                       }|rt          j	        d          t          d                   }|dk     rt                      |S # t          $ r}t          j	        d          |d}~ww xY w|sdS dS )a  
        Args:
            - expect_continue_as_0: If true, incorrectly predict a body size of 0 for requests which are waiting
              for a 100 Continue response.
        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:
            exceptions.HttpSyntaxException, if the content length header is invalid
    expect z100-continuer   HEADd         CONNECT)   i0  chunkedztransfer-encodingNzcontent-lengthc              3   0   K   | ]}|d          k    V  dS )r   Nr   )r   rH   sizess     r   	<genexpr>z*expected_http_body_size.<locals>.<genexpr>   s+      2P2PQ1a=2P2P2P2P2P2Pr   z"Conflicting Content Length HeaderszUnparseable Content Length)r   getlowerr.   upperr9   get_allanyr   HttpSyntaxExceptionint
ValueError)r   r   rP   r    different_content_length_headerssizeer\   s          @r   r   r      s   (  / 	KGK"$=$=$C$C$E$E$W$W1">!!V++1(&----#-----13&&7>+?+?+A+AY+N+N1:--1KGK 3R88>>@@@@t7""
	V#GO$455E/22P2P2P2P%2P2P2P/P/P,/ [ 45YZZZuQx==Daxx ll"K 	V 	V 	V01MNNTUU	V q2s   A+E 
E%E  E%c                 (   	 |                                  }|dk    s|dk    r|                                  }n3# t          j        t          j        f$ r t          j        d          w xY w|st          j        d          |                                S )N   
   
zRemote disconnected)readliner   TcpDisconnectTlsExceptionHttpReadDisconnectr   )r    lines     r   _get_first_linerr      s    C~~7??demm>>##D$j&=> C C C+,ABBBC C+,ABBB::<<s	   47 0A'c                    	 t          |           }n'# t          j        $ r t          j        d          w xY w	 |                                \  }}}|dk    s|                    d          rdd|}}}d\  }}	n|dk    r)d|d}}}t          j        |d          \  }}	|	st          n|                    d	d
          \  }}
d|
vr|
dz   }
|
                    dd
          \  }}d|z   }t          j        |d          \  }}	|	pt          j        |          }	|	st          t          j	        |           t          |           n%# t          $ r t          j        d|           w xY w||	|||||fS )NzClient disconnected   *   /r   )rS   r   s   CONNECTT)checks   ://r=   )maxsplitzBad HTTP request line: )rr   r   rp   r   
startswithr   parse_authorityrf   default_portparse_check_http_versionrd   )r    rq   r.   targetr2   r/   r0   r1   r,   r-   restpath_s               r   r(   r(      s   Cu%%( C C C+,ABBBCO'+zz||$T>>V..t44>&)3tIFJD$$z!!&)63tIF,YdCCCJD$ !  ! "<<<;;LFD
 4d{#zz$z;;Iu%<D,YdCCCJD$33+F33D !  IfL)))) O O O,-Mt-M-MNNNO vvy$DDs    $6DE "E(c                    	 t          |           }n'# t          j        $ r t          j        d          w xY w	 |                    d d          }t	          |          dk    r|                    d           |\  }}}t          |          }t          |           n5# t          $ r( t          j	        d
                    |                    w xY w|||fS )NzServer disconnected   r   zBad HTTP response line: {})rr   r   rp   r   rE   appendre   r|   rf   rd   rB   )r    rq   partsr2   r9   r:   s         r   r7   r7     s    Cu%%( C C C+,ABBBC
X

4##u::??LL-2*k7+&&L)))) X X X,-I-P-PQU-V-VWWWX g--s    $6A"B 2Cc                 ~    t          j        d|           s't          j        d                    |                     d S )Ns   ^HTTP/\d\.\d$zUnknown HTTP version: {})rematchr   rd   rB   )r2   s    r   r|   r|   /  sD    8%|44 ^,-G-N-N|-\-\]]]^ ^r   c                 :   g }	 |                                  }|r|dk    s|dk    rn|d         dv rN|st          j        d          |d         d         |d         d         d	z   |                                z   f|d<   n	 |                    d
d          \  }}|                                }|st                      |                    ||f           n2# t
          $ r% t          j        dt          |          z            w xY wt          j	        |          S )z
        Read a set of headers.
        Stop once a blank line is reached.

        Returns:
            A headers object

        Raises:
            exceptions.HttpSyntaxException
    Trk   rl   r   s    	zInvalid headersr^   r=   s   
    :zInvalid header line: %s)
rm   r   rd   r   r   rf   r   reprr   Headers)r    retrq   namevalues        r   r)   r)   4  s=    C~~ 	tw$%--7f H 45FGGG2wqz3r71:#84::<<#GHCGG	"jjq11e '$,,&

D%=))))    4-T

:  #* ?3s    AC /Dc              #     K   d}	 |                      d          }|dk    rt          j        d          |dk    r|dk    r	 t          |d          }n5# t          $ r( t          j        d	                    |                    w xY w||z  }||k    r(t          j        d
                    ||                    |                     |          }|                      d          }|dk    rt          j        d          |dk    rdS |V  )z
    Read a HTTP body with chunked transfer encoding.

    Args:
        rfile: the input file
        limit: A positive integer
    r   T   r   zConnection closed prematurelyrk   rl      z#Invalid chunked encoding length: {}z@HTTP Body too large. Limit is {}, chunked content longer than {}   zMalformed chunked bodyN)rm   r   rA   re   rf   rd   rB   rD   )r    r   totalrq   lengthchunksuffixs          r   r@   r@   X  s<      E~~c""3;;*+JKKK7??tu}}iT2 i i i 45Z5a5abf5g5ghhhiVOEu}} .55;VE55I5I   JJv&&E^^A&&F   45MNNN{{KKK+s   A 2B)N)Nr;   )NT)r   r>   r   typing!seleniumwire.thirdparty.mitmproxyr   *seleniumwire.thirdparty.mitmproxy.net.httpr   r   r   r   r   r#   r   r5   r4   r   rO   r*   Optionalr8   boolr   rr   r(   r7   r|   r)   r?   r@   r   r   r   <module>r      s   				 



   8 8 8 8 8 8G G G G G G G G G G G G	/ 	/ 	/     >   m m m<4^ 4^ 4^ 4^nF F F& 8<%)3 33/("343 #3 3 3 3l
 
 
'E 'E 'ET. . .,^ ^ ^
!  !  ! H  #{      r   