U
    beG*                     @   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k
rH   dZ	Y nX 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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kst|j|j }d|d }t	|j|dS )N/)schemeuri_path)
urllibparseurlsplitr   SCHEMESAssertionErrornetlocpathlstripdict)Zuri_as_stringZ	split_urir	    r   Q/var/www/html/services/stratfitenv/lib/python3.8/site-packages/smart_open/http.py	parse_uri&   s
    r   c                 C   s   t jt|}t| |f|S N)
smart_openutilsZcheck_kwargsopen)urimodeZtransport_paramskwargsr   r   r   open_uri/   s    r   Fc	           
      C   sP   |t jkr@t| ||||||||d	}	tjtj| j|	_	|	S t
d| dS )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, ``{}``.
    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timeoutz(http support for mode %r not implementedN)r   ZREAD_BINARYSeekableBufferedInputBaseosr   basenamer
   r   urlparsenameNotImplementedError)
r   r   r   r    r!   r"   r#   r$   r   Zfobjr   r   r   r   4   s    !
      r   c                   @   sb   e Zd ZdeddddddfddZ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 )BufferedInputBaserFNc
                 C   s   |rdd l }
|
 }n|d k	r0|d k	r0||f}nd }|| _|| _|d krTt | _n|| _|	| _tj	|||d| j| jd| _
| j
js| j
  | j
| j| _t|| _d| _d | _d S )Nr   T)authr"   streamr#   r$   )requests_kerberosHTTPKerberosAuthr   r   _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#   r$   r/   r-   r   r   r   __init__b   s4    

	
zBufferedInputBase.__init__c                 C   s   t d d| _d| _dS )zFlush and close this stream.zclose: calledN)loggerdebugr5   r9   r>   r   r   r   close   s    
zBufferedInputBase.closec                 C   s   dS )z+Return True if the stream can be read from.Tr   rC   r   r   r   readable   s    zBufferedInputBase.readablec                 C   s   dS )NFr   rC   r   r   r   seekable   s    zBufferedInputBase.seekablec                 C   s
   t jdS zUnsupported.NioUnsupportedOperationrC   r   r   r   detach   s    zBufferedInputBase.detachc                 C   s   t d| | jdkrdS |dkr&dS |dk rJt| jdkrJ| jj }n|dk rj| j | jj  }nbt| j|k rt d| j| | j| j	}|dkrj| j }|  jt|7  _|S qj| j|}|  jt|7  _|S )z>
        Mimics the read call to a filehandle object.
        zreading with size: %dN    r   z:http reading more content at current_pos: %d with size: %d)
rA   rB   r5   lenr;   r=   readr<   fillr9   )r>   sizeretvalZ
bytes_readr   r   r   rO      s.    
 
zBufferedInputBase.readc                 C   s   | j |dS )zThis is the same as read().)rQ   )rO   r>   rQ   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)rO   rN   )r>   bdatar   r   r   readinto   s
    zBufferedInputBase.readinto)rL   )rL   )__name__
__module____qualname__DEFAULT_BUFFER_SIZEr@   rD   rE   rF   rK   rO   rT   rW   r   r   r   r   r+   a   s       
-
#
r+   c                   @   sX   e Zd ZdZdeddddddfddZddd	Zd
d Zdd ZdddZ	dddZ
dS )r%   z
    Implement seekable streamed reader from a web site.
    Supports Kerberos, client certificate and Basic HTTP authentication.
    r,   FNc
                 C   s   || _ |rddl}
|
 | _n"|dk	r:|dk	r:||f| _nd| _|dkrTt | _n|| _|| _|	| _|| _	|| _
|  | _| jjs| j  td| j| jj t| jjdd| _| jjdd dk| _| j| j	| _t|| _d| _d| _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.
        r   Nzself.response: %r, raw: %rzContent-LengthrL   zAccept-Ranges none)r?   r/   r0   r-   r1   r2   r#   r"   r$   r   r   _partial_requestr5   r6   r7   rA   rB   r=   intr4   content_lengthlower	_seekabler8   r9   r   r:   r;   r<   )r>   r?   r   r   r   r    r!   r"   r#   r$   r/   r   r   r   r@      s0    


z"SeekableBufferedInputBase.__init__r   c                 C   s&  t d|| |tjkr&tdtj |  s6td|tjkrF|}n*|tjkr\| j	| }n|tj
krp| j| }| jdkrtjj|dd}ntjj|| jd}| j	|kr| j	S t d| j	| || _	|| jkrd| _d| _| j  n:| |}|jr|| _| 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 seekablerL   N)maxvalz0http seeking from current_pos: %d to new_pos: %d)rA   rB   r   ZWHENCE_CHOICES
ValueErrorrF   OSErrorZWHENCE_STARTZWHENCE_CURRENTr<   Z
WHENCE_ENDr`   r   r   clampr5   r9   r;   emptyr^   r6   r8   r   )r>   offsetwhencenew_posr5   r   r   r   seek  s:    








zSeekableBufferedInputBase.seekc                 C   s   | j S r   )r<   rC   r   r   r   tellA  s    zSeekableBufferedInputBase.tellc                 O   s   | j S r   )rb   )r>   argsr   r   r   r   rF   D  s    z"SeekableBufferedInputBase.seekablec                 C   s
   t jdS rG   rH   rS   r   r   r   truncateG  s    z"SeekableBufferedInputBase.truncatec                 C   sD   |d k	r | j dtj|i tj| j| jd| j	| j | j
d}|S )NrangeT)r-   r.   r"   r#   r$   )r#   updater   r   Zmake_range_stringr3   r4   r?   r-   r"   r$   )r>   	start_posr5   r   r   r   r^   K  s    z*SeekableBufferedInputBase._partial_request)r   )N)N)rX   rY   rZ   __doc__r[   r@   rk   rl   rF   rn   r^   r   r   r   r   r%      s       
7
0
r%   )rr   rI   loggingos.pathr&   urllib.parser
   r3   ImportErrorZMISSING_DEPSr   r   r   Zsmart_open.utilsr[   r   	getLoggerrX   rA   r1   r   r   r   BufferedIOBaser+   r%   r   r   r   r   <module>   s.   

		  
-s