� Y2Pc@s�dZdZdZdZdZdZdZddlmZdd lZdd l Z dd l m Z dd l m Z eje�Zd efd ��YZd S(s�PyOpenSSL utilities including HTTPSSocket class which wraps PyOpenSSL SSL connection into a httplib-like interface suitable for use with urllib2 s P J Kershaws21/12/10s2(C) 2012 Science and Technology Facilities Councils-BSD - see LICENSE file in top-level directorysPhilip.Kershaw@stfc.ac.uks$Id$i����(tdatetimeN(tStringIO(tSSLt SSLSocketcBs.eZdZdZdd�Zd�Zed��Zej d��Zd�Z d�Z d�Z d �Z d �Zd �Zd �Zd �Zd�Zd�Zd�Zd�Zd�Zd�Zed�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"RS( s: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 cCs�|dk r||_n7tjtjtj�|_|jjtjtjd�tj||j�|_ |j j |_ d|_ dS(s�Create SSL socket object @param ctx: SSL context @type ctx: OpenSSL.SSL.Context @param sock: underlying socket object @type sock: socket.socket iiN(tNonetsockettAF_INETt SOCK_STREAMt setsockoptt SOL_SOCKETt SO_REUSEADDRRt Connectiont_SSLSocket__ssl_connt __class__tdefault_buf_sizetbuf_sizet_makefile_refs(tselftctxtsock((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyt__init__!s  cCs|j�dS(sCClose underlying socket when this object goes out of scope N(tclose(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyt__del__3scCs|jS(s1Buffer size for makefile method recv() operations(t_SSLSocket__buf_size(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR8scCs;t|ttf�s.tdt|���n||_dS(s1Buffer size for makefile method recv() operationss9Expecting int or long type for "buf_size"; got %r insteadN(t isinstancetinttlongt TypeErrorttypeR(Rtvalue((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR=scCs2|jdkr|jj�n|jd8_dS(sVShutdown the SSL connection and call the close method of the underlying socketiN(RR tshutdown(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyREs cCs|jj|�dS(s�Set the shutdown state of the Connection. @param mode: bit vector of either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN N(R t set_shutdown(Rtmode((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyRSscCs |jj�S(s�Get the shutdown state of the Connection. @return: bit vector of either or both of SENT_SHUTDOWN and RECEIVED_SHUTDOWN (R t get_shutdown(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR!ZscCs|jj|�dS(s�bind to the given address - calls method of the underlying socket @param addr: address/port number tuple @type addr: tupleN(R tbind(Rtaddr((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR"ascCs|jj|�dS(s�Listen 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 tlisten(Rtbacklog((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR$gscCs|jj�dS(slSet the connection to work in server mode. The handshake will be handled automatically by read/writeN(R tset_accept_state(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR&pscCs |jj�S(s�Accept 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 taccept(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR'uscCs|jj�dS(slSet the connection to work in client mode. The handshake will be handled automatically by read/writeN(R tset_connect_state(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR(~scCs|jj|�dS(s�Call 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 tconnect(RR#((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR)�scCs |jj�S(s�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(Rthow((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR�s cCs |jj�S(s-Renegotiate this connection's SSL parameters.(R t renegotiate(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR+�scCs |jj�S(sk@return: numbers of bytes that can be safely read from the SSL buffer. @rtype: int (R tpending(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR,�scGs|jj|�S(s�Send 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 tsend(Rtdatat flags_arg((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR-�scCs|jj|�dS(N(R tsendall(RR.((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR0�scCs|jj|�S(s�Receive 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 trecv(Rtsize((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR1�scCs|jj|�dS(s}Set this connection's underlying socket blocking _mode_. @param mode: blocking mode @type mode: int N(R t setblocking(RR ((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR3�scCs |jj�S(s_ @return: file descriptor number for the underlying socket @rtype: int (R tfileno(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR4�scGs|jj|�S(s%See socket.socket.getsockopt (R t getsockopt(Rtargs((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR5�scGs|jj|�S(ssSee socket.socket.setsockopt @return: value of the given socket option @rtype: int/string (R R(RR6((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR�scCs |jj�S(s(Return the SSL state of this connection.(R t state_string(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyR7�scGs�|jd7_|j}d}t�}tj�}yL|jj|�}x3|r}|d7}|j|�|jj|�}qKWWntj tj fk r�nXt j �t jkr�t jd|tj�|�n|jd�|S(shSpecific 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 iis-Socket.makefile %d recv calls completed in %s(RRRRtutcnowR R1twriteRtZeroReturnErrort SysCallErrortlogtgetEffectiveLeveltloggingtDEBUGtdebugtseek(RR6t _buf_sizetitstreamt startTimetdat((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pytmakefile�s$        cCs |jj�S(sC @return: the socket's own address @rtype: (R t getsockname(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyRHscCs |jj�S(sJ @return: remote address to which the socket is connected (R t getpeername(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyRIscCs |jj�S(s=Retrieve the Context object associated with this Connection. (R t get_context(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyRJ!scCs |jj�S(s0Retrieve the other side's certificate (if any) (R tget_peer_certificate(R((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyRK%sN(#t__name__t __module__t__doc__RRRRtpropertyRtsetterRRR!R"R$R&R'R(R)RR+R,R-R0R1R3R4R5RR7RGRHRIRJRK(((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyRs>                 9   (RNt __author__t__date__t __copyright__t __license__t __contact__t __revision__RR>Rt cStringIORtOpenSSLRt getLoggerRLR<tobjectR(((s>/usr/lib/python2.7/site-packages/ndg/httpsclient/ssl_socket.pyt<module>s