
    x[h                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	J
r
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJrJrJrJrJr  S S	K J!r!J"r"J#r#J$r$  S SK%r%S S
K%J&r&  S SK'J(r(J)r)J*r*  \RV                  " \,5      r-Sr.\\S/\/4      r0S r1S r2S\34S jr4SKS\5S\6S\7SS4S jjr8S\5SS4S jr9S\S   4S jr: " S S5      r; " S S\;5      r< " S S\;5      r= " S  S!5      r> " S" S\?5      r@S# rAS$\5S\64S% jrBSS&.S'\@S(\0S\\6   4S) jjrCSSS S*SSSSS+S+SSS,S+S-S,S..S(\0S/\/S\>4S0 jjrD SLS1\S2\4   S3\5S\3S4\R                  S5\\6   S\4S6 jjrF  SMS1\S2\4   S8\\5   S9\6S\3S\\\5   \\>   4   4
S: jjrG " S; S<\5      rH\6" S=5      S\-R                  SSSSSS-S+S7S>.S?\6S\\6   S@\SA\\   SB\\6   S(\0SC\\\\6/\64      SD\5SE\/SF\64SG jjrJ " SH SI5      rK SLSJ jrLg)N    N)ThreadPoolExecutorTimeoutErroras_completed)	parsedate)partial)	NOT_FOUND)count)create_default_context)	AnyCallableIteratorListMapping
NamedTupleOptionalTupleUnion)quoteurlparseurlsplit
urlunparse)
exceptions)performanceutilversionREDACTEDUrlErrorc                     [        [        U SS95      nUS   (       d  US   (       a  US   US'   SUS'   [        U5      $ )Nhttp)scheme       )listr   r   )url
parsed_urls     6/usr/lib/python3/dist-packages/cloudinit/url_helper.py	_cleanurlr(   1   sC    hs623Ja=Z] #1
1
1j!!    c                 2    S nU nU H  nU" X45      nM     U$ )Nc                     [        [        U 5      5      nUS   nU(       a  UR                  S5      (       d  US-  nU[        [	        U5      SS9-  nX2S'   [        U5      $ )Nr"   /z/:)safe)r$   r   endswithr   strr   )r%   add_on
url_parsedpaths       r'   combine_single#combine_url.<locals>.combine_single<   sZ    (3-(
!}c**CKDc&k--1*%%r)    )baseadd_onsr3   r%   r0   s        r'   combine_urlr8   ;   s'    & CS) Jr)   returnc           
      L   [         R                  S[         R                  S[         R                  S[         R                  S[
        S0nUR                  [        U 5      5      nU(       d:  [        U [        5      (       a  U R                  nU$ [        R                  S5        SnU$ )z4helper for read_ftps to map return codes to a number,      iX  i  z9Unexpected exception type while connecting to ftp server.i)ftpliberror_reply
error_temp
error_permerror_protoEOFErrorgettype
isinstanceOSErrorerrnoLOGwarning)excftp_error_codescodes      r'   "ftp_get_return_code_from_exceptionrN   K   s     	C33C#O tCy)Dc7##99D K	 KKK DKr)   r%   timeoutkwargsFtpResponsec           	      b   [        U 5      nUR                  (       d  [        S[        SU S9e[        R
                  " 5        nUR                  =(       d    SnUR                  =(       d    SnSUR                  :X  GaG   [        R                  " [        5       S9n[        R                  SU U5        UR                  UR                  UU=(       d    S	S
9  [        R                  SU5         UR#                  UUR$                  =(       d    SS9  [        R                  S5        UR'                  5          [        R                  SUR,                  5        UR/                  SUR,                   3UR0                  S9  [3        UR5                  5       U 5      [        R                  S5        UR7                  5         sSSS5        $  [        R8                  " 5       n
[        R                  SX5        U
R                  UR                  UU=(       d    S	S
9  [        R                  SU5        U
R#                  UUR$                  =(       d    SS9  [        R                  SUR,                  5        U
R/                  SUR,                   3UR0                  S9  [3        UR5                  5       U 5      [        R                  S5        U
R7                  5         sSSS5        $ ! [        R                   a$  n[!        U5      n	[        SU  SU	 S3U	SU S9UeSnAff = f! [        R(                   a'  n[        R+                  S5        [        SSSU S9UeSnAff = f! [        R                   a$  n[!        U5      n	[        SU  SU	 S3U	SU S9UeSnAff = f! [        R                  S5        UR7                  5         f = f! [        R                   a$  n[!        U5      n	[        SU  SU	 S3U	SU S9UeSnAff = f! [        R                  S5        W
R7                  5         f = f! , (       d  f       g= f)aN  connect to URL using ftp over TLS and read a file

when using strict mode (ftps://), raise exception in event of failure
when not using strict mode (ftp://), fall back to using unencrypted ftp

url: string containing the desination to read a file from. The url is
    parsed with urllib.urlsplit to identify username, password, host,
    path, and port in the following format:
        ftps://[username:password@]host[:port]/[path]
    host is the only required component
timeout: maximum time for the connection to take
kwargs: unused, for compatibility with read_url
returns: UrlResponse
zInvalid url providedNcauserM   headersr%      	anonymousftps)contextz3Attempting to connect to %s via port [%s] over tls.      @)hostportrO   z1Reading file from server over tls failed for url z []z"Attempting to login with user [%s]r#   )userpasswdzCreating a secure connectionzAttempted to connect to an insecure ftp server but used a scheme of ftps://, which is not allowed. Use ftp:// to allow connecting to insecure ftp servers.r=   zReading file: %szRETR )callbackzClosing connectionz,Reading file from ftp server failed for url z(Attempting to connect to %s via port %s.)r   hostnamer   r   ioBytesIOr\   usernamer    r>   FTP_TLSr
   rI   debugconnect
all_errorsrN   loginpasswordprot_prA   rJ   r2   
retrbinarywriterQ   getvaluecloseFTP)r%   rO   rP   	url_partsbufferr\   r^   ftp_tlserM   ftps              r'   	read_ftpsrv   b   s     I(y$C
 	
 
~~#!!0[Y%%% ..24 		I
 "++#Ns     II:DA$--3   		89 " 		,inn=""INN+,v|| #  #6??#4c: 		./S 
Vjjl		> "++#Ns  
 		>E		$--3   		,inn=y~~&67&,,O"6??#4c: 		./		U 
$ $$ 
9!<**-ba9   
& $$ C
 N  	 	. $$ 
9!<++.%r$q:   
 		./( $$ 
9!<++.%r$q:   
 		./		U 
s   <P ?AKP -AL6A M%P C(N;.%P L1LLP M,"MMP N*N		NN'N88P ;O3O..O33O66'PP  
P.r2   FileResponsec                 *   UR                  S5      (       a  [        R                  SU 5         [        R                  " U 5      n[        X 5      $ ! [         a  n[        U[        SU S9UeSnAf[         a  n[        X3R                  SU S9UeSnAff = f)zeread a binary file and return a FileResponse

matches function signature with read_ftps and read_url
dataz'Unable to post data to file resource %sNrS   )rD   rI   rJ   r   load_binary_filerw   FileNotFoundErrorr   r   IOErrorrH   )r2   rP   contentsrt   s       r'   
_read_filer~      s    
 zz&=tDM((.H++ OQY$GQN MQWWdE1LMs#    A 
BA**B7BB)rw   UrlResponserQ   c                 n   U R                  5       n  [        U 5      nUR                  nUS:X  d  U (       a  SU S   :X  a  [        UR                  40 UD6$ US;   a  [        U 40 UD6$ US;   a  [        U 40 UD6$ [        R                  SU5        [        U 40 UD6$ ! [         a  n[        X0S9UeSnAff = f)	a   Wrapper function around readurl to allow passing a file path as url.

When url is not a local file path, passthrough any kwargs to readurl.

In the case of parameter passthrough to readurl, default values for some
parameters. See: call-signature of readurl in this module for param docs.
)rT   r%   Nfiler,   r   )ru   rX   )r   httpszAttempting unknown protocol %s)lstripr   
ValueErrorr   r    r~   r2   rv   readurlrI   rJ   )r%   rP   parsedrt   r    s        r'   read_file_or_urlr      s     **,C0# ]]FCC3q6M&++000	?	"'''	$	$s%f%%4f=s%f%%  0Q(a/0s   B 
B4%
B//B4c                   *    \ rS rSrSS jrS rS rSrg)StringResponsei  c                 6    X0l         0 U l        Xl        X l        g N)rM   rU   r}   r%   )selfr}   r%   rM   s       r'   __init__StringResponse.__init__  s    	 r)   c                      U R                   S:H  $ )N   rM   r   argsrP   s      r'   okStringResponse.ok  s    yyCr)   c                 8    U R                   R                  S5      $ )Nzutf-8)r}   decoder   s    r'   __str__StringResponse.__str__  s    }}##G,,r)   )rM   r}   rU   r%   Nr   )__name__
__module____qualname____firstlineno__r   r   r   __static_attributes__r5   r)   r'   r   r     s     -r)   r   c                   8   ^  \ rS rSrSS\S\4U 4S jjjrSrU =r$ )rw   i   r}   r%   c                 "   > [         TU ]  XUS9  g )Nr   superr   )r   r}   r%   rM   	__class__s       r'   r   FileResponse.__init__!  s    T2r)   r5   r   	r   r   r   r   bytesr/   r   r   __classcell__r   s   @r'   rw   rw      s    3 3S 3 3r)   c                   4   ^  \ rS rSrS\S\4U 4S jjrSrU =r$ )rQ   i%  r}   r%   c                 $   > [         TU ]  X5        g r   r   )r   r}   r%   r   s      r'   r   FtpResponse.__init__&  s    'r)   r5   r   r   s   @r'   rQ   rQ   %  s    ( (S ( (r)   c                       \ rS rSrS\R
                  4S jr\S\4S j5       r	\S\
4S j5       rSS\4S jjr\S 5       r\S\4S	 j5       rS
 r SS\\   S\S\\   4S jjrSrg)r   i*  responsec                     Xl         g r   	_response)r   r   s     r'   r   UrlResponse.__init__+  s    !r)   r9   c                 ^    U R                   R                  c  gU R                   R                  $ )Nr)   )r   contentr   s    r'   r}   UrlResponse.contents.  s%    >>!!)~~%%%r)   c                 .    U R                   R                  $ r   )r   r%   r   s    r'   r%   UrlResponse.url4  s    ~~!!!r)   c                 P    SnU(       a  SnSU R                   s=::  a  U:  a   g  gg)Nr;   r<   r   TFr   )r   redirects_okuppers      r'   r   UrlResponse.ok8  s-    E$))#e# $ r)   c                 .    U R                   R                  $ r   )r   rU   r   s    r'   rU   UrlResponse.headersA  s    ~~%%%r)   c                 .    U R                   R                  $ r   )r   status_coder   s    r'   rM   UrlResponse.codeE  s    ~~)))r)   c                 .    U R                   R                  $ r   )r   textr   s    r'   r   UrlResponse.__str__I  s    ~~"""r)   
chunk_sizedecode_unicodec              #   V   #    U R                   R                  X5       Sh  vN   g N7f)aD  Iterates over the response data.

When stream=True is set on the request, this avoids reading the content
at once into memory for large responses.

:param chunk_size: Number of bytes it should read into memory.
:param decode_unicode: If True, content will be decoded using the best
available encoding based on the response.
N)r   iter_content)r   r   r   s      r'   r   UrlResponse.iter_contentL  s      >>..zJJJs   )')r   N)F)r!   F)r   r   r   r   requestsResponser   propertyr   r}   r/   r%   boolr   rU   intrM   r   r   r   r   r   r5   r)   r'   r   r   *  s    "!2!2 " &% & &
 "S " "  & & *c * *# EJK"3-K=AK	%K Kr)   r   c            
       J    \ rS rSr   S	S\S\\   S\\   S\\   4S jjr	Sr
g)
r   i[  NrT   rM   rU   r%   c                 ~    [         R                  U [        U5      5        Xl        X l        Uc  0 OUU l        X@l        g r   )r|   r   r/   rT   rM   rU   r%   )r   rT   rM   rU   r%   s        r'   r   UrlError.__init__\  s4     	s5z*
	&-o7r)   rS   )NNN)r   r   r   r   r   r   r   r   r/   r   r   r5   r)   r'   r   r   [  sJ     #%)! sm '"	
 c] r)   c                     0 n[        U 5      R                  nUS:X  aW  U(       aP  SU;   a  US   (       a	  US   US'   OSUS'   SU;   a  SU;   a  US   US   /US'   U$ SU;   a  [        US   5      US'   U$ )Nr   ca_certsverifyT	cert_filekey_filecert)r   r    r/   )r%   ssl_detailsssl_argsr    s       r'   _get_ssl_argsr   j  s    Hc]!!F[$Z)@!,Z!8HX!%HX+%**CK(J' HV O K'";{#;<HVOr)   retry_afterc                 r    [        U 5      nU$ ! [         a     [        U 5      nU(       d  [        S5      e[        [        R                  " U5      [        R                  " 5       -
  5      nO(! [         a    [
        R                  SU 5        Sn Of = fUS:  a  [
        R                  S5        Sn U$ f = f)a(  Parse a Retry-After header value into an integer.

: param retry_after: The value of the Retry-After header.
    https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.3
    https://www.rfc-editor.org/rfc/rfc2616#section-3.3
: return: The number of seconds to wait before retrying the request.
z(Failed to parse Retry-After header valuezGFailed to parse Retry-After header value: %s. Waiting 1 second instead.r!   r   zBRetry-After header value is in the past. Waiting 1 second instead.)floatr   r   timemktimerI   info)r   to_wait
time_tuples      r'   _get_retry_afterr   |  s    $, N+  	";/J !KLLDKK
3diikABG 	HH,
 G	 Q;HH, GN+s3    
B6AA-,B6-"BB6B B65B6exception_cberrorr   c                D   U(       a  U" U 5      (       a  gU R                   (       aq  U R                   S:X  aa  [        R                  S5        U R                  (       a%  [	        U R                  R                  SS5      5      $ [        R                  S5        gU(       d  gU e)a  Handle exceptions raised during request processing.

If we have no exception callback or the callback handled the error or we
got a 503, return with an optional timeout so the request can be retried.
Otherwise, raise the error.

:param error: The exception raised during the request.
:param response: The response object.
:param exception_cb: Callable to handle the exception.

:return: Optional time to wait before retrying the request.
Ni  zEEndpoint returned a 503 error. HTTP endpoint is overloaded. Retrying.zRetry-After1z7Unable to introspect response header. Waiting 1 second.r!   )rM   rI   rJ   rU   r   rD   r   )r   r   s     r'   _handle_errorr     sw    " U++zzejjC'5	
 ==#EMM$5$5mS$IJJJK
Kr)   r!   TFr#   )ry   rO   retriessec_betweenrU   
headers_cbheaders_redactr   check_statusallow_redirectsr   sessioninfinitelog_req_resprequest_methodstreamr   c          	         [        U 5      n U US.nUR                  [        X5      5        U
US'   U(       d  U(       a  SOSnUUS'   Ub3  [        U[        5      (       a  UUS'   O[        [        U5      S5      US'   Uc  / nS	nU(       a  [        [        U5      S	-   S	5      nS
[        R                  " 5       -  nUb  UR                  5       nO0 nU(       a  UUS'   Uc  SnUc  [        R                  " 5       n[        5        GHR  nU(       a  U" U 5      nSU;  a  UUS'   UUS'   0 nUR                  5        H  u  nnUS:X  a  M  US:X  aj  U(       ac  U Vs/ s H  nUR                  U5      (       d  M  UPM     nnU(       a0  [        R                   " U5      UW'   U H  n["        UU   U'   M     M|  M~  UUU'   M      U(       a"  [$        R'                  SUU(       a  SOUU U5        UR(                  " S0 UD6nU	(       a  UR+                  5         [$        R'                  SU UR,                  [/        UR0                  5      US	-   5        [3        U5      s  $    [I        S5      es  snf ! [4        R6                   a  n[9        UU S9UeSnAf[4        R:                   a@  n[9        UUR<                  R,                  UR<                  R>                  U S9nUn SnAO2SnAf[4        R@                   a  n[9        UU S9nUnSn SnAOSnAff = f[C        UUS9nU(       d  U=(       d    US	-   U:  nU(       d  UUeU=(       d    Un US:  d  GMJ  U(       a  [$        R'                  SU5        [D        RF                  " U 5        GM  )a  Wrapper around requests.Session to read the url and retry if necessary

:param url: Mandatory url to request.
:param data: Optional form data to post the URL. Will set request_method
    to 'POST' if present.
:param timeout: Timeout in seconds to wait for a response. May be a tuple
    if specifying (connection timeout, read timeout).
:param retries: Number of times to retry on exception if exception_cb is
    None or exception_cb returns True for the exception caught. Default is
    to fail with 0 retries on exception.
:param sec_between: Default 1: amount of seconds passed to time.sleep
    between retries. None or -1 means don't sleep.
:param headers: Optional dict of headers to send during request
:param headers_cb: Optional callable returning a dict of values to send as
    headers during request
:param headers_redact: Optional list of header names to redact from the log
:param ssl_details: Optional dict providing key_file, ca_certs, and
    cert_file keys for use on in ssl connections.
:param check_status: Optional boolean set True to raise when HTTPError
    occurs. Default: True.
:param allow_redirects: Optional boolean passed straight to Session.request
    as 'allow_redirects'. Default: True.
:param exception_cb: Optional callable to handle exception and returns
    True if retries are permitted.
:param session: Optional exiting requests.Session instance to reuse.
:param infinite: Bool, set True to retry indefinitely. Default: False.
:param log_req_resp: Set False to turn off verbose debug messages.
:param request_method: String passed as 'method' to Session.request.
    Typically GET, or POST. Default: POST if data is provided, GET
    otherwise.
:param stream: if False, the response content will be immediately
downloaded.
)r%   r   r   POSTGETmethodNrO   r   r!   zCloud-Init/%sry   z
User-AgentrU   z'[%s/%s] open '%s' with %s configurationr   z(Read from %s (%s, %sb) after %s attempts)r%   rM   rU   r%   r   1Please wait %s seconds while we wait to try againz"This path should be unreachable...r5   )%r(   updater   rF   tuplemaxr   r   r   version_stringcopyr   Sessionr	   itemsrD   deepcopyr   rI   rf   requestraise_for_statusr   lenr   r   r   SSLErrorr   	HTTPErrorr   rU   RequestExceptionr   r   sleepRuntimeError)!r%   ry   rO   r   r   rU   r   r   r   r   r   r   r   r   r   r   r   req_argsmanual_tries
user_agentifiltered_req_argskvmatched_headerskeyr   rt   	url_errorraised_exceptionresponse_sleep_time
will_retry
sleep_times!                                    r'   r   r     s[   j C.CH OOM#34"1H#'U'HXgu%%")HY"%eGna"8HYL3w<!+Q/ G$:$:$<=J,,.""$ W oGw&$.GL!%NN$DAqF{I~..<"Ina1n"I"+/==+;%a(.4<)!,S1  / #
 ()!!$ %(			="*J% 22H))+II:$$H$$%Q x(([ f ;
<<M #JB "" 	. 1#&A-## 	! ZZ++

**	I  !** 	 ,I H	
 ,%
 #!;a!el&:J%55(7K
?		G JJz"s=   I&6I&BI++L?J

L!6KL3LLfunc.addreventdelayc                 J    U(       a  UR                  US9(       a  gU " X5      $ )z Execute func with optional delayrO   Nwaitr  r  rO   r  r  s        r'   _run_func_with_delayr#  p  s%     
 ::e:$r)   333333?	addressesstagger_delayc                 L   SnSnSn/ n[         R                  " 5       n[        [        U5      S9n	 [	        U5       V
Vs0 s H   u  pU	R                  [        U UUUX-  S9U_M"     nn
n[        XS9 Ho  nX   nUR                  5       nU(       a  UnUR                  " U5        M4  UR                  5       nU(       d  MM  UR                  5         XT4s  U	R                  SS9  $    U(       a  [        R                  SUU5        Ue[        R                  SU5        [!        S	5      es  snn
f ! ["         aF    [        R                  S
SR%                  U5      SR%                  ['        [(        U5      5      5         Of = f U	R                  SS9  XT4$ ! U	R                  SS9  f = f)a  execute multiple callbacks in parallel

Run blocking func against two different addresses staggered with a
delay. The first call to return successfully is returned from this
function and remaining unfinished calls are cancelled if they have not
yet started
N)max_workersr"  r  Fr   z<Exception(s) %s during request to %s, raising last exceptionzEmpty result for address %szNo result returnedzJTimed out waiting for addresses: %s, exception(s) raised while waiting: %s )	threadingEventr   r  	enumeratesubmitr#  r   	exceptionappendresultsetshutdownrI   rf   r   r   r   joinmapr/   )r  r%  r&  rO   return_resultreturned_addresslast_exceptionr   is_doneexecutorr  r  futuresfuturereturn_exceptions                  r'   
dual_stackr=    s    M.2NJooG "c)n=H5& %Y/

 0 OO$(    0 	 

 #7<F&%//1!1!!.1 & = KKM,<<2 	u%O =$ II- 	 ! II35EF122S

X  
		4HHYHHSj)*		

	
 	u%,, 	u%s=   D, 'D&&AD, ?D, #A	D, ,AE<9F ;E<<F F#c                   F    \ rS rSr% \\   \S'   \\   \S'   \\   \S'   Sr	g)HandledResponsei  r%   r   	wait_timer5   N)
r   r   r   r   r   r/   __annotations__r   r   r   r5   r)   r'   r?  r?    s#    	#{## r)   r?  inf)max_waitrO   	status_cbr   r   r  r   sleep_time_cbr   connect_synchronouslyasync_delayrC  rD  r   r  rE  r   rF  rG  c                  ^ ^^^^^	^^^^^^^ S[         S[        4U4S jjnSS[        S[        S[        4S jjmS	[        [           S
[        [           S[
        [        [           [        4   4S jmS[        [        /[
        [        [           [        [           4   4   S[        [        [        [           4   S[         S[        S[        S[        4UU4S jjmS
[        S[         S[        4UUU	4S jjmS[        4UUUUUUU 4S jjnS[        4UUUU 4S jjn[        R                  " 5       nT(       a  U(       a  [        S5      eU
(       a  UOUnU=(       d    UnSmSn U" XXs5      nSmUR                   (       a"  UR"                  UR                   R$                  4$ UR&                  (       a)  [        R(                  " UR&                  5        TS-   mSmMy  U" UT5      nT" TUU5      (       a   gTS-   m[*        R-                  SU5        [        R(                  " U5        [        R                  " 5       nU(       a  UU-   UT-   :  a  TUU-
  -
  nUS::  a   gM  )a  Wait for a response from one of the urls provided.

:param urls: List of urls to try
:param max_wait: Roughly the maximum time to wait before giving up
    The max time is *actually* len(urls)*timeout as each url will
    be tried once and given the timeout provided.
    a number <= 0 will always result in only one try
:param timeout: Timeout provided to urlopen
:param status_cb: Callable with string message when a url is not available
:param headers_cb: Callable with single argument of url to get headers
    for request.
:param headers_redact: List of header names to redact from the log
:param sleep_time: Amount of time to sleep between retries. If this and
    sleep_time_cb are None, the default sleep time defaults to 1 second
    and increases by 1 seconds every 5 tries. Cannot be specified along
    with `sleep_time_cb`.
:param exception_cb: Callable to handle exception and returns True if
    retries are permitted.
:param sleep_time_cb: Callable with 2 arguments (response, loop_n) that
    generates the next sleep time. Cannot be specified
    along with 'sleep_time`.
:param request_method: Indicates the type of HTTP request:
    GET, PUT, or POST
:param connect_synchronously: If false, enables executing requests
    in parallel
:param async_delay: Delay before parallel metadata requests, see RFC 6555

:return: tuple of (url, response contents), on failure, (False, None)

:raises: UrlError on unrecoverable error
loop_numberr9   c                    > Tb  T$ US-  S-   $ )N   r!   r5   )_rI  r  s     r'   default_sleep_time(wait_for_url.<locals>.default_sleep_time  s    '3zM9IA9MMr)   r   rC  
start_timer  c                 z    U [        S5      S4;   a  gU S:*  =(       d    [        R                  " 5       U-
  U-   U :  $ )z4Check if time is up based on start time and max waitrB  NFr   )r   r   	monotonic)rC  rO  r  s      r'   timeupwait_for_url.<locals>.timeup  s@    ed++A 
NNz)J6A	
r)   r   r%   c                    SnSnU (       a  U(       d  Sn[        [        U5      5      nX24$  U R                  R                  5         U R                  (       d;  SU R                  -  n[        [        U5      U R                  U R                  US9nX24$ U R                  5       (       d8  SU R                  -  n[        [        U5      U R                  U R                  US9nX24$ ! [        R
                  R                   aK  n[        UUR                  R                  UR                  R                  US9nU[        U5      4s SnA$ SnAff = f)z?Map requests response code/contents to internal "UrlError" typer#   NzRequest timed outr   zempty response [%s]zbad status code [%s])r   r   r   r  r   r   r  r   r   rU   r/   r}   rM   r   )r   r%   reasonurl_excrt   s        r'   handle_url_response)wait_for_url.<locals>.handle_url_response  s4    S(Fz&12G?"	# //1   *hmm<F6"]] ((	G    +x}}=F6"]] ((	G   3 "",, 	#ZZ++

**	G CF?"	#s   C# #EA EEEurl_reader_cburlsexc_cblog_cbc           	        > SnSn U " U5      u  pgT" Xv5      u  pU(       d
  [        XgSS9$  [        [        R
                  " 5       U-
  5      n
T(       a  ST-  OSnSU=(       d    [        US	S	5      < S
U
< SU< SU< 3nU" U5        [        SS[        U[        5      (       a  [        XS9S9$ SS9$ ! [         a  n	SU	-  nU	n Sn	A	NSn	A	f[         a  n	SU	-  nU	n Sn	A	NSn	A	ff = f)z:Execute request, handle response, optionally log exceptionr#   N)r@  zrequest error [%s]zunexpected error [%s]z%ss	unlimitedz	Calling 'r%   z
' failed [r,   z]: r   r%   r   r@  )	r?  r   	Exceptionr   r   rQ  getattrrF   r   )rY  rZ  rO  r[  r\  rU  r%   r   rV  rt   
time_takenmax_wait_str
status_msgrW  rC  s                r'   read_url_handle_exceptions0wait_for_url.<locals>.read_url_handle_exceptionsA  s	    
	)$/MC1(@OG&sEE  )J67
+3ux'177E511	

 	z gx00 g;	
 	
 
 	
!  	)A-FG 	,q0FG	s#   $B5 5
C)?CC)C$$C)rO   c           	      6   > [        U Tc  0 OT" U 5      TUSTS9$ )NF)rU   r   rO   r   r   )r   )r%   rO   r   r   r   s     r'   read_url_cb!wait_for_url.<locals>.read_url_cbm  s-    $,B*S/))
 	
r)   c                   >^ S[         4UU4S jjn/ nT H  n[        R                  " 5       nT
S:w  aI  T(       dB  T" TU 5      (       a  [        SSSS9s  $ Tb$  T(       a  UT-   U T-   :  a  [	        U T-   U-
  5      mT" XFXU5      nUR
                  (       a  Us  $ UR                  (       d  M  UR                  UR                  5        M     U(       a  [        U5      OSn	[        SSU	S9$ )zliterate over list of urls, request each one and handle responses
and thrown exceptions individually per url
r%   c                    > U T" U T5      4$ r   r5   )r%   rh  rO   s    r'   url_reader_serial@wait_for_url.<locals>.read_url_serial.<locals>.url_reader_serial~  s    S'233r)   r   Nr_  )	r/   r   rQ  r?  r   r   r@  r/  r   )rO  rO   r[  r\  rl  
wait_timesr%   nowoutr@  loop_nrC  must_try_againrh  re  rR  rZ  s    `        r'   read_url_serial%wait_for_url.<locals>.read_url_serialw  s    	43 	4 	4 
C.."C{>(J//* 44  (w*x*?@ ":#8C"?@G,!
FC ||
!!#--0+ , (2C
Ot	4$)LLr)   c                 :   > [        [        TTUS9nT" UTXU5      $ )zpass list of urls to dual_stack which sends requests in parallel
handle response and exceptions of the first endpoint to respond
)r&  rO   )r   r=  )	rO  rO   r[  r\  url_reader_parallelrG  rh  re  rZ  s	        r'   read_url_parallel'wait_for_url.<locals>.read_url_parallel  s4     &%	
 *z6
 	
r)   z3sleep_time and sleep_time_cb are mutually exclusiveNTFr!   r   )FN)r   )r   r   r   r   r/   r   r   r   r   r   r   ExceptionCallbackr?  r   rQ  r   r   r%   r}   r@  r
  rI   rf   )rZ  rC  rO   rD  r   r   r  r   rE  r   rF  rG  rM  rs  rw  rO  do_read_urlcalculate_sleep_timer   respcurrent_sleep_timecurrent_timerW  rq  rr  rh  re  rR  s   ``  ```  ` `          @@@@@@r'   wait_for_urlr    s\   ^N3 N5 N
 
E 
u 
'!;''!.6sm'!	x!3&	''!R*
E5#(==>>
*
 CcN#	*

 *
 "*
 *
 
*
 *
X
 
s 
{ 
 
"M	"M "MH
	
 
  !JmNOO 16G  )>,>FH
:H==88T]]3333^^JJt~~&aZF!N1(FC(J(:;;"  !		?	
 	

%& ~~'|g-
X0EE,";<G!|= r)   c                   ^    \ rS rSr     SS jrS rS rS rS rS r	S	 r
S
 rS rS rSrg)OauthUrlHelperi  Nc                 \   Xl         U=(       d    SU l        X l        X0l        XPl        SU l        SU l        U R                  U R                  U R                   4n[        U5      (       d  SU l        O[        U5      (       d  [        S5      eU R                  5       nU=(       d    0 U l        g )Nr#   TrK  FzBall or none of token_key, token_secret, or consumer_key can be set)consumer_keyconsumer_secret	token_keytoken_secretskew_data_file	_do_oauthskew_change_limitanyallr   read_skew_file	skew_data)r   r  r  r  r  r  requiredolds           r'   r   OauthUrlHelper.__init__  s     ).4""(,!"NND$5$5t7H7HI8}}"DNX* 
 !!#r)   c                    U R                   (       a  [        R                  R                  U R                   5      (       aa  [        R
                  " SU R                    35         [        U R                   SS9 n[        R                  " U5      sS S S 5        sS S S 5        $ g ! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)NzReading rmode)	r  osr2   isfiler   Timedopenjsonload)r   fps     r'   r  OauthUrlHelper.read_skew_file  s    277>>$2E2E#F#F""Xd.A.A-B#CDd###Gyy}G GDD 	G GD 	 ED s$   #B<8B"	B<"
B0	,B<<
C
c                 z   U R                   (       d  g U R                  5       nUc  0 nX#U'   [        R                  " SU R                    35         [	        U R                   SS9 nUR                  [        R                  " U5      5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)NzWriting wr  )r  r  r   r  r  rm   r  dumps)r   r[   valuecurr  s        r'   update_skew_fileOauthUrlHelper.update_skew_file  s    ""!!#;CD	$*=*=)>?@$cC
HHTZZ_%C
@@ C
 C
@@s$   B,$&B
B,
B)	%B,,
B:c                    [        U[        5      (       a   UR                  S:X  d  UR                  S:X  d  g SUR                  ;  a!  [        R                  SUR                  5        g UR                  S   n [        R                  " [        U5      5      n[        U[        R                  " 5       -
  5      n[        UR                  5      R                  nU R                  R                  US5      n[!        Xu-
  5      U R"                  :  a'  U R%                  Xe5        [        R                  SXe5        XPR                  U'   g ! [         a   n[        R                  SX$5         S nAg S nAff = f)Ni  i  datez$Missing header 'date' in %s responsez#Failed to convert datetime '%s': %sr   z$Setting oauth clockskew for %s to %d)rF   r   rM   rU   rI   rJ   r   r   r   r`  r   r   r%   netlocr  rD   absr  r  )r   r.  r  remote_timert   skewr[   old_skews           r'   r   OauthUrlHelper.exception_cb  s   y(++3&)..C*?***KK>	O  (	++io6K
 ;,-	&-->>%%dA.x$"8"88!!$-KK>K#t  	KK=tG	s   8E 
E.E))E.c           	      ^   U R                   (       d  0 $ S n[        U5      R                  nU R                  (       a=  X0R                  ;   a.  [	        [
        R
                  " 5       5      U R                  U   -   n[        UU R                  U R                  U R                  U R                  US9$ )N)r%   r  r  r  r  	timestamp)r  r   r  r  r   r   oauth_headersr  r  r  r  )r   r%   r  r[   s       r'   r   OauthUrlHelper.headers_cb*  s    ~~I	}##>>dnn4DIIK(4>>$+??I**nn** 00
 	
r)   c                     [        U R                  UR                  S5      5      US'   [        U R                  UR                  S5      5      US'   U" U0 UD6$ )Nr   r   )r   _headers_cbrD   _exception_cb)r   wrapped_funcr   rP   s       r'   _wrappedOauthUrlHelper._wrapped<  s\    &fjj6 
| ")

> :"
~ T,V,,r)   c                 .    U R                  [        X5      $ r   )r  r  r   s      r'   r  OauthUrlHelper.wait_for_urlE  s    }}\488r)   c                 .    U R                  [        X5      $ r   )r  r   r   s      r'   r   OauthUrlHelper.readurlH  s    }}Wd33r)   c                 x    Sn U(       a  U" U5      nU R                  U5        U$ ! U R                  U5        f = f)NTr   )r   extra_exception_cbr.  rets       r'   r  OauthUrlHelper._exception_cbK  s>    	)!(3i(
 i(s   & 9c                 h    0 nU(       a  U" U5      nUR                  U R                  U5      5        U$ r   )r   r   )r   extra_headers_cbr%   rU   s       r'   r  OauthUrlHelper._headers_cbT  s.    &s+Gts+,r)   )r  r  r  r  r  r  r  r  )NNNNz/run/oauth_skew.json)r   r   r   r   r   r  r  r   r   r  r  r   r  r  r   r5   r)   r'   r  r    sE     -#6&8
$-94r)   r  c           	           SS K Jn  U(       a  [	        U5      nOS nUR                  UUUUUR                  US9nUR                  U 5      u  pnU
$ ! [         a  n[        S5      UeS nAff = f)Nr   zoauth support is not available)client_secretresource_owner_keyresource_owner_secretsignature_methodr  )oauthlib.oauth1oauth1ImportErrorNotImplementedErrorr/   ClientSIGNATURE_PLAINTEXTsign)r%   r  r  r  r  r  r  rt   client_urisigned_headers_bodys               r'   r  r  \  s    K( 	N		]]%$*33  F #)++c"2D%#  K!"BCJKs   A 
A,A''A,)rZ   r   )r$  
   )Mr   r>   rb   r  loggingr  r*  r   concurrent.futuresr   r   r   email.utilsr   	functoolsr   http.clientr   	itertoolsr	   sslr
   typingr   r   r   r   r   r   r   r   r   urllib.parser   r   r   r   r   r   	cloudinitr   r   r   	getLoggerr   rI   r   r   ry  r(   r8   r   rN   r/   r   dictrv   r~   r   r   rw   rQ   r   r|   r   r   r   r   r   r+  r#  r=  r?  rf   r  r  r  r5   r)   r'   <module>r     s\     	   	   M M !  !  &
 
 
 ? >   0 0!XzlD&89: " s .3    DMS M~ M &
78&<- -3> 3
(. (
.K .Kbw $# % J '+ $ e_	J 
&*%m= $m=$ %m=& 'm=j "
38

  ??	
 E? 	* !	P-
38
P-CyP- P- 	P-
 8C=(;//0P-fj  El#))%)"&&*=A"&| | e_	|
 | "| | $| Hc5\5%89:| |  | |~| |@ LPr)   