
    $TXg-                       d 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d
lmZ ddlmZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ erddl0m1Z1m2Z2  ee3      Z4dZ5 e6d      Z7 G d de       Z8d'dZ9ed(d       Z:d)dZ; G d d e<      Z= G d! d"e#e=#      Z> G d$ d%e      Z?y&)*z>Requests session configured with all accepted scheme adapters.    )annotations)fnmatch)cache)	getLogger)local)TYPE_CHECKING   )
CondaError)dals)CONDA_HOMEPAGE_URL)context)add_username_and_passwordget_proxy_username_and_passsplit_anaconda_tokenurlparse)
ProxyErrorChannel   )read_binstar_tokens   )	AuthBaseBaseAdapterCaseInsensitiveDictRetrySession_basic_auth_strextract_cookies_to_jarget_auth_from_urlget_netrc_auth)
FTPAdapter)HTTPAdapter)LocalFSAdapter)	S3Adapter)PreparedRequestRequest)httphttpsftps3filec                      e Zd Zd Zd Zy)EnforceUnusedAdapterc                J    t        d|j                   d      }t        |      )Nz.
        EnforceUnusedAdapter called with url zL
        This command is using a remote connection in offline mode.
        )r   urlRuntimeError)selfrequestargskwargsmessages        Alib/python3.12/site-packages/conda/gateways/connection/session.pysendzEnforceUnusedAdapter.send=   s1    ..5kk] ;	
 7##    c                    t               N)NotImplementedError)r1   s    r6   closezEnforceUnusedAdapter.closeF   s    !##r8   N)__name__
__module____qualname__r7   r<    r8   r6   r-   r-   <   s    $$r8   r-   c                @    t        j                  |       j                  S )zO
    Given a URL, determine the channel it belongs to and return its name.
    )r   from_urlcanonical_name)r/   s    r6   get_channel_name_from_urlrD   J   s     C ///r8   c                N   t        |       }|
t               S i }t        j                  D ]  }|j	                  dd      }||k(  r|}t        |       }t        |      }|j                  |j                  k7  rM|j                  |j                  z   }|j                  |j                  z   }t        ||      s|} |j	                  dd      j                         xs d}	|	
t               S t        j                  j                  |	      }
|
s
t               S t         |
|            S )zt
    Function that determines the correct Session object to be returned
    based on the URL that is passed in.
    Nchannel auth)rH   )rD   CondaSessionr   channel_settingsgetr   schemenetlocpathr   stripplugin_managerget_auth_handler)r/   channel_namerJ   settingsrF   
parsed_urlparsed_settingurl_without_schemapatternauth_handlerauth_handler_clss              r6   get_sessionrZ   Q   s&    -S1L ~ ,,,,y"-l"' c]
!'*   J$5$55'..@ ''.*=*==%w/'' -* $''399;CtL ~-->>|L~-l;<<r8   c                    | yt        | t              rt        |       S t        |       }|j                   d|j
                   d| j                   S )z_
    Function that determines which storage key to use for our CondaSession object caching
    default.z::)
isinstancetuplehashtyper>   r?   rR   )rH   	auth_types     r6   get_session_storage_keyrc      sU     |$DzT
I""#1Y%;%;$<Bt?P?P>QRRr8   c                  ,     e Zd ZdZ fdZ fdZ xZS )CondaSessionTypez
    Takes advice from https://github.com/requests/requests/issues/1871#issuecomment-33327847
    and creates one Session instance per thread.
    c                B    t               |d<   t        | 	  | |||      S )N_thread_local)r   super__new__)mcsnamebasesdct	__class__s       r6   ri   zCondaSessionType.__new__   s$    $wOwsD%55r8   c                6   t        |j                  d            }	 | j                  j                  |   S # t        $ r& t        |   di |}||i| j                  _        Y |S t        $ r, t        |   di |x}| j                  j                  |<   Y |S w xY w)NrH   r@   )rc   rK   rg   sessionsAttributeErrorrh   __call__KeyError)clsr4   storage_keysessionrn   s       r6   rr   zCondaSessionType.__call__   s    -fjj.@A	$$--k:: 	@g&00G*5w)?C&   	@E@P AA Gc''00= 	s   6 +B$0BB)r=   r>   r?   __doc__ri   rr   __classcell__rn   s   @r6   re   re      s    
6 r8   re   c                  B     e Zd Z	 d	 d fdZd fdZed        Z xZS )rI   c                   t         |           |xs
 t               | _        | j                  j                  t        j                         d}t        j                  dk(  r,	 ddl	}ddl
}|j                  |j                        }d| _        nt        j                  | _        t        j                   rSt#               }| j%                  d|       | j%                  d|       | j%                  d|       | j%                  d	|       nt'        t        j(                  t        j*                  g d
dd      }t-        ||      }| j%                  d|       | j%                  d|       | j%                  dt/                      | j%                  d	t1                      | j%                  dt3                      t        j4                  | j6                  d<   t        j8                  r&t        j:                  t        j8                  f| _        yt        j:                  rt        j:                  | _        yy# t        $ r t        d      w xY w)zY
        :param auth: Optionally provide ``requests.AuthBase`` compliant objects
        N
truststorer   zNThe `ssl_verify: truststore` setting is only supported onPython 3.10 or later.Tzhttp://zhttps://zftp://zs3://)i  i  i  i  F)totalbackoff_factorstatus_forcelistraise_on_statusrespect_retry_after_header)max_retriesssl_contextzfile://z
User-Agent)rh   __init__CondaHttpAuthrH   proxiesupdater   proxy_servers
ssl_verifysslr|   
SSLContextPROTOCOL_TLS_CLIENTImportErrorr
   verifyoffliner-   mountr   remote_max_retriesremote_backoff_factorr"   r!   r$   r#   
user_agentheadersclient_ssl_cert_keyclient_ssl_certcert)	r1   rH   r   r   r|   unused_adapterretryhttp_adapterrn   s	           r6   r   zCondaSession.__init__   s    	+MO	G112-
!(33C4K4KL DK!,,DK??13NJJy.1JJz>2JJx0JJw/ 00&<<!5 %+0E '5kRLJJy,/JJz<0JJx.JJw	,

9n./%,%7%7\"&& 00'2M2MNDI$$//DI %K   , s   "#H, ,Ic                f   t        |j                        }|j                  dv rt        i t        j
                  j                  |j                        t        j
                  j                  |j                  |j                        |j                  xs i       |_
        t        | 1  |      S )N)r(   r'   )host)r   rN   )r   r/   rL   r   r   rP   get_cached_session_headersrM   get_cached_request_headersrN   r   rh   prepare_request)r1   r2   r/   rn   s      r6   r   zCondaSession.prepare_request   s    w{{#::**1	,,GG ZZ H 	
 ,,GG ZZchh H 	 ,"	GO w&w//r8   c                l    	 | j                   j                  j                          y # t        $ r Y y w xY wr:   )rg   rp   clearrq   )rt   s    r6   cache_clearzCondaSession.cache_clear   s1    	&&,,. 		s   $' 	33r:   )rH   z!AuthBase | tuple[str, str] | None)r2   r&   returnr%   )r=   r>   r?   r   r   classmethodr   rx   ry   s   @r6   rI   rI      s/     37;0/;0z0$  r8   rI   )	metaclassc                  B    e Zd Zd Zed        Zed        Zed        Zy)r   c                    t         j                  |j                        |_        | j                  |       |j	                  d| j
                         |S )Nresponse)r   add_binstar_tokenr/   _apply_basic_authregister_hook
handle_407)r1   r2   s     r6   rr   zCondaHttpAuth.__call__  s?    #55gkkBw'j$//:r8   c                    t        | j                        }t        |      r|nd }|t        | j                        }t	        |t
              r#t        |      dk(  rt        | | j                  d<   | S )Nr   Authorization)	r   r/   anyr    r^   r_   lenr   r   )r2   url_authrH   s      r6   r   zCondaHttpAuth._apply_basic_auth  s^     %W[[1x=xd<!'++.DdE"s4yA~/>/EGOOO,r8   c                &   t        |       \  }}|st        j                  rpt               j	                         D ]U  \  }}|j                  |      st        j                  d|       ddlm	}  ||      }||_
        |j                  d      c S  | S )Nz"Adding anaconda token for url <%s>r	   r   T)with_credentials)r   r   add_anaconda_tokenr   items
startswithlogdebugmodels.channelr   tokenr/   )r/   	clean_urlr   binstar_urlr   rF   s         r6   r   zCondaHttpAuth.add_binstar_token  s    /4	533&9&;&A&A&C"U''4IIBIN9%i0G$)GM";;;== 'D 
r8   c           	        | j                   dk7  r| S | j                   | j                          |j                  d      }t	        | j
                        j                  }||vrt        t        d|dt         d            ||   }t        |      \  }}t        |||      }t        ||      }|||<   ||d<   | j                  j                         }t        |j                   | j                  | j"                         |j%                  |j                          ||j&                  d<    | j(                  j*                  |fi |}	|	j,                  j/                  |        ||	_        |	S )a  
        Prompts the user for the proxy username and password and modifies the
        proxy in the session object to include it.

        This method is modeled after
          * requests.auth.HTTPDigestAuth.handle_401()
          * requests.auth.HTTPProxyAuth
          * the previous conda.fetch.handle_proxy_407()

        It both adds 'username:password' to the proxy URL, as well as adding a
        'Proxy-Authorization' header.  If any of this is incorrect, please file an issue.

        i  r   z(
            Could not find a proxy for z. See
            z/docs/html#configure-conda-for-use-behind-a-proxy-server
            for more information on how to configure proxies.
            zProxy-Authorization)status_codecontentr<   popr   r/   rL   r   r   r   r   r   r   r2   copyr   _cookiesrawprepare_cookiesr   
connectionr7   historyappend)
r   r4   r   proxy_scheme	proxy_urlusernamepasswordproxy_authorization_headerprep	_responses
             r6   r   zCondaHttpAuth.handle_407+  sc   $ 3&O 	**Y'-44w&((4'7 8  !  L)	8F(-i8L	%4Xx%H" )#y$$&t}}h.>.>MT]]+.H*+,H'',,T<V<	  * 	r8   N)r=   r>   r?   rr   staticmethodr   r   r   r@   r8   r6   r   r     sC         8 8r8   r   N)r/   strr   z
str | None)r/   r   )r   r   )@rw   
__future__r   r   	functoolsr   loggingr   	threadingr   typingr   rG   r
   
auxlib.ishr   base.constantsr   base.contextr   
common.urlr   r   r   r   
exceptionsr   r   r   anaconda_clientr   r   r   r   r   r   r   r   r   r    adapters.ftpr!   adapters.httpr"   adapters.localfsr#   adapters.s3r$   requests.modelsr%   r&   r=   r   RETRIES	frozensetCONDA_SESSION_SCHEMESr-   rD   rZ   rc   ra   re   rI   r   r@   r8   r6   <module>r      s    E "         0 #  % % 1
 
 
 % & , "8
 " $; $0 .= .=bSt 4V7&6 Vr_H _r8   