ó
ŒGUc           @   s#  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
 k r” d d l m Z d d l m Z d d l m Z n0 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 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s¯   ndg_httpsclient HTTPS module containing PyOpenSSL implementation of
httplib.HTTPSConnection

PyOpenSSL utility to make a httplib-like interface suitable for use with 
urllib2
s   P J Kershaw (STFC)s   09/12/11s2   (C) 2012 Science and Technology Facilities Councils-   BSD - see LICENSE file in top-level directorys   Philip.Kershaw@stfc.ac.uks   $Id$iÿÿÿÿNi    i   (   t
   HTTPS_PORT(   t   HTTPConnection(   t   AbstractHTTPHandler(   t   SSL(   t	   SSLSockett   HTTPSConnectionc           B   sG   e  Z d  Z e Z e j Z d d e	 j
 d d „ Z d „  Z d „  Z RS(   sT  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
    c         C   su   t  j |  | | | | ƒ t |  d ƒ s4 d  |  _ n  | d  k	 rq t | t j ƒ se t d | ƒ ‚ n  | |  _ n  d  S(   Nt   ssl_contextsL   Expecting OpenSSL.SSL.Context type for "ssl_context" keyword; got %r instead(	   R   t   __init__t   hasattrt   NoneR   t
   isinstanceR   t   Contextt	   TypeError(   t   selft   hostt   portt   strictt   timeoutR   (    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyR   5   s    c         C   sÊ   t  |  d d ƒ rI t |  j t j ƒ s= t d |  j ƒ ‚ n  |  j } n t j |  j j ƒ } t	 j
 |  j |  j f |  j ƒ } t  |  d d ƒ r§ | |  _ |  j ƒ  n  t | | ƒ |  _ |  j j ƒ  d S(   s.   Create SSL socket and connect to peer
        R   sN   Expecting OpenSSL.SSL.Context type for "ssl_context" attribute; got %r insteadt   _tunnel_hostN(   t   getattrR	   R
   R   R   R   R   t	   __class__t   default_ssl_methodt   sockett   create_connectionR   R   R   t   sockt   _tunnelR   t   set_connect_state(   R   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyt   connectC   s    !	c         C   s&   t  |  j d ƒ r" |  j j ƒ  n  d S(   s)   Close socket and shut down SSL connectiont   closeN(   R   R   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyR   [   s    N(   t   __name__t
   __module__t   __doc__R    t   default_portR   t   SSLv23_METHODR   R	   R   t   _GLOBAL_DEFAULT_TIMEOUTR   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyR   $   s   		t   HTTPSContextHandlerc           B   s,   e  Z d  Z e j Z d d „ Z d „  Z RS(   sS   HTTPS handler that allows a SSL context to be set for the SSL
    connections.
    i    c         C   sf   t  j |  | ƒ | d k	 rM t | t j ƒ sA t d | ƒ ‚ n  | |  _ n t j t j ƒ |  _ d S(   sµ   
        @param ssl_context:SSL context
        @type ssl_context: OpenSSL.SSL.Context
        @param debuglevel: debug level for HTTPSHandler
        @type debuglevel: int
        sL   Expecting OpenSSL.SSL.Context type for "ssl_context" keyword; got %r insteadN(	   R   R   R	   R
   R   R   R   R   t   TLSv1_METHOD(   R   R   t
   debuglevel(    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyR   g   s    c         C   s2   t  d t t f i |  j d 6ƒ } |  j | | ƒ S(   sc   Opens HTTPS request
        @param req: HTTP request
        @return: HTTP Response object
        t   CustomHTTPSContextConnectionR   (   t   typeR   t   objectR   t   do_open(   R   t   reqt   customHTTPSContextConnection(    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyt
   https_openy   s    	(   R   R   R   R   t   do_request_t   https_requestR   R,   (    (    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyR#   a   s   	(   R   t
   __author__t   __date__t   __copyright__t   __license__t   __contact__t   __revision__t   loggingR   t   syst   version_infot   http.clientR    R   t   urllib.requestR   t   httplibt   urllib2t   OpenSSLR   t   ndg.httpsclient.ssl_socketR   t	   getLoggerR   t   logR   R#   (    (    (    s9   /usr/lib/python2.7/dist-packages/ndg/httpsclient/https.pyt   <module>   s*   =