
    Bfi$                         d Z dZdZdZdZdZdZddlmZ dd	lZdd	l	Z	dd
l
mZ ddlmZ  ej                  e      Z G d de      Zy	)zPyOpenSSL utilities including HTTPSSocket class which wraps PyOpenSSL
SSL connection into a httplib-like interface suitable for use with urllib2

zP J Kershawz21/12/10z2(C) 2012 Science and Technology Facilities Councilz-BSD - see LICENSE file in top-level directoryzPhilip.Kershaw@stfc.ac.ukz$Id$    )datetimeN)BytesIO)SSLc                       e Zd ZdZdZd!dZd Zed        Zej                  d        Zd Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZefdZd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"y)"	SSLSocketa:  SSL Socket class wraps pyOpenSSL's SSL.Connection class implementing
    the makefile method so that it is compatible with the standard socket
    interface and usable with httplib.

    @cvar default_buf_size: default buffer size for recv operations in the
    makefile method
    @type default_buf_size: int
    i    Nc                    ||| _         npt        j                   t         j                  t         j                        | _         | j                   j                  t         j                  t         j
                  d       t        j                  || j                         | _        | j                  j                  | _        d| _        y)zCreate SSL socket object

        @param ctx: SSL context
        @type ctx: OpenSSL.SSL.Context
        @param sock: underlying socket object
        @type sock: socket.socket
        N   r   )socketAF_INETSOCK_STREAM
setsockopt
SOL_SOCKETSO_REUSEADDRr   
Connection_SSLSocket__ssl_conn	__class__default_buf_sizebuf_size_makefile_refs)selfctxsocks      l/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/ndg/httpsclient/ssl_socket.py__init__zSSLSocket.__init__!   s~     DK --8J8JKDKKK""6#4#4f6I6I1M..dkk:77    c                 $    | j                          y)zCClose underlying socket when this object goes out of scope
        N)closer   s    r   __del__zSSLSocket.__del__3   s     	

r   c                     | j                   S )1Buffer size for makefile method recv() operations)_SSLSocket__buf_sizer   s    r   r   zSSLSocket.buf_size8   s     r   c                 `    t        |t              st        dt        |      z        || _        y)r!   z1Expecting int type for "buf_size"; got %r insteadN)
isinstanceint	TypeErrortyper"   )r   values     r   r   zSSLSocket.buf_size=   s3     %% -/3E{; < <r   c                     | j                   dk  r	 | j                  j                          y| xj                   dz  c_         y# t        j                  t        j
                  f$ r Y yw xY w)zVShutdown the SSL connection and call the close method of the
        underlying socketr	   N)r   r   shutdownr   ErrorSysCallErrorr   s    r   r   zSSLSocket.closeE   s]     "((*
 1$	 IIs//0 s   A #A('A(c                 :    | j                   j                  |       y)zSet the shutdown state of the Connection.
        @param mode: bit vector of either or both of SENT_SHUTDOWN and
        RECEIVED_SHUTDOWN
        N)r   set_shutdownr   modes     r   r.   zSSLSocket.set_shutdownQ   s    
 	$$T*r   c                 6    | j                   j                         S )zGet the shutdown state of the Connection.
        @return: bit vector of either or both of SENT_SHUTDOWN and
        RECEIVED_SHUTDOWN
        )r   get_shutdownr   s    r   r2   zSSLSocket.get_shutdownX   s    
 ++--r   c                 :    | j                   j                  |       y)zbind to the given address - calls method of the underlying socket
        @param addr: address/port number tuple
        @type addr: tupleN)r   bindr   addrs     r   r4   zSSLSocket.bind_   s     	T"r   c                 :    | j                   j                  |       y)zListen for connections made to the socket.

        @param backlog: specifies the maximum number of queued connections and
        should be at least 1; the maximum value is system-dependent (usually 5).
        @param backlog: int
        N)r   listen)r   backlogs     r   r8   zSSLSocket.listene   s     	w'r   c                 8    | j                   j                          y)zlSet the connection to work in server mode. The handshake will be
        handled automatically by read/writeN)r   set_accept_stater   s    r   r;   zSSLSocket.set_accept_staten   s     	((*r   c                 6    | j                   j                         S )zAccept an SSL connection.

        @return: pair (ssl, addr) where ssl is a new SSL connection object and
        addr is the address bound to the other end of the SSL connection.
        @rtype: tuple
        )r   acceptr   s    r   r=   zSSLSocket.accepts   s     %%''r   c                 8    | j                   j                          y)zlSet the connection to work in client mode. The handshake will be
        handled automatically by read/writeN)r   set_connect_stater   s    r   r?   zSSLSocket.set_connect_state|   s     	))+r   c                 :    | j                   j                  |       y)zCall the connect method of the underlying socket and set up SSL on
        the socket, using the Context object supplied to this Connection object
        at creation.

        @param addr: address/port number pair
        @type addr: tuple
        N)r   connectr5   s     r   rA   zSSLSocket.connect   s     	%r   c                 6    | j                   j                         S )a  Send the shutdown message to the Connection.

        @param how: for socket.socket this flag determines whether read, write
        or both type operations are supported.  OpenSSL.SSL.Connection doesn't
        support this so this parameter is IGNORED
        @return: true if the shutdown message exchange is completed and false
        otherwise (in which case you call recv() or send() when the connection
        becomes readable/writeable.
        @rtype: bool
        )r   r*   )r   hows     r   r*   zSSLSocket.shutdown   s     ''))r   c                 6    | j                   j                         S )z-Renegotiate this connection's SSL parameters.)r   renegotiater   s    r   rE   zSSLSocket.renegotiate       **,,r   c                 6    | j                   j                         S )zk@return: numbers of bytes that can be safely read from the SSL
        buffer.
        @rtype: int
        )r   pendingr   s    r   rH   zSSLSocket.pending   s    
 &&((r   c                 8    | j                   j                  |      S )zSend data to the socket. Nb. The optional flags argument is ignored.
        - retained for compatibility with socket.socket interface

        @param data: data to send down the socket
        @type data: string
        )r   send)r   data	flags_args      r   rJ   zSSLSocket.send   s     ##D))r   c                 :    | j                   j                  |       y N)r   sendall)r   rK   s     r   rO   zSSLSocket.sendall   s    %r   c                 8    | j                   j                  |      S )zReceive data from the Connection.

        @param size: The maximum amount of data to be received at once
        @type size: int
        @return: data received.
        @rtype: string
        )r   recv)r   sizes     r   rQ   zSSLSocket.recv   s     ##D))r   c                 :    | j                   j                  |       y)z}Set this connection's underlying socket blocking _mode_.

        @param mode: blocking mode
        @type mode: int
        N)r   setblockingr/   s     r   rT   zSSLSocket.setblocking   s     	##D)r   c                 6    | j                   j                         S )z_
        @return: file descriptor number for the underlying socket
        @rtype: int
        )r   filenor   s    r   rV   zSSLSocket.fileno   s    
 %%''r   c                 4     | j                   j                  | S )z%See socket.socket.getsockopt
        )r   
getsockoptr   argss     r   rX   zSSLSocket.getsockopt   s     *t))400r   c                 4     | j                   j                  | S )zsSee socket.socket.setsockopt

        @return: value of the given socket option
        @rtype: int/string
        )r   r   rY   s     r   r   zSSLSocket.setsockopt   s     *t))400r   c                 6    | j                   j                         S )z(Return the SSL state of this connection.)r   state_stringr   s    r   r]   zSSLSocket.state_string   s    ++--r   c                 B   | xj                   dz  c_         | j                  }d}t               }t        j                         }	 | j
                  j                  |      }|r4|dz  }|j                  |       | j
                  j                  |      }|r4t        j                         t        j                  k  r,t        j                  d|t        j                         |z
         |j!                  d       |S # t        j                  t        j                  f$ r Y w xY w)ah  Specific to Python socket API and required by httplib: convert
        response into a file-like object.  This implementation reads using recv
        and copies the output into a StringIO buffer to simulate a file object
        for consumption by httplib

        Nb. Ignoring optional file open mode (StringIO is generic and will
        open for read and write unless a string is passed to the constructor)
        and buffer size - httplib set a zero buffer size which results in recv
        reading nothing

        @return: file object for data returned from socket
        @rtype: cStringIO.StringO
        r	   r   z-Socket.makefile %d recv calls completed in %s)r   r   r   r   utcnowr   rQ   writer   ZeroReturnErrorr,   loggetEffectiveLevelloggingDEBUGdebugseek)r   rZ   	_buf_sizeistream	startTimedats          r   makefilezSSLSocket.makefile   s     	q  MM	
OO%		//&&y1C1S!oo**95    "gmm3IIEqoo')35
 	A ##S%5%56 	
 	s   AC8 8#DDc                 6    | j                   j                         S )zC
        @return: the socket's own address
        @rtype:
        )r   getsocknamer   s    r   ro   zSSLSocket.getsockname  s    
 **,,r   c                 6    | j                   j                         S )zJ
        @return: remote address to which the socket is connected
        )r   getpeernamer   s    r   rq   zSSLSocket.getpeername  s     **,,r   c                 6    | j                   j                         S )z=Retrieve the Context object associated with this Connection. )r   get_contextr   s    r   rs   zSSLSocket.get_context  rF   r   c                 6    | j                   j                         S )z0Retrieve the other side's certificate (if any)  )r   get_peer_certificater   s    r   ru   zSSLSocket.get_peer_certificate  s    3355r   rN   )#__name__
__module____qualname____doc__r   r   r   propertyr   setterr   r.   r2   r4   r8   r;   r=   r?   rA   r*   rE   rH   rJ   rO   rQ   rT   rV   rX   r   r]   rm   ro   rq   rs   ru    r   r   r   r      s      $
   __   
%+.#(+
(,
&*-)*& ) **(1
1.,\---6r   r   )ry   
__author____date____copyright____license____contact____revision__r   rd   r
   ior   OpenSSLr   	getLoggerrv   rb   objectr   r|   r   r   <module>r      sY    
D=)     g!D6 D6r   