+
    Zhi;              
       L   ^ RI t^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
HtHtHtHtHtHt ^ RIHtHt ^ RIHtHt ^ RIHtHtHtHt R,tRtRtRtR	t ]PB                  ! ]PD                  ! ]#4      4      t$]! R
R]%3R]]&]&3,          3R]'3.4      t(]! RR]%3R]]&]&3,          3R]&3R]]&]3,          3R]],          3.4      t)R R lt*R R lt+/ s,R R lt-R R lt.R R lt/R-R R llt0R t1R t2R.R R  llt3R! R" lt4R# R$ lt5R.R% R& llt6R/ RRR'3R( R) llt7R/R* R+ llt8R# )0    N)AnyDictList
NamedTupleOptionalTuple)errorrequest)ParseResulturlparse)defaults
exceptionssystemutilzhttp://archive.ubuntu.comzhttps://esm.ubuntu.comzhttp://api.snapcraft.iozhttps://api.snapcraft.ioUnparsedHTTPResponsecodeheadersbodyHTTPResponse	json_dict	json_listc                0    V ^8  d   QhR\         R\        /# )   urlreturn)strbool)formats   "8/usr/lib/python3/dist-packages/uaclient/http/__init__.py__annotate__r    +   s            c                      \        V 4      pTP                  R9  d   R #  TP                   R#   \         d     R # i ; i  \         d     R # i ; i)FT)httpshttp)r   
ValueErrorschemeport)r   
parsed_urls   & r   is_service_urlr)   +   s\    c]
  11     s   / A >>AAc          	      t    V ^8  d   QhR\         R\        \         ,          R\         R\        \         ,          /# )r   protocolproxytest_urlr   )r   r   )r   s   "r   r    r    <   s4     /; /;/;"3-/;36/;c]/;r!   c                    V'       g   R # \        V4      '       g   \        P                  ! VR7      h\        P                  ! VRR7      pV R8X  dR   \        V4      P                  R8X  d8    \        W1R7      pTP                   ^8X  d   T# \        P                  ! TR7      h\        P"                  ! W/4      p\        P$                  ! V4      p VP'                  V4       V#   \        P                   d    h \        P                   d    h \        P                   d    h \         d>   p\        P                  RTT\        T4      4       \        P                  ! TR7      hR p?ii ; i  \(        P*                  \        P,                  3 dI   p\        P                  RTT\/        TR\        T4      4      4       \        P                  ! TR7      hR p?ii ; i)	N)r,   HEAD)methodr#   https_proxyz:Error trying to use "%s" as pycurl proxy to reach "%s": %sz:Error trying to use "%s" as urllib proxy to reach "%s": %sreason)r)   r   ProxyInvalidUrlr
   Requestr   r&   _readurl_pycurl_https_in_httpsPycurlRequiredErrorProxyAuthenticationFailedPycurlCACertificatesError	ExceptionLOGr	   r   ProxyNotWorkingErrorr   ProxyHandlerbuild_openeropensockettimeoutURLErrorgetattr)r+   r,   r-   reqresponseeproxy_handleropeners   &&&     r   validate_proxyrI   <   s    %  ((u55
//(6
2C7x55@	?5cMH  ==CL11>>((():;M!!-0F
;C3 -- 	33 	33 	 	?IILA	 11>>	?( NNENN+ ;		HAxQ(		
 --E::;sC   -C$ E. $(E+E+"
E+-E+.8E&&E+.%GAGGc                `    V ^8  d   QhR\         \        ,          R\         \        ,          RR/# )r   
http_proxyr2   r   N)r   r   )r   s   "r   r    r    q   s,     .$ .$.$,4SM.$	.$r!   c           
        / pV '       d   WR&   V'       d   WR&   RP                  \        \        4      4      pR Fz  p\        P                  P                  V4      pV'       g   K,  RP                  \        \        VP                  R4      4      P                  \        \        4      4      4      4      pK|  	  \        P                  RV4       V\        P                  R&   V\        P                  R&   V'       dC   \        P                  ! V4      p\        P                  ! V4      p\        P                  ! V4       \        P                  RRV/R	7       VsR
# )a+  
Globally configure pro-client to use http and https proxies.

- sets global proxy configuration for urllib
- sets the no_proxy environment variable for the current process
  which gets inherited for all subprocesses
- sets module variable for use in https-in-https pycurl requests
  this is retrieved later using get_configured_web_proxy

:param http_proxy: http proxy to be used by urllib. If None, it will
                   not be configured
:param https_proxy: https proxy to be used by urllib. If None, it will
                    not be configured
r$   r#   ,no_proxyNO_PROXYzSetting no_proxy: %szSetting global proxy dictextra)rP   N)rN   rO   )joinsortedUA_NO_PROXY_URLSosenvirongetsetsplitunionr;   debugr
   r=   r>   install_opener_global_proxy_dict)rK   r2   
proxy_dictrN   env_varproxy_valuerG   rH   s   &&      r   configure_web_proxyr`   q   s
   " J'6)7 xx/01H+jjnnW-;xx))#./55c:J6KLH	 , II$h/%BJJz%BJJz,,Z8%%m4v&II)':1FIG#r!   c                F    V ^8  d   QhR\         \        \        3,          /# r   r   )r   r   )r   s   "r   r    r       s      $sCx. r!   c                      \         # N)r\    r!   r   get_configured_web_proxyrf      s    r!   c                z    V ^8  d   QhR\         P                  P                  R\        \        \        3,          /# )r   r   r   )emailmessageMessager   r   )r   s   "r   r    r       s,     7 7emm33 7S#X 7r!   c                 t    V P                  4        UUu/ uF  w  rVP                  4       VbK  	  upp# u uppi rd   )itemslower)r   kvs   &  r   _headers_to_dictrp      s,    &-mmo6oUQAGGIqLo666s   4c                f    V ^8  d   QhR\         P                  R\        \        ,          R\        /# )r   rD   rA   r   )r
   r5   r   intr   )r   s   "r   r    r       s.      	c] r!   c                     \         P                  ! WR 7      pTP                  4       p\        TP                  \        TP                   4      TR7      #   \        P                   d   pTp Rp?LWRp?i\        P                   dO   p\
        P                  \        TP                  4      4       \        P                  ! Y0P                  R7      hRp?ii ; i)rA   N)causer   r   r   r   )r
   urlopenr	   	HTTPErrorrB   r;   	exceptionr   r3   r   ConnectivityErrorfull_urlreadr   r   rp   r   )rD   rA   resprF   r   s   &&   r   _readurl_urllibr~      s    Fs4 99;DYY .  ?? >> Fc!((m$**EEFs*   A C)A00CCA	CCc                   \        V4      p\        V 4      pVP                  R8H  ;'       dI    \        P                  ! VP
                  4      '       * ;'       d    VRJ;'       d    VP                  R8H  p\        P                  RV4       V# )a  
We only want to use pycurl if all of the following are true

- The target url scheme is https
- The target host is not in no_proxy
- An https_proxy is configured either via pro's config or via environment
- The https_proxy url scheme is https

urllib.request provides some helpful functions that we re-use here.

This function also returns the https_proxy to use, since it is calculated
here anyway.
r#   NzShould use pycurl: %r)r   _parse_https_proxyr&   r
   proxy_bypasshostnamer;   rZ   )r2   
target_urlparsed_target_urlparsed_https_proxyrets   &&   r   should_use_pycurlr      s     !,+K8  G+ 	1 	1$$%6%?%?@@	1 	1d*	1 	1 %%0	  II%s+Jr!   c                 z   R pR p\        V P                  4      ^ 8  d   V P                  ^ ,          p\        V P                  4      ^8  d   V P                  ^,          pWB8X  d%   V'       d   RV9   d   \        P                  ! 4       hWC8X  d   \        P                  ! VR7      h\        P
                  ! V R7      h)N407r   )rF   )lenargsr   r8   r9   PycurlError)r	   r   authentication_error_codeca_certificates_error_coder   msgs   &&&&  r   _handle_pycurl_errorr      s     D
C
5::zz!}
5::jjm(SUc\2244		+22s;;$$u--r!   c                    V ^8  d   QhR\         P                  R\        \        ,          R\        \        ,          R\
        /# )r   rD   rA   r2   r   )r
   r5   r   rr   r   r   )r   s   "r   r    r       sB     \ \	\c]\ #\ 	\r!   c                 "  a  ^ RI pTP	                  4       pT P                  4       P                  4       pTR8X  d   TP                  TP                  R4       MTR8X  d   TP                  TP                  R4       MvTR8X  dV   TP                  TP                  R4       T P                  '       d'   TP                  TP                  T P                  4       M\        RP                  T4      4      hTP                  TP                  T P!                  4       4       T P#                  4        UUu. uF  w  rgRP                  Yg4      NK  	  ppp\%        T4      ^ 8  d   TP                  TP&                  T4       TP                  TP(                  R4       TP                  TP*                  \,        P.                  4       T'       d   TP                  TP0                  T4       T'       d_   \3        T4      p	T	'       d   T	P5                  4       MRpTP                  TP6                  T4       TP                  TP8                  ^4       M\:        P=                  R4       \>        P@                  ! 4       p
TP                  TPB                  T
4       / oT3R	 lpTP                  TPD                  T4        TPG                  4        \Q        TPS                  TPT                  4      4      pT
PW                  4       pTPY                  4        \[        TSTR7      #   \         d    \        P                  ! 4       hi ; iu uppi   TPH                   d<   p\K        TT P!                  4       TPL                  TPN                  R
7        Rp?LRp?ii ; i)r   NGETTr/   POSTz5HTTP method "{}" not supported in HTTPS-in-HTTPS modez{}: {}z1in pycurl request function without an https proxyc                    < V P                  R 4      p RV 9  d   R# V P                  R^4      w  rVP                  4       P                  4       pVP                  4       pVSV&   R# )z
iso-8859-1:N)decoderX   striprm   )header_linename_raw	value_rawnamevaluer   s   &    r   save_header3_readurl_pycurl_https_in_https.<locals>.save_header(  s]    !((6k!)//Q7~~%%'!r!   )r   r   r   rv   ).pycurlImportErrorr   r7   Curl
get_methoduppersetoptHTTPGETNOBODYr   dataCOPYPOSTFIELDSr%   r   URLget_full_urlheader_itemsr   
HTTPHEADERFOLLOWLOCATIONCAINFOr   SSL_CERTS_PATHTIMEOUTr   geturlPROXY	PROXYTYPEr;   warningioBytesIO	WRITEDATAHEADERFUNCTIONperformr	   r   E_RECV_ERRORE_SSL_CACERT_BADFILErr   getinfoRESPONSE_CODEgetvaluecloser   )rD   rA   r2   r   cr0   r   valheader_str_listr   body_outputr   rF   r   r   r   s   &&&            @r   r6   r6      s   
/ 	A ^^##%F	&	6		%	6		d#888HHV**CHH5CJJ
 	
 HHVZZ))+, 584D4D4F4Fyt"4F   ?a	""O4 HHV""D)HHV]]H334	) /<+=%%'4 	 	
{+	!!1%GH **,KHHV{+G HHV""K0
			 qyy--./D!DGGI c  /,,../4T << 
  "&,&9&9'-'B'B		
 	

s)   L 0L<6M !L9N1N		Nc                :    V ^8  d   QhR\         \        ,          /# rb   )r   r   )r   s   "r   r    r    J  s     : :x'< :r!   c                     V '       g%   \         P                  ! 4       P                  R 4      p V '       d   \        V 4      # R# )r#   N)r
   
getproxiesrV   r   r1   s   &r   r   r   J  s0    ((*..w7$/8K 9T9r!   c                $    V ^8  d   QhR\         /# )r   r   )r   )r   s   "r   r    r    P  s     ) ) )r!   c                    V P                   P                  R 4      p/ pV'       g   / pMR\        P                  P	                  V4      '       g   / pM*\
        P                  ! \        P                  ! V4      4      pVP                  V. 4      # )serviceclient_url_responses)	featuresrV   rT   pathexistsjsonloadsr   	load_file)cfgr   response_overlay_pathresponse_overlays   &&  r   _get_overlay_datar   P  so    LL,,-JK WW^^122::f&6&67L&MNR((r!   c                t    V ^8  d   QhR\         R\        \        ,          R\        \        \         3,          /# )r   r   rA   r   )r   r   rr   r   bytes)r   s   "r   r    r    ^  s1     1 11$SM1
5#:1r!   c           	      ~   \        W4      pV'       d[   VP                  ^ 4      p\        P                  ! VR,          R,          4      ;_uu_ 4       pVP	                  4       R3uuRRR4       # \        V4      '       g   \        P                  ! VR7      h\        P                  RP                  V4      4       \        4       P                  R4      p/ pV'       d   W8R&   \        Wq4      '       d   \        \        P                   ! WR	7      VVR
7      pVP"                  R8X  d   \        P$                  ! VR7      h\        P&                  ! VP(                  4      VP*                  P                  R4      3# \        P                   ! WR	7      p	 \        P,                  ! V	4      ;_uu_ 4       p\        P                  ! V4      ;_uu_ 4       pVP	                  4       VP*                  P                  R4      3uuRRR4       uuRRR4       #   + '       g   i     EL; i  + '       g   i     M; i RRR4       R#   + '       g   i     R# ; i  \.        P0                   dU   p
T
P"                  R8X  d   \        P$                  ! TR7      hT
P"                  R8X  d   \        P2                  ! 4       hh Rp
?
ii ; i)r   rE   	file_path Nr   zURL [GET]: {}r#   zIf-None-Match)r   rA   r2   i0  etagETagi  )r   poplzmar?   r|   r)   r   
InvalidUrlr;   rZ   r   rf   rV   r   r6   r
   r5   r   ETagUnchanged
decompressr   r   rw   r	   rx   VulnerabilityDataNotFound)r   r   rA   r   overlay_responserE   fr2   r   rD   rF   s   &&&&       r   download_xz_file_from_urlr   ^  s    )2#''*YYx
+K899QFFHb> :9 ###,,IIo$$S)**,009KG#' **1OOC1#
 ==C**s33 OOHMM*  (
 	

 ooc3	%%YYx((A ((,,V4 )( &%= :99> )(( &%%%  	vv} ..377vv} ::<<	sa   HI .H?+H!	8
H?
I H	!H1,H?5I ?I	
I I J<(AJ77J<Tc                    V ^8  d   QhR\         R\        \        ,          R\        \         \         3,          R\        \         ,          R\        \        ,          R\
        R\        /# )r   r   r   r   r0   rA   log_response_bodyr   )r   r   r   r   rr   r   r   )r   s   "r   r    r      sk     F F	F
5/F #s(^F SM	F
 c]F F Fr!   c           
         \        V 4      '       g   \        P                  ! V R 7      hV'       d   V'       g   Rp\        P                  ! WW#R7      pRP                  \        V4       Uu. uF  pRP                  WrV,          4      NK  	  up4      p\        P                  RP                  T;'       g    RT TV'       d   VP                  R4      MR4      4       \        4       P                  R	4      p	\        W4      '       d   \        WdV	R
7      p
M\        WdR7      p
V
P                   P                  RRR7      p/ p. pRV
P"                  P                  RR4      9   dx   \$        P&                  ! V\(        P*                  R7      p\-        V\.        4      '       d   TpM8\-        V\0        4      '       d   TpM\        P3                  R\5        V4      4       RP                  \        V
P"                  4       Uu. uF%  pRP                  WzP"                  V,          4      NK'  	  up4      pRP                  T;'       g    RW4      pV'       d:   V
P                   pV'       d   TpM
V'       d   TpVRP                  V4      ,          p\        P                  V4       \7        V
P8                  V
P"                  VVVR7      # u upi u upi )r   r   )r   r   r0   z, z
'{}': '{}'z'URL [{}]: {}, headers: {{{}}}, data: {}r   utf-8Nr#   r   rt   ignoreerrorsapplication/jsoncontent-typer   clsunexpected JSON response: %sz&URL [{}] response: {}, headers: {{{}}}z
, data: {}r   r   r   r   r   )r)   r   r   r
   r5   rQ   rR   r   r;   rZ   r   rf   rV   r   r6   r~   r   r   r   r   r   DatetimeAwareJSONDecoder
isinstancedictlistr   r   r   r   )r   r   r   r0   rA   r   rD   rn   sorted_header_strr2   r}   decoded_bodyr   r   	json_body	debug_msgbody_to_logs   &&&&&&           r   readurlr     s:    ###,,F
//#'
IC		5;G_E_		Q
	+_E II188OOe$(DKK d		
 +,009K**-k
 s499##GH#=LIIT\\--nbAAJJ|1N1NO	i&&!I	4((!IKK6IG		:@:NO:NQ		QQ	0:NO 9??%I ii#K#K\((55	IIiYY a 	FB 	Ps   +!K<+K	c          
      T    V ^8  d   QhR\         R\         R\         R\         R\        /# )r   socket_pathhttp_method	http_pathhttp_hostnamer   )r   r   )r   s   "r   r    r      s:     & &&& & 	&
 &r!   c                 v   \         P                   ! \         P                  \         P                  4      pVP                  V 4       \        P
                  P                  V4      pWEn         VP                  W4       VP                  4       pVP                  4       P                  R RR7      pVP                  4        VP                  4        / p. p	RVP                  P                  RR4      9   dx   \        P                   ! V\"        P$                  R7      p
\'        V
\(        4      '       d   T
pM8\'        V
\*        4      '       d   T
p	M\,        P/                  R\1        V
4      4       \3        VP4                  \7        VP                  4      VVV	R7      #   TP                  4        TP                  4        i ; i)	r   r   r   r   r   r   r   r   r   )r@   AF_UNIXSOCK_STREAMconnectr$   clientHTTPConnectionsockr
   getresponser|   r   r   r   rV   r   r   r   r   r   r   r   r;   r   r   r   statusrp   )r   r   r  r  r	  connr}   outr   r   r   s   &&&&       r   unix_socket_requestr    s>    ==););<DLL;;%%m4DI[,!iik   :



IIT\\--nbAAJJs(E(EF	i&&!I	4((!IKK6IG[[ .  	



s   +AF "F8)z169.254.169.254metadataz[fd00:ec2::254]rd   )NN)	localhost)9email.messagerh   http.clientr$   r   r   loggingr   rT   r@   typingr   r   r   r   r   r   urllibr	   r
   urllib.parser   r   uaclientr   r   r   r   rS   PROXY_VALIDATION_APT_HTTP_URLPROXY_VALIDATION_APT_HTTPS_URLPROXY_VALIDATION_SNAP_HTTP_URLPROXY_VALIDATION_SNAP_HTTPS_URL	getLoggerreplace_top_level_logger_name__name__r;   rr   r   r   r   r   r)   rI   r\   r`   rf   rp   r~   r   r   r6   r   r   r   r   r  re   r!   r   <module>r     sR     	    	  ? ? ! . 7 7E  ; !9 !: "< ::8DE!		DcN#	  		DcN#		d38n%	d3i 	"/;d  .$b7
*4."\~:)1l !  !"FR& &r!   