
    kj                     h   U d dl Z d dlZd dlmZ d dlmZ 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 d	lmZ d d
lmZ dej"                  dej$                  fdZdej$                  ddfdZdej"                  dej*                  fdZdej$                  defdZdej"                  defdZdej"                  defdZdej"                  defdZdej"                  defdZddej"                  defdZ eeeeee      Z eeeej"                  geez  f   f   e!d<    G d d      Z"y)    N)Callable)Sequence)command)ctx)
exceptions)flow)http)assemble)strutilsfreturnc                     t        | dd       st        j                  d      t        | t        j
                        sJ | j                  j                         }|j                  d       |S )Nrequestz"Can't export flow with no request.Fstrict)	getattrr   CommandError
isinstancer	   HTTPFlowr   copydecoder   r   s     `/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/addons/export.pycleanup_requestr      sW    1i&%%&JKKa'''iinnGNN%N N    r   c                 N   | j                   j                  dd       | j                   j                  dd      | j                  k(  r| j                   j                  d       | j                   j                  dd      | j                  k(  r| j                   j                  d       yy)z>Remove some headers that are redundant for curl/httpie export.zcontent-lengthNhost z
:authority)headerspopgetr   )r   s    r   pop_headersr"      sz    OO($/62&',,6F#<,<L) =r   c                     t        | dd       st        j                  d      t        | t        j
                        sJ | j                  j                         }|j                  d       |S )Nresponsez#Can't export flow with no response.Fr   )	r   r   r   r   r	   r   r$   r   r   r   r$   s     r   cleanup_responser&   %   sW    1j$'%%&KLLa'''zz HOO5O!Or   c                    	 | j                  d      }|sJ 	 t	        d      D ci c]  }t        |      d|d c}dj                  fd|D              }t        fd	|D              rd
t        j                  |       dS t        j                  |      S # t        $ r t        j                  d      w xY wc c}w )NTr   z%Request content must be valid unicode    z\x02xr   c              3   B   K   | ]  }j                  ||        y wN)r!   ).0xescape_control_charss     r   	<genexpr>z.request_content_for_console.<locals>.<genexpr>7   s     Ha/33Aq9Hs   c              3   &   K   | ]  }|v  
 y wr+    )r,   charr.   s     r   r/   z.request_content_for_console.<locals>.<genexpr>8   s     
9D4''
9s   z
"$(printf z)")
get_text
ValueErrorr   r   rangechrjoinanyshlexquote)r   textiescaped_textr.   s       @r   request_content_for_consoler>   .   s    Ot,t
 <A9EaCFc!CM1E77H4HHL

9D
99EKK56b99;;|$$  O %%&MNNO Fs   B B?B<c                 "   t        |       }t        |       dg}| j                  j                  r| j                  j                  d   nd }t        j
                  j                  rR|rP|j                  |k7  rA|j                   d|j                   d| d}|j                  d       |j                  |       |j                  j                  d      D ]6  \  }}|j                         d	k(  r|j                  d
       +|d| d| gz  }8 |j                  dk7  r$|j                  s|ddgz  }|d|j                  gz  }|j                  |j                         dj!                  d |D              }|j                  r|dt#        |       z  }|S )Ncurlr   :z:[]z	--resolveTmultizaccept-encodingz--compressedz-H: GETzcontent-length: 0z-X c              3   F   K   | ]  }t        j                  |        y wr+   r9   r:   r,   args     r   r/   zcurl_command.<locals>.<genexpr>a   s     8Cu{{3'8   !z -d )r   r"   server_connpeernamer   optionsexport_preserve_original_ippretty_hostportappendr   itemslowermethodcontent
pretty_urlr7   r>   )r   r   argsserver_addrresolvekvr   s           r   curl_commandr^   ?   su   a G8D/0}}/E/E!--((+4K 	//;.(()7<<.;-qIK G%%D%1 (1779))KK'TaS1#;''D	( ~~ T.//Dw~~&&KK""#hh8488GT5g>?@@Nr   c                 F   t        |       }t        |       |j                  }d|j                  |g}|j                  j                  d      D ]  \  }}|j                  | d|         dj                  d |D              }|j                  r|dt        |      z   z  }|S )Nr	   TrC   rE   rG   c              3   F   K   | ]  }t        j                  |        y wr+   rI   rJ   s     r   r/   z!httpie_command.<locals>.<genexpr>s   s     45;;s#4rL   z <<< )
r   r"   rX   rV   r   rT   rS   r7   rW   r>   )r   r   urlrY   r\   r]   cmds          r   httpie_commandrc   g   s    a G
 

CGNNC(D%%D%1 !1qcA3K !
((4t4
4Cw4W===Jr   c                     t        |       }|j                  t        j                  d      t	        j
                  |      S )NzRequest content missing.)r   raw_contentr   r   r
   assemble_requestr   s     r   raw_requestrg   y   s;    a G"%%&@AA$$W--r   c                     t        |       }|j                  t        j                  d      t	        j
                  |      S )NzResponse content missing.)r&   re   r   r   r
   assemble_responser%   s     r   raw_responserj      s;    "H#%%&ABB%%h//r   c                 \   t        | t        j                        xr& | j                  xr | j                  j                  du}t        | t        j                        xr& | j
                  xr | j
                  j                  du}|rx|rvt        |       t        |       g}t        | t        j                        r5| j                  r)|j                  | j                  j                                |j                  |      S |rt        |       S |rt        |       S t        j                  d      )zOReturn either the request or response if only one exists, otherwise return bothNz.Can't export flow with no request or response.)r   r	   r   r   re   r$   rg   rj   	websocketrS   _get_formatted_messagesr7   r   r   )r   	separatorrequest_presentresponse_presentpartss        r   rawrr      s     	1dmm$XXqyy7L7LTX7X  	1dmm$ 	/JJ	/JJ""$.  +Qa1a'AKKLL<<>?~~e$$	1~	A%%&VWWr   )r@   httpierr   rg   rj   formatsc            	       p   e Zd Zd Z ej                  d      dee   fd       Z ej                  d      dedej                  de
j                  j                  dd	fd
       Z ej                  d      dedej                  dd	fd       Z ej                  d      dedej                  defd       Zy	)Exportc                 4    |j                  dt        dd       y )NrP   Fa[  
            When exporting a request as an external command, make an effort to
            connect to the same IP as in the original request. This helps with
            reproducibility in cases where the behaviour depends on the
            particular host we are connecting to. Currently this only affects
            curl exports.
            )
add_optionbool)selfloaders     r   loadzExport.load   s    )		
r   zexport.formatsr   c                 N    t        t        t        j                                     S )z@
        Return a list of the supported export formats.
        )listsortedrt   keys)rz   s    r   rt   zExport.formats   s    
 F7<<>*++r   zexport.fileformatr   pathNc                    |t         vrt        j                  d|z        t        |   |      }	 t        |d      5 }t	        |t
              r|j                  |       n!|j                  |j                  dd             ddd       y# 1 sw Y   yxY w# t        $ r(}t        j                  t        |             Y d}~yd}~ww xY w)z(
        Export a flow to path.
        No such export format: %swbzutf-8surrogateescapeN)rt   r   r   openr   byteswriteencodeOSErrorloggingerrorstr)rz   r   r   r   r]   fpes          r   filezExport.file   s    
  ))*E*NOOFOD!	"dD! CRa'HHQKHHQXXg/@AB	C C C
  	"MM#a&!!	"s5   B AB	 B 	BB B 	CCCzexport.clipr   c                     | j                  ||      }	 t        j                  |       y# t        j                  $ r(}t	        j
                  t        |             Y d}~yd}~ww xY w)z8
        Export a flow to the system clipboard.
        N)
export_str	pyperclipr   PyperclipExceptionr   r   r   )rz   r   r   rW   r   s        r   clipzExport.clip   sN    
 //&!,	"NN7#++ 	"MM#a&!!	"s   * A%A  A%exportc                     |t         vrt        j                  d|z        t        |   |      }t        j                  |dd      }t        j
                  |dd      }|S )z6
        Export a flow and return the result.
        r   utf8r   backslashreplace)rt   r   r   r   always_bytes
always_str)rz   r   r   rW   s       r   r   zExport.export_str   sb    
  ))*E*NOO&/!$ ''9JK%%gv7IJr   )__name__
__module____qualname__r|   r   r   r   rt   r   Flow	mitmproxytypesPathr   r   r   r1   r   r   rv   rv      s    
 W__%&,# , ', W__]#"3 "dii "y7K7K "PT " $"  W__]#"3 "499 " " $" W__X  s  r   rv   )s   

)#r   r9   collections.abcr   r   r   mitmproxy.typesr   r   r   r   r   r	   mitmproxy.net.http.http1r
   mitmproxy.utilsr   r   Requestr   r"   Responser&   r   r>   r^   rc   r   rg   rj   rr   dictrt   __annotations__rv   r1   r   r   <module>r      sS     $ $         - $tyy T\\ * *$ *		 dmm % %# %"%DII %# %Pdii C $.499 . .0DII 0% 0X499 X X0 :>	:c8TYYKu455	6 @ @r   