HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/landscape/lib/__pycache__/fetch.cpython-38.pyc
U

���c��@s�ddlZddlZddlZddlmZddlmZddlmZddl	m
Z
mZGdd�de�Z
Gdd	�d	e
�ZGd
d�de
�Zdd
iddddddddfdd�Zdd�Zddd�Zd dd�Zd!dd�Zdd�Zedkr�eejdd��dS)"�N)�OptionParser)�DeferredList)�
deferToThread)�	iteritems�
networkStringc@seZdZdS)�
FetchErrorN)�__name__�
__module__�__qualname__�rr�5/usr/lib/python3/dist-packages/landscape/lib/fetch.pyrsrc@s$eZdZdd�Zdd�Zdd�ZdS)�
HTTPCodeErrorcCs||_||_dS�N)�	http_code�body)�selfrrrrr�__init__szHTTPCodeError.__init__cCs
d|jS)NzServer returned HTTP code %d�r�rrrr�__str__szHTTPCodeError.__str__cCs
d|jS)Nz<HTTPCodeError http_code=%d>rrrrr�__repr__szHTTPCodeError.__repr__N)rr	r
rrrrrrrr
sr
c@s0eZdZdd�Zdd�Zdd�Zedd��Zd	S)
�PyCurlErrorcCs||_||_dSr)�
error_code�_message)rr�messagerrrrszPyCurlError.__init__cCsd|j|jfS)NzError %d: %s�rrrrrrr#szPyCurlError.__str__cCsd|j|jfS)Nz<PyCurlError args=(%d, '%s')>rrrrrr&s�zPyCurlError.__repr__cCs|jSr)rrrrrr*szPyCurlError.messageN)rr	r
rrr�propertyrrrrrrs
rF��iXTc
Csddl}t|t�s|�d�}t�|�}
t��}|dkr>|��}|�|jt	t
|���|r�|�|jd�|r�|�|jt
|��|�|j|
j�|r�|�d�r�|�|jt	|��|r�|�|jdd�tt|��D��|r�|�|jd�|	r�|�|jd	�|
dk	�r|�|jt	|
��|dk	�r,|�|jt	|��|�|jd
�|�|j|�|�|jd	�|�|j|�|�|jd	�|�|j|j�|�|j d�|�|j!d�z|�"�Wn<|j#k
�r�}zt$|j%d|j%d	��W5d}~XYnX|�&�}|�'|j(�}|dk�rt)||��|S)
aCRetrieve a URL and return the content.

    @param url: The url to be fetched.
    @param post: If true, the POST method will be used (defaults to GET).
    @param data: Data to be sent to the server as the POST content.
    @param headers: Dictionary of header => value entries to be used on the
        request.
    @param curl: A pycurl.Curl instance to use. If not provided, one will be
        created.
    @param cainfo: Path to the file with CA certificates.
    @param insecure: If true, perform curl using insecure option which will
        not attempt to verify authenticity of the peer's certificate. (Used
        during autodiscovery)
    @param follow: If True, follow HTTP redirects (default True).
    @param user_agent: The user-agent to set in the request.
    @param proxy: The proxy url to use for the request.
    rNzutf-8Tzhttps:cSsg|]}d|�qS)z%s: %sr)�.0Zpairrrr�
<listcomp>\szfetch.<locals>.<listcomp>F��sgzip,deflate��)*�pycurl�
isinstance�bytes�encode�io�BytesIOZCurlZsetoptZURLr�strZPOSTZ
POSTFIELDSIZE�lenZREADFUNCTION�read�
startswithZCAINFOZ
HTTPHEADER�sortedrZSSL_VERIFYPEERZFOLLOWLOCATIONZ	USERAGENTZPROXYZ	MAXREDIRSZCONNECTTIMEOUTZLOW_SPEED_LIMITZLOW_SPEED_TIMEZNOSIGNALZ
WRITEFUNCTION�writeZDNS_CACHE_TIMEOUT�ENCODINGZperform�errorr�args�getvalueZgetinfoZ	HTTP_CODEr
)�url�post�dataZheaders�cainfoZcurlZconnect_timeoutZ
total_timeoutZinsecureZfollowZ
user_agent�proxyr$�output�input�errrrr�fetch/sX


�

(

r<cOsttf|�|�S)z]Retrieve a URL asynchronously.

    @return: A C{Deferred} resulting in the URL content.
    )rr<)r2�kwargsrrr�fetch_async�sr>cKsRg}|D]:}t|f|�}|r(|�||�|r8|�||�|�|�qt|ddd�S)a9
    Retrieve a list of URLs asynchronously.

    @param callback: Optionally, a function that will be fired one time for
        each successful URL, and will be passed its content and the URL itself.
    @param errback: Optionally, a function that will be fired one time for each
        failing URL, and will be passed the failure and the URL itself.
    @return: A C{DeferredList} whose callback chain will be fired as soon as
        all downloads have terminated. If an error occurs, the errback chain
        of the C{DeferredList} will be fired immediatly.
    T)ZfireOnOneErrbackZ
consumeErrors)r>ZaddCallbackZ
addErrback�appendr)�urls�callback�errbackr=Zresultsr4�resultrrr�fetch_many_async�srDcCs.|�d��d�d}|dk	r*tj�||�}|S)z�Return the last component of the given C{url}.

    @param url: The URL to get the filename from.
    @param directory: Optionally a path to prepend to the returned filename.

    @note: Any trailing slash in the C{url} will be removed
    �/���N)�rstrip�split�os�path�join)r4�	directory�filenamerrr�url_to_filename�srNcs.�fdd�}�fdd�}t|f||d�|��S)aJ
    Retrieve a list of URLs and save their content as files in a directory.

    @param urls: The list URLs to fetch.
    @param directory: The directory to save the files to, the name of the file
        will equal the last fragment of the URL.
    @param logger: Optional function to be used to log errors for failed URLs.
    cs,t|�d�}t|d�}|�|�|��dS)N�rL�wb)rN�openr/�close)r6r4rM�fdrOrrr/�s

zfetch_to_files.<locals>.writecs�r�d|t|j�f�|S)Nz Couldn't fetch file from %s (%s))r*�value)Zfailurer4)�loggerrr�	log_error�s�z!fetch_to_files.<locals>.log_error)rArB)rD)r@rLrUr=r/rVr)rLrUr�fetch_to_files�s
rWcCsZt�}|jddd�|jddd�|�d�|�|�\}\}tt||j|j|jd��dS)	Nz--post�
store_true)�actionz--datar)�defaultz--cainfo)r5r6r7)rZ
add_option�
parse_args�printr<r5r6r7)r2�parserZoptionsr4rrr�test�s
�r^�__main__r!)NN)N)N)rI�sysr(ZoptparserZtwisted.internet.deferrZtwisted.internet.threadsrZtwisted.python.compatrr�	Exceptionrr
rr<r>rDrNrWr^r�argvrrrr�<module>s0

�
R