o
    
im&                     @   s   d Z ddlZddlZddlZddlZzddlZW n ey#   dZ	Y nw ddl
mZmZ ddlZ
dZdZeeZddiZ	 d	d
 Zdd ZdddddddefddZG dd dejZG dd deZdS )z3Implements file-like objects for reading from http.    NT)
bytebuffer	constantsi   )httphttpszAccept-Encodingidentityc                 C   sB   t j| }|jtv sJ |j|j }d|d }t|j|dS )N/)schemeuri_path)	urllibparseurlsplitr   SCHEMESnetlocpathlstripdict)uri_as_string	split_urir	    r   C/home/ubuntu/.local/lib/python3.10/site-packages/smart_open/http.py	parse_uri&   s
   r   c                 C   s    t jt|}t| |fi |S N)
smart_openutilscheck_kwargsopen)urimodetransport_paramskwargsr   r   r   open_uri/   s   r    Fc
                 C   sN   |t jkr!t| ||	|||||||d
}
tjtj| j|
_	|
S t
d| )u  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
    cert: str/tuple, optional
        if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’)
    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, ``{}``.
    session: object, optional
        The requests Session object to use with http get requests.
        Can be used for OAuth2 clients.
    buffer_size: int, optional
        The buffer size to use when performing I/O.

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

    )buffer_sizekerberosuserpasswordcertheaderssession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&   r(   r'   r!   fobjr   r   r   r   4   s   
$r   c                	   @   st   e Zd ZdZdedddddddf	ddZdd Zedd	 Zd
d Z	dd Z
dd ZdddZdddZdd ZdS )BufferedInputBaseNrFc                 C   s   || _ || _|	p	t| _|rdd l}| | _n|d ur%|d ur%||f| _nd | _|| _|| _|d u r8t	
 | _n|| _|
| _| jj| j | j| jd| j| jd| _| jjsY| j  t|| _d| _d S )Nr   T)authr%   streamr&   r(   )urlr%   requestsr'   requests_kerberosHTTPKerberosAuthr3   r!   r   _HEADERScopyr&   r(   getresponseokraise_for_statusr   
ByteBuffer_read_buffer_current_pos)selfr5   r   r!   r"   r#   r$   r%   r&   r'   r(   r7   r   r   r   __init__g   s6   
	

zBufferedInputBase.__init__c                 C   s$   t d | jsd| _d| _dS dS )zFlush and close this stream.zclose: calledN)loggerdebugclosedr<   r@   rB   r   r   r   close   s
   

zBufferedInputBase.closec                 C   s
   | j d u S r   )r<   rG   r   r   r   rF      s   
zBufferedInputBase.closedc                 C      dS )z+Return True if the stream can be read from.Tr   rG   r   r   r   readable   s   zBufferedInputBase.readablec                 C   rI   )NFr   rG   r   r   r   seekable   s   zBufferedInputBase.seekablec                 C      t jzUnsupported.ioUnsupportedOperationrG   r   r   r   detach      zBufferedInputBase.detachc                 C   s   |dk rt d| td| | js|dkrdS |dkr6t| jr/| j | jj  }n&| jj }nt| j|k rO| j	| jjdkrHnt| j|k s=| j|}|  j
t|7  _
|S )z>
        Mimics the read call to a filehandle object.
        rS   zsize must be >= -1, got zreading with size: %dr       )
ValueErrorrD   rE   rF   lenr@   readr<   rawfillrA   )rB   sizeretvalr   r   r   rW      s    
zBufferedInputBase.readc                 C   s   | j |dS )zThis is the same as read().)rZ   )rW   rB   rZ   r   r   r   read1   s   zBufferedInputBase.read1c                 C   s.   |  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)rW   rV   )rB   bdatar   r   r   readinto   s
   zBufferedInputBase.readinto)rS   )__name__
__module____qualname__r<   DEFAULT_BUFFER_SIZErC   rH   propertyrF   rJ   rK   rQ   rW   r]   r`   r   r   r   r   r1   d   s    
,


r1   c                	       sb   e Zd ZdZdedddddddf	 fdd	Zddd	Zd
d Zdd ZdddZ	dddZ
  ZS )r*   z
    Implement seekable streamed reader from a web site.
    Supports Kerberos, client certificate and Basic HTTP authentication.
    r2   FNc                    sR   t  |||||||||	|

 t| jjdd| _| jjdd dk| _dS )a.  
        If Kerberos is True, will attempt to use the local Kerberos credentials.
        If cert is set, will try to use a client certificate
        Otherwise, will try to use "basic" HTTP authentication via username/password.

        If none of those are set, will connect unauthenticated.
        zContent-LengthrS   zAccept-Ranges noneN)	superrC   intr<   r&   r;   content_lengthlower	_seekable)rB   r5   r   r!   r"   r#   r$   r%   r&   r'   r(   	__class__r   r   rC      s   
z"SeekableBufferedInputBase.__init__r   c                 C   s  t d|| |tjvrtdtj |  std|tjkr#|}n|tjkr.| j	| }n
|tj
kr8| j| }| jdkrFtjj|dd}n	tjj|| jd}| j	|krW| j	S t d| j	| || _	|| jkrrd| _| j  | j	S | |}|jr|| _| j  | j	S 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 seekablerS   N)maxvalz0http seeking from current_pos: %d to new_pos: %d)rD   rE   r   WHENCE_CHOICESrU   rK   OSErrorWHENCE_STARTWHENCE_CURRENTrA   
WHENCE_ENDrj   r   r   clampr<   r@   empty_partial_requestr=   )rB   offsetwhencenew_posr<   r   r   r   seek   s:   








	

zSeekableBufferedInputBase.seekc                 C      | j S r   )rA   rG   r   r   r   tell     zSeekableBufferedInputBase.tellc                 O   r|   r   )rl   )rB   argsr   r   r   r   rK     r~   z"SeekableBufferedInputBase.seekablec                 C   rL   rM   rN   r\   r   r   r   truncate"  rR   z"SeekableBufferedInputBase.truncatec                 C   sF   | j  }|d urtj||d< | jj| j| jd| j	|| j
d}|S )NrangeT)r3   r4   r%   r&   r(   )r&   r:   r   r   make_range_stringr'   r;   r5   r3   r%   r(   )rB   	start_posr&   r<   r   r   r   rw   &  s   
z*SeekableBufferedInputBase._partial_request)r   r   )ra   rb   rc   __doc__rd   rC   r{   r}   rK   r   rw   __classcell__r   r   rm   r   r*      s    
.
r*   )r   rO   loggingos.pathr+   urllib.parser
   r6   ImportErrorMISSING_DEPSr   r   r   smart_open.utilsrd   r   	getLoggerra   rD   r9   r   r    r   BufferedIOBaser1   r*   r   r   r   r   <module>   s0   
	
0p