
    )hF+                        d Z 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dlmZ ddlmZ ddlmZ ddlmZ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! ddl"m#Z#m$Z$m%Z%  ejL                  e'      Z(dedee)   fdZ*dedee+   fdZ,	 d%dedede+dee)   dee)   de
e-   fdZ.de+de+fdZ/de+de+de+fdZ0dedede+fdZ1	 	 d&dededee)   dee+   def
d Z2 G d! d"      Z3 G d# d$      Z4y)'z(Download files with progress indicators.    N)
HTTPStatus)BinaryIOIterableOptionalTuple)Response)ReadTimeoutError)get_download_progress_renderer)IncompleteDownloadErrorNetworkConnectionError)PyPI)Link)is_from_cache)
PipSession)HEADERSraise_for_statusresponse_chunks)format_sizeredact_auth_from_urlsplitextrespreturnc                 h    	 t        | j                  d         S # t        t        t        f$ r Y y w xY w)Nzcontent-length)intheaders
ValueErrorKeyError	TypeErrorr   s    Q/var/www/html/venv/lib/python3.12/site-packages/pip/_internal/network/download.py_get_http_response_sizer!      s5    4<< 0122), s    11c                 l    | j                   j                  d| j                   j                  d            S )z
    Return either the ETag or Last-Modified header (or None if neither exists).
    The return value can be used in an If-Range header.
    etagzlast-modified)r   getr   s    r    (_get_http_response_etag_or_last_modifiedr%       s*    
 <<FDLL$4$4_$EFF    linkprogress_bartotal_lengthrange_startc                 `   |j                   t        j                  k(  r|j                  }n|j                  }t        |      }|r1|r| dt        |       dt        |       d}n| dt        |       d}t        |       rt        j                  d|       n/|rt        j                  d|       nt        j                  d|       t        j                         t        j                  kD  rd}nt        |       rd}n|sd}n
|d	kD  rd}nd}t        |       }|s|S t        |||
      }	 |	|      S )Nz (/)zUsing cached %szResuming download %szDownloading %sFTi   )bar_typesizeinitial_progress)netlocr   file_storage_domainshow_urlurl_without_fragmentr   r   r   loggerinfogetEffectiveLevelloggingINFOr   r
   )
r   r'   r(   r)   r*   url
logged_urlshow_progresschunksrenderers
             r    _prepare_downloadr?   (   s%    {{d...mm''%c*J,b[!9 :!K<U;VVWX  '<r+l*C)DAFJT%z2	*J7$j1!GLL0	t			$T"F-L;H Fr&   filenamec                 @    t         j                  j                  |       S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)r@   s    r    sanitize_content_filenamerE   [   s     77H%%r&   content_dispositiondefault_filenamec                     t         j                  j                         }| |d<   |j                  d      }|rt	        t        |            }|xs |S )z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    content-typer@   )emailmessageMessage	get_paramrE   str)rF   rG   mr@   s       r    parse_content_dispositionrP   b   sM    
 	A+An{{:&H -S];'''r&   c                    |j                   }| j                  j                  d      }|rt        ||      }t	        |      d   }|s6t        j                  | j                  j                  dd            }|r||z  }|sL|j                  | j                  k7  r3t        j                  j	                  | j                        d   }|r||z  }|S )zoGet an ideal filename from the given HTTP response, falling back to
    the link filename if not provided.
    zcontent-disposition   rI    )
r@   r   r$   rP   r   	mimetypesguess_extensionr:   rB   rC   )r   r'   r@   rF   exts        r    _get_http_response_filenamerW   q   s     }}H,,**+@A,-@(K!(+A.C''(8(8(LMOH488txx'ggtxx(+OHOr&   sessionif_rangec                 f   |j                   j                  dd      d   }t        j                         }|r	d| d|d<   |r||d<   	 | j	                  ||d	      }t        |       |S # t        $ r?}|j                  J t        j                  d
|j                  j                  |        d }~ww xY w)N#rR   r   zbytes=-RangezIf-RangeT)r   streamzHTTP error %s while getting %s)r:   splitr   copyr$   r   r   responser5   criticalstatus_code)rX   r'   r*   rY   
target_urlr   r   es           r    _http_get_downloadrf      s     Q'*JllnG#K=2&
{{:wt{D
 K	 " zz%%%8!**:P:PRVWs   A( (	B01:B++B0c                       e Zd ZdedededdfdZdededeeef   fd	Z	d
e
dedededee   defdZdee   dededefdZd
e
dededee   deddfdZd
e
dedeeee   ee   f   fdZy)
DownloaderrX   r(   resume_retriesr   Nc                 F    |dk\  sJ d       || _         || _        || _        y )Nr   z<Number of max resume retries must be bigger or equal to zero)_session_progress_bar_resume_retriesselfrX   r(   ri   s       r    __init__zDownloader.__init__   s5     a	JI	J)-r&   r'   locationc                    t        | j                  |      }t        |      }|j                  j	                  dd      }t        ||      }t        j                  j                  ||      }t        |d      5 }| j                  |||d|      }	|r|	|k  r| j                  |||||	       ddd       ||fS # 1 sw Y   ||fS xY w)z.Download the file given by link into location.zContent-TyperS   wbr   N)rf   rk   r!   r   r$   rW   rB   rC   joinopen_process_response_attempt_resume)
ro   r'   rq   r   r)   content_typer@   filepathcontent_filebytes_receiveds
             r    __call__zDownloader.__call__   s    !$--6 /t4||'';.tT:77<<(3(D! 	\!33dL!\N  =$$$lN	 %%	 %%s   62B44C r   rz   r{   r)   c                 p    t        ||| j                  ||      }| j                  ||t        |            S )z6Process the response and write the chunks to the file.)r*   )allow_partial)r?   rl   _write_chunks_to_filebool)ro   r   r'   rz   r{   r)   r=   s          r    rv   zDownloader._process_response   sE     #$**Ln
 ))L\0B * 
 	
r&   r=   r~   c                    d}	 |D ]!  }|t        |      z  }|j                  |       # 	 |S # t        $ r$}|s|t        j	                  d       Y d}~|S d}~ww xY w)zEWrite the chunks to the file and return the number of bytes received.r   z'Connection timed out while downloading.N)lenwriter	   r5   warning)ro   r=   rz   r~   r{   chunkre   s          r    r   z Downloader._write_chunks_to_file   sv     
	F *#e*,""5)*    	F  NNDEE	Fs   &- 	AAAc                 T   t        |      }| j                  }|r|r||k  r|dz  }t        j                  dt	        |      t	        |      | j                  |z
         	 t        | j                  |||      }|j                  t        j                  k7  }	|	r| j                  ||      \  }}}|| j                  |||||      z  }|r|r||k  r|r>||k  r8t        j                   |j"                         t%        |||| j                        yy# t        t        t        f$ r Y w xY w)z9Attempt to resume the download if connection was dropped.rR   z<Attempting to resume incomplete download (%s/%s, attempt %d))r*   rY   )retriesN)r%   rm   r5   r   r   rf   rk   rc   r   PARTIAL_CONTENT_reset_download_staterv   ConnectionErrorr	   OSErrorrB   removenamer   )
ro   r   r'   rz   r)   r{   etag_or_last_modifiedattempts_leftresume_respmust_restarts
             r    rw   zDownloader._attempt_resume   sE    !I N,,},1NQMNNNN+L)%%5	0MM .2	  +66*:T:TT22;M HNL2G $"8"8|^\# 9 },1NF N\9IIl''()nlD<P<P  :<	 $%5w? s   A&D D'&D'c                 ~    |j                  d       |j                          d}t        |      }t        |      }|||fS )zCReset the download state to restart downloading from the beginning.r   )seektruncater!   r%   )ro   r   rz   r{   r)   r   s         r    r   z Downloader._reset_download_state  sF     	!.t4 H N|-BBBr&   )__name__
__module____qualname__r   rN   r   rp   r   r   r|   r   r   r   rv   r   bytesr   r   rw   r    r&   r    rh   rh      s8   .. . 	.
 
.&T &S &U38_ &0

 
 	

 
 sm
 

 uo5=QU	&33 3 	3
 sm3 3 
3jCC C 
sHSM8C=0	1	Cr&   rh   c                   X    e Zd ZdedededdfdZdee   dedee	ee	eef   f      fd	Z
y)
BatchDownloaderrX   r(   ri   r   Nc                 (    t        |||      | _        y )N)rh   _downloaderrn   s       r    rp   zBatchDownloader.__init__,  s     &g|^Lr&   linksrq   c              #   T   K   |D ]  }| j                  ||      \  }}|||ff ! yw)z0Download the files given by links into location.N)r   )ro   r   rq   r'   ry   rx   s         r    r|   zBatchDownloader.__call__4  s>       	1D%)%5%5dH%E"Hl<000	1s   &()r   r   r   r   rN   r   rp   r   r   r   r|   r   r&   r    r   r   +  sj    MM M 	M
 
M1d^1/21	%eCHo-.	/1r&   r   )r   )r   N)5__doc__email.messagerJ   r8   rT   rB   httpr   typingr   r   r   r   pip._vendor.requests.modelsr   pip._vendor.urllib3.exceptionsr	   pip._internal.cli.progress_barsr
   pip._internal.exceptionsr   r   pip._internal.models.indexr   pip._internal.models.linkr   pip._internal.network.cacher   pip._internal.network.sessionr   pip._internal.network.utilsr   r   r   pip._internal.utils.miscr   r   r   	getLoggerr   r5   r   r!   rN   r%   r   r?   rE   rP   rW   rf   rh   r   r   r&   r    <module>r      sw   .    	  6 6 0 ; J T + * 5 4 R R P P			8	$( x} G8 G G "#0
0
0 0 3-	0
 #0 e_0f& & &(3 (# (RU (h d s 0 "#"	
 # sm	
 0JC JCZ1 1r&   