ó
z\Vc           @   s¸   d  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 d l Td d l m	 Z	 d d l
 Z
 d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d S(   se   M2Crypto support for Python's httplib. 

Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved.iÿÿÿÿN(   t   urlsplitt
   urlunsplit(   t   *(   t
   HTTPS_PORTt   HTTPSConnectionc           B   sG   e  Z d  Z e Z d d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sA   
    This class allows communication via SSL using M2Crypto.
    c         K   s  d  |  _ | j ƒ  } y | j d ƒ Wn t k
 r9 n Xy | j d ƒ Wn t k
 r^ n Xy | j d ƒ Wn t k
 rƒ n X| r™ t d ƒ ‚ n  y5 | d |  _ t |  j t j ƒ sÍ t	 |  j ƒ ‚ Wn# t
 k
 ró t j d ƒ |  _ n Xt j |  | | | ƒ d  S(   Nt   key_filet	   cert_filet   ssl_contexts   unknown keyword argumentt   sslv23(   t   Nonet   sessiont   keyst   removet
   ValueErrort   ssl_ctxt
   isinstancet   SSLt   Contextt   AssertionErrort   KeyErrort   HTTPConnectiont   __init__(   t   selft   hostt   portt   strictt   sslR   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR      s,    	(c         C   s  d  } xè t j |  j |  j d t j ƒ D]Å \ } } } } } d  } z yj t j |  j d | ƒ} |  j	 d  k	 rƒ | j
 |  j	 ƒ n  | j |  j |  j f ƒ | |  _ d  } d  SWn t j k
 rÎ } | } n XWd  | d  k	 rì | j ƒ  n  Xq( W| d  k rt d ƒ ‚ n  | ‚ d  S(   Ni    t   familys"   Empty list returned by getaddrinfo(   R	   t   sockett   getaddrinfoR   R   t   SOCK_STREAMR   t
   ConnectionR   R
   t   set_sessiont   connectt   sockt   errort   closeR   (   R   R#   R   t   _R"   t   e(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR!   .   s(    4	c         C   s   d  S(   N(    (   R   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR$   K   s    c         C   s   |  j  j ƒ  S(   N(   R"   t   get_session(   R   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR'   \   s    c         C   s   | |  _  d  S(   N(   R
   (   R   R
   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR    _   s    N(   t   __name__t
   __module__t   __doc__R   t   default_portR	   R   R!   R$   R'   R    (    (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR      s   			t   HTTPSc           B   s    e  Z e Z d  d d d „ Z RS(   t    c         K   sx   t  j |  | | | ƒ y | d |  _ Wn# t k
 rL t j d ƒ |  _ n Xt |  j t ƒ se t	 ‚ |  j |  j _ d  S(   NR   R   (
   t   HTTPR   R   R   R   R   R   t   _connR   R   (   R   R   R   R   R   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR   g   s    N(   R(   R)   R   t   _connection_classR	   R   (    (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR,   c   s   t   ProxyHTTPSConnectionc           B   sˆ   e  Z d  Z i d d 6d d 6Z d Z d Z d d d d d „ Z d d d	 „ Z d
 „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z RS(   s   
    An HTTPS Connection that uses a proxy and the CONNECT request.

    When the connection is initiated, CONNECT is first sent to the proxy (along
    with authorization headers, if supplied). If successful, an SSL connection
    will be established over the socket through the proxy and to the target
    host.

    Finally, the actual request is sent over the SSL connection tunneling
    through the proxy.
    iP   t   httpi»  t   httpss   Proxy-Authorizations
   User-Agentc         K   sA   t  j |  | | | |  | |  _ | |  _ d |  _ d |  _ d S(   s†   
        Create the ProxyHTTPSConnection object.

        host and port are the hostname and port number of the proxy server.
        N(   R   R   t	   _usernamet	   _passwordR	   t   _proxy_autht	   _proxy_UA(   R   R   R   R   t   usernamet   passwordR   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR   ƒ   s
    			i    c         C   sA  t  | ƒ \ } } } } }	 | s1 t d | ‚ n  y | j d ƒ \ }
 } Wn t k
 rc | } n Xy | j d ƒ \ } } WnI t k
 rÈ | } y |  j | } WqÉ t k
 rÄ t d | ‚ qÉ Xn X| |  _ t | ƒ |  _ t d  d  | | |	 f ƒ } t
 j d k  r$t j |  | | | ƒ n t j |  | | | | ƒ d  S(   Ns   unknown URL type: %st   @t   :s   unknown protocol for: %si   i   (   i   i   (   R    R   t   splitt   _portsR   t
   _real_hostt   intt
   _real_portR   R	   t   syst   version_infoR   t
   putrequest(   R   t   methodt   urlt	   skip_hostt   skip_accept_encodingt   protot   netloct   patht   queryt   fragmentt   username_passwordt	   host_portR   R   t   rest(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyRC   ‘   s*    
	c         C   se   | j  ƒ  |  j j  ƒ  k r' | |  _ n  | j  ƒ  |  j j  ƒ  k rN | |  _ n t j |  | | ƒ d  S(   N(   t   lowert
   _UA_HEADERR7   t   _AUTH_HEADERR6   R   t	   putheader(   R   t   headert   value(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyRS   °   s
    c         O   s2   |  j  s |  j ƒ  |  _  n  t j |  | | Ž d  S(   N(   R6   t   _encode_authR   t
   endheaders(   R   t   argst   kwargs(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyRW   ¹   s    	c         C   s|   t  j |  ƒ |  j j |  j ƒ  ƒ t |  j ƒ } | j ƒ  | j } | d k rn |  j ƒ  t	 j
 d | ‚ n  |  j ƒ  d  S(   NiÈ   s   Proxy connection failed: %d(   R   R!   R"   t   sendallt   _get_connect_msgt   HTTPResponset   begint   statusR$   R   R#   t
   _start_ssl(   R   t   responset   code(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR!   Â   s    
	
c         C   sŠ   d |  j  |  j f } | d |  j  |  j f } |  j rV | d |  j |  j f } n  |  j r| | d |  j |  j f } n  | d } | S(   s6    Return an HTTP CONNECT request to send to the proxy. s   CONNECT %s:%d HTTP/1.1
s   Host: %s:%d
s   %s: %s
s   
(   R>   R@   R7   RQ   R6   RR   (   R   t   msg(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR[   Ò   s    		
c         C   sF   t  j |  j |  j ƒ |  _ |  j j ƒ  |  j j ƒ  |  j j ƒ  d S(   s*    Make this connection's socket SSL-aware. N(   R   R   R   R"   t	   setup_sslt   set_connect_statet   connect_ssl(   R   (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR_   Ý   s    c         C   sO   |  j  o |  j s d Sd |  j  |  j f } t j | ƒ j d d ƒ } d | S(   s>    Encode the username and password for use in the auth header. s   %s:%ss   
R-   s   Basic %sN(   R4   R5   R	   t   base64t   encodestringt   replace(   R   t   userpasst   enc_userpass(    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyRV   ä   s
    N(   R(   R)   R*   R=   RR   RQ   R	   R   RC   RS   RW   R!   R[   R_   RV   (    (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyR1   q   s   								(   R*   t   stringRA   R   t   urlparseR    R   Rf   t   httplibR   R   R   R   R.   R,   R1   (    (    (    s7   /usr/lib64/python2.7/site-packages/M2Crypto/httpslib.pyt   <module>   s   
U