
    lcB(                         d Z ddlZddlZddlZddlZ	 ddlZn# e$ r dZ	Y nw xY wddl
mZmZ ddlZ
dZdZ ej        e          ZddiZ	 d	 Zd
 ZddZ G d dej                  Z G d de          ZdS )z3Implements file-like objects for reading from http.    NT)
bytebuffer	constantsi   )httphttpszAccept-Encodingidentityc                     t           j                            |           }|j        t          v sJ |j        |j        z   }d|                    d          z   }t          |j        |          S )N/)schemeuri_path)	urllibparseurlsplitr
   SCHEMESnetlocpathlstripdict)uri_as_string	split_urir   s      /lib/python3.11/site-packages/smart_open/http.py	parse_urir   &   sf    %%m44Iw&&&&).0HX__S)))Hy'(;;;;    c                 h    t           j                            t          |          }t          | |fi |S N)
smart_openutilscheck_kwargsopen)urimodetransport_paramskwargss       r   open_urir#   /   s3    **41ABBFT$$V$$$r   Fc           	         |t           j        k    r^t          | ||||||          }t          j                            t          j                            |           j                  |_	        |S t          d|z            )aO  Implement streamed reader from a web site.

    Supports Kerberos and Basic HTTP authentication.

    Parameters
    ----------
    url: str
        The URL to open.
    mode: str
        The mode to open using.
    kerberos: boolean, optional
        If True, will attempt to use the local Kerberos credentials
    user: str, optional
        The username for authenticating over HTTP
    password: str, optional
        The password for authenticating over HTTP
    headers: dict, optional
        Any headers to send in the request. If ``None``, the default headers are sent:
        ``{'Accept-Encoding': 'identity'}``. To use no headers at all,
        set this variable to an empty dict, ``{}``.

    Note
    ----
    If neither kerberos or (user, password) are set, will connect
    unauthenticated, unless set separately in headers.

    )kerberosuserpasswordheaderstimeoutz(http support for mode %r not implemented)r   READ_BINARYSeekableBufferedInputBaseosr   basenamer   r   urlparsenameNotImplementedError)r   r    r%   r&   r'   r(   r)   fobjs           r   r   r   4   s~    8 y$$ U('7
 
 
 G$$V\%:%:3%?%?%DEE	!"Lt"STTTr   c                   R    e Zd ZdedddddfdZd Zd Zd Zd Zdd
Z	ddZ
d ZdS )BufferedInputBaserFNc	                    |rdd l }	|	                                }
n||||f}
nd }
|| _        || _        |t                                          | _        n|| _        || _        t          j	        ||
d| j        | j                  | _
        | j
        j        s| j
                                         | j
                            | j                  | _        t          j        |          | _        d| _        d | _        d S )Nr   Tauthstreamr(   r)   )requests_kerberosHTTPKerberosAuthbuffer_sizer    _HEADERScopyr(   r)   requestsgetresponseokraise_for_statusiter_content
_read_iterr   
ByteBuffer_read_buffer_current_posraw)selfurlr    r;   r%   r&   r'   r(   r)   r9   r7   s              r   __init__zBufferedInputBase.__init__\   s    	$$$$$5577DD 	( 	(#DDD&	 	##==??DLL"DL LL
 
 
 } 	-M**,,,-44T5EFF&1+>>
 r   c                 V    t                               d           d| _        d| _        dS )zFlush and close this stream.zclose: calledN)loggerdebugr@   rD   rI   s    r   closezBufferedInputBase.close   s&    _%%%r   c                     dS )z+Return True if the stream can be read from.T rO   s    r   readablezBufferedInputBase.readable   s    tr   c                     dS )NFrR   rO   s    r   seekablezBufferedInputBase.seekable   s    ur   c                     t           j        zUnsupported.ioUnsupportedOperationrO   s    r   detachzBufferedInputBase.detach       %%r   c                 "   t                               d|           | j        dS |dk    rdS |dk     r8t          | j                  dk    r | j        j                                        }n|dk     r9| j                                        | j        j                                        z   }nt          | j                  |k     rt                               d| j        |           | j                            | j	                  }|dk    r8| j                                        }| xj        t          |          z  c_        |S t          | j                  |k     | j                            |          }| xj        t          |          z  c_        |S )z>
        Mimics the read call to a filehandle object.
        zreading with size: %dNr   r   z:http reading more content at current_pos: %d with size: %d)
rM   rN   r@   lenrF   rH   readrG   fillrD   )rI   sizeretval
bytes_reads       r   r`   zBufferedInputBase.read   s    	,d333= 	319 	23AX 	2#d/00A5 	2]&++--FFAX 	2&++--0A0F0F0H0HHFFd'((4/ "P%t   ".33DODD
? "!.3355F%%V4%%!M d'((4/ " &++D11FS[[(r   c                 .    |                      |          S )zThis is the same as read().)rb   )r`   rI   rb   s     r   read1zBufferedInputBase.read1   s    yydy###r   c                     |                      t          |                    }|sdS ||dt          |          <   t          |          S )zLRead up to len(b) bytes into b, and return the number of bytes
        read.r   N)r`   r_   )rI   bdatas      r   readintozBufferedInputBase.readinto   sG     yyQ   	1*3t99*4yyr   )r]   )__name__
__module____qualname__DEFAULT_BUFFER_SIZErK   rP   rS   rU   r[   r`   rg   rk   rR   r   r   r3   r3   [   s        !$2EdT4QU& & & &V      & & &! ! ! !F$ $ $ $    r   r3   c                   L    e Zd ZdZdedddddfdZddZd Zd	 Zdd
Z	ddZ
dS )r+   zr
    Implement seekable streamed reader from a web site.
    Supports Kerberos and Basic HTTP authentication.
    r4   FNc	                 :   || _         |rddl}	|	                                | _        n||
||f| _        nd| _        |t                                          | _        n|| _        || _        || _        || _	        | 
                                | _        | j        j        s| j                                         t                              d| j        | j        j                   t#          | j        j                            dd                    | _        | j        j                            dd                                          dk    | _        | j                            | j                  | _        t1          j        |          | _        d| _        d| _        dS )	z
        If Kerberos is True, will attempt to use the local Kerberos credentials.
        Otherwise, will try to use "basic" HTTP authentication via username/password.

        If none of those are set, will connect unauthenticated.
        r   Nzself.response: %r, raw: %rzContent-Lengthr]   zAccept-Ranges none)rJ   r9   r:   r7   r<   r=   r(   r)   r;   r    _partial_requestr@   rA   rB   rM   rN   rH   intr?   content_lengthlower	_seekablerC   rD   r   rE   rF   rG   )
rI   rJ   r    r;   r%   r&   r'   r(   r)   r9   s
             r   rK   z"SeekableBufferedInputBase.__init__   sx     	$$$$)::<<DII 	( 	x(DIIDI 	##==??DLL"DL&	--//} 	-M**,,,14=$-BSTTT!$-"7";";<Lb"Q"QRR .22?BGGMMOOSYY-44T5EFF&1+>>
 r   r   c                    t                               d||           |t          j        vrt	          dt          j        z            |                                 st          d          |t          j        k    r|}n5|t          j        k    r| j	        |z   }n|t          j
        k    r
| j        |z   }| j        dk    r"t          j                            |d          }n&t          j                            || j                  }| j	        |k    r| j	        S t                               d| j	        |           || _	        || j        k    r(d| _        d| _        | j                                         nh|                     |          }|j        rE|| _        | j                            | j                  | _        | j                                         nd| _        | j	        S )zSeek to the specified position.

        :param int offset: The offset in bytes.
        :param int whence: Where the offset is from.

        Returns the position after seeking.z seeking to offset: %r whence: %rz"invalid whence, expected one of %rzstream is not seekabler]   N)maxvalz0http seeking from current_pos: %d to new_pos: %d)rM   rN   r   WHENCE_CHOICES
ValueErrorrU   OSErrorWHENCE_STARTWHENCE_CURRENTrG   
WHENCE_ENDrv   r   r   clampr@   rD   rF   emptyrt   rA   rC   r;   )rI   offsetwhencenew_posr@   s        r   seekzSeekableBufferedInputBase.seek  s    	7HHH11 	^AID\\]]]}} 	42333Y++ 	3GGy// 	3'&0GGy++ 	3)F2G"$ 	R &,,WT,BBGG &,,WT=P,QQG' 	%$$GIZ\cddd#d)) 	% DM"DO##%%%%,,W55H{ % ("&-"<"<T=M"N"N!'')))) $  r   c                     | j         S r   )rG   rO   s    r   tellzSeekableBufferedInputBase.tell6  s      r   c                     | j         S r   )rx   )rI   argsr"   s      r   rU   z"SeekableBufferedInputBase.seekable9  s
    ~r   c                     t           j        rW   rX   rf   s     r   truncatez"SeekableBufferedInputBase.truncate<  r\   r   c                     |9| j                             dt          j                            |          i           t          j        | j        | j        d| j         | j	                  }|S )NrangeTr6   )
r(   updater   r   make_range_stringr>   r?   rJ   r7   r)   )rI   	start_posr@   s      r   rt   z*SeekableBufferedInputBase._partial_request@  sk     	ZL**:*L*LY*W*W XYYY<HLL
 
 
 r   )r   r   )rl   rm   rn   __doc__ro   rK   r   r   rU   r   rt   rR   r   r   r+   r+      s         
 "%2EdT4QU2 2 2 2h.! .! .! .!`! ! !  & & & &     r   r+   )FNNNN)r   rY   loggingos.pathr,   urllib.parser   r>   ImportErrorMISSING_DEPSr   r   r   smart_open.utilsro   r   	getLoggerrl   rM   r<   r   r#   r   BufferedIOBaser3   r+   rR   r   r   <module>r      sh   : 9 				      OOOO   LLL - , , , , , , ,      
		8	$	$ z*< < <% % %
$U $U $U $UNn n n n n) n n nb     1     s    ##