
    \jm&                         d Z ddlZddlZddlZddlZ	 ddlZ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dddddefdZ G d dej.                        Z G d de      Zy# e$ r dZ	Y cw xY w)z3Implements file-like objects for reading from http.    NT)
bytebuffer	constantsi   )httphttpszAccept-Encodingidentityc                     t         j                  j                  |       }|j                  t        v sJ |j
                  |j                  z   }d|j                  d      z   }t        |j                  |      S )N/)schemeuri_path)	urllibparseurlsplitr
   SCHEMESnetlocpathlstripdict)uri_as_string	split_urir   s      9/root/env/lib/python3.12/site-packages/smart_open/http.py	parse_urir   &   sd    %%m4Iw&&&)..0HX__S))Hy''(;;    c                 d    t         j                  j                  t        |      }t        | |fi |S N)
smart_openutilscheck_kwargsopen)urimodetransport_paramskwargss       r   open_urir#   /   s-    **41ABFT$V$$r   Fc
                    |t         j                  k(  rbt        | ||	|||||||
      }
t        j                  j                  t        j                  j                  |       j                        |
_	        |
S t        d|z        )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%   fobjs              r   r   r   4   sw    H y$$$(;tWg

 GG$$V\\%:%:3%?%D%DE	!"Lt"STTr   c            	       h    e Zd ZdZdedddddddf	dZd Zed        Zd Z	d Z
d	 Zdd
ZddZd Zy)BufferedInputBaseNrFc                 b   || _         || _        |	xs t        | _        |rdd l}|j                         | _        n||
||f| _        nd | _        || _        || _        |t        j                         | _        n|| _        |
| _        | j                  j                  | j                   | j                  | j                  d| j                  | j                        | _        | j                  j                  s| j                  j!                          t#        j$                  |      | _        d| _        y )Nr   T)authr)   streamr*   r,   )urlr)   requestsr+   requests_kerberosHTTPKerberosAuthr9   r%   r    _HEADERScopyr*   r,   getresponseokraise_for_statusr   
ByteBuffer_read_buffer_current_pos)selfr;   r    r%   r&   r'   r(   r)   r*   r+   r,   r=   s               r   __init__zBufferedInputBase.__init__g   s     	*($)::<DI("6x(DIDI&	?#==?DL"DL((HHLLLL ) 
 }}MM**,&11+>r   c                 d    t         j                  d       | j                  sd| _        d| _        yy)zFlush and close this stream.zclose: calledN)loggerdebugclosedrB   rF   rH   s    r   closezBufferedInputBase.close   s)    _%{{ DM $D r   c                     | j                   d u S r   )rB   rN   s    r   rM   zBufferedInputBase.closed   s    }}$$r   c                      y)z+Return True if the stream can be read from.T rN   s    r   readablezBufferedInputBase.readable   s    r   c                      y)NFrR   rN   s    r   seekablezBufferedInputBase.seekable   s    r   c                 "    t         j                  zUnsupported.ioUnsupportedOperationrN   s    r   detachzBufferedInputBase.detach       %%%r   c                    |dk  rt        d|       t        j                  d|       | j                  s|dk(  ry|dk(  rzt	        | j
                        r@| j
                  j                         | j                  j                  j                         z   }n| j                  j                  j                         }nt	        | j
                        |k  rL| j
                  j                  | j                  j                        dk(  rnt	        | j
                        |k  rL| j
                  j                  |      }| xj                  t	        |      z  c_
        |S )z>
        Mimics the read call to a filehandle object.
        zsize must be >= -1, got zreading with size: %dr   r   )
ValueErrorrK   rL   rM   lenrF   readrB   rawfillrG   )rH   sizeretvals      r   ra   zBufferedInputBase.read   s    "97v>??,d3;;$!)2:4$$%**//1DMM4E4E4J4J4LL**//1 d''(4/$$))$--*;*;<A d''(4/ &&++D1FS[(r   c                 &    | j                  |      S )zThis is the same as read().)rd   )ra   rH   rd   s     r   read1zBufferedInputBase.read1   s    yydy##r   c                 n    | j                  t        |            }|sy||dt        |       t        |      S )zLRead up to len(b) bytes into b, and return the number of bytes
        read.r   N)ra   r`   )rH   bdatas      r   readintozBufferedInputBase.readinto   s5     yyQ *3t94yr   )r^   )__name__
__module____qualname__rB   DEFAULT_BUFFER_SIZErI   rO   propertyrM   rS   rU   r[   ra   rh   rl   rR   r   r   r6   r6   d   sZ    H!$2EdTtT'X% % %&4$r   r6   c            	       Z     e Zd ZdZdedddddddf	 fd	ZddZd Zd Zdd	Z	dd
Z
 xZS )r.   z
    Implement seekable streamed reader from a web site.
    Supports Kerberos, client certificate and Basic HTTP authentication.
    r7   FNc                    t         |   |||||||||	|

       t        | j                  j                  j                  dd            | _        | j                  j                  j                  dd      j                         dk7  | _        y)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-Lengthr^   zAccept-Ranges noneN)	superrI   intrB   r*   rA   content_lengthlower	_seekable)rH   r;   r    r%   r&   r'   r(   r)   r*   r+   r,   	__class__s              r   rI   z"SeekableBufferedInputBase.__init__   s{     	dK44QXZacjk!$--"7"7";";<Lb"QR ..22?BGMMOSYYr   c                    t         j                  d||       |t        j                  vrt	        dt        j                  z        | j                         st        d      |t        j                  k(  r|}nE|t        j                  k(  r| j                  |z   }n"|t        j                  k(  r| j                  |z   }| j                  dk(  r"t        j                  j                  d      }n+t        j                  j                  | j                        }| j                  |k(  r| j                  S t         j                  d| j                  |       || _	        || j                  k(  r-d| _        | j                   j#                          | j                  S | j%                  |      }|j&                  r-|| _        | j                   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 seekabler^   N)maxvalz0http seeking from current_pos: %d to new_pos: %d)rK   rL   r   WHENCE_CHOICESr_   rU   OSErrorWHENCE_STARTWHENCE_CURRENTrG   
WHENCE_ENDrx   r   r   clamprB   rF   empty_partial_requestrC   )rH   offsetwhencenew_posrB   s        r   seekzSeekableBufferedInputBase.seek   s    	7H111AID\D\\]]}}233Y+++Gy///''&0Gy+++))F2G"$ &&,,WT,BG &&,,WT=P=P,QG'$$$GIZIZ\cd#d))) DM##%     ,,W5H{{ (!!'')     !%   r   c                     | j                   S r   )rG   rN   s    r   tellzSeekableBufferedInputBase.tell  s       r   c                     | j                   S r   )rz   )rH   argsr"   s      r   rU   z"SeekableBufferedInputBase.seekable  s    ~~r   c                 "    t         j                  rW   rX   rg   s     r   truncatez"SeekableBufferedInputBase.truncate"  r\   r   c                    | j                   j                         }|"t        j                  j	                  |      |d<   | j
                  j                  | j                  | j                  d| j                  || j                        }|S )NrangeT)r9   r:   r)   r*   r,   )r*   r@   r   r   make_range_stringr+   rA   r;   r9   r)   r,   )rH   	start_posr*   rB   s       r   r   z*SeekableBufferedInputBase._partial_request&  sw    ,,##% )//AA)LGG<<##HHLL $ 
 r   )r   r   )rm   rn   ro   __doc__rp   rI   r   r   rU   r   r   __classcell__)r{   s   @r   r.   r.      s@    
 "%2EdTtTZ(,!\!&r   r.   )r   rY   loggingos.pathr/   urllib.parser   r<   ImportErrorMISSING_DEPSr   r   r   smart_open.utilsrp   r   	getLoggerrm   rK   r?   r   r#   r   BufferedIOBaser6   r.   rR   r   r   <module>r      s    : 	    -   
			8	$ z*<%
 #4tT?R-U`m)) m`_ 1 _G  Ls   A2 2A<;A<