
    Bfi                        d Z dZdZdZdZdZdZddlZddlZddl	Z	e	j                  d   d	kD  rdd
lmZ ddlmZ ddlmZ ndd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ  ej,                  e      Z G d de      Z G d de      Zy)zndg_httpsclient HTTPS module containing PyOpenSSL implementation of
httplib.HTTPSConnection

PyOpenSSL utility to make a httplib-like interface suitable for use with 
urllib2
zP J Kershaw (STFC)z09/12/11z2(C) 2012 Science and Technology Facilities Councilz-BSD - see LICENSE file in top-level directoryzPhilip.Kershaw@stfc.ac.ukz$Id$    N   )
HTTPS_PORT)HTTPConnection)AbstractHTTPHandler)SSL)	SSLSocketc                   \    e Zd ZdZeZej                  Zdde	j                  dfdZd Zd Zy)HTTPSConnectionaT  This class allows communication via SSL using PyOpenSSL.
    It is based on httplib.HTTPSConnection, modified to use PyOpenSSL.

    Note: This uses the constructor inherited from HTTPConnection to allow it to
    be used with httplib and HTTPSContextHandler. To use the class directly with
    an SSL context set ssl_context after construction.
    
    @cvar default_port: default port for this class (443)
    @type default_port: int
    @cvar default_ssl_method: default SSL method used if no SSL context is
    explicitly set - defaults to version 2/3.
    @type default_ssl_method: int
    Nc                     t        j                  | ||||       t        | d      sd | _        |0t	        |t
        j                        st        d|z        || _        y y )Nssl_contextLExpecting OpenSSL.SSL.Context type for "ssl_context" keyword; got %r instead)r   __init__hasattrr   
isinstancer   Context	TypeError)selfhostportstricttimeoutr   s         g/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/ndg/httpsclient/https.pyr   zHTTPSConnection.__init__5   sh    dD&'Bt]+#D"k3;;7 !G +!, - -  +D #    c                    t        | dd      rIt        | j                  t        j                        st        d| j                  z        | j                  }n)t        j                  | j                  j                        }t        j                  | j                  | j                  f| j                        }t        | dd      r|| _        | j                          t        ||      | _        | j                  j!                          y)z.Create SSL socket and connect to peer
        r   NzNExpecting OpenSSL.SSL.Context type for "ssl_context" attribute; got %r instead_tunnel_host)getattrr   r   r   r   r   	__class__default_ssl_methodsocketcreate_connectionr   r   r   sock_tunnelr   set_connect_state)r   r   r!   s      r   connectzHTTPSConnection.connectC   s     4-d..< !I $ 0 0!1 2 2 **K++dnn&G&GHK''DII(>M 4.DILLNk40	 			##%r   c                 f    t        | j                  d      r| j                  j                          yy)z)Close socket and shut down SSL connectioncloseN)r   r!   r&   )r   s    r   r&   zHTTPSConnection.close[   s#    499g&IIOO 'r   )__name__
__module____qualname____doc__r   default_portr   TLSv1_2_METHODr   r   _GLOBAL_DEFAULT_TIMEOUTr   r$   r&    r   r   r
   r
   $   s9     L++"&t77T+&0r   r
   c                   N    e Zd ZdZej
                  Zej                  Z	ddZ
d Zy)HTTPSContextHandlerzSHTTPS handler that allows a SSL context to be set for the SSL
    connections.
    c                     t        j                  | |       |0t        |t        j                        st        d|z        || _        yt        j                  | j                  j                        | _        y)z
        @param ssl_context:SSL context
        @type ssl_context: OpenSSL.SSL.Context
        @param debuglevel: debug level for HTTPSHandler
        @type debuglevel: int
        Nr   )	r   r   r   r   r   r   r   r   
SSL_METHOD)r   r   
debuglevels      r   r   zHTTPSContextHandler.__init__i   sh     	$$T:6"k3;;7 !G +!, - -  +D"{{4>>+D+DEDr   c                 l    t        dt        t        fd| j                  i      }| j	                  ||      S )zcOpens HTTPS request
        @param req: HTTP request
        @return: HTTP Response object
        CustomHTTPSContextConnectionr   )typer
   objectr   do_open)r   reqcustomHTTPSContextConnections      r   
https_openzHTTPSContextHandler.https_open{   s=     (,,J-<f,E-:D<L<L,M(O$ ||8#>>r   N)r   )r'   r(   r)   r*   r   do_request_https_requestr   r,   r2   r   r;   r.   r   r   r0   r0   a   s*     (33M##JF$
?r   r0   )r*   
__author____date____copyright____license____contact____revision__loggingr   sysversion_infohttp.clientr   r   urllib.requestr   httpliburllib2OpenSSLr   ndg.httpsclient.ssl_socketr   	getLoggerr'   logr
   r0   r.   r   r   <module>rO      s    "
D=)   
A&*2"&+  0g!:n :z$?- $?r   