Blame SOURCES/m2crypto-0.21.1-timeouts.patch

71c899
diff -urN M2Crypto/M2Crypto/SSL/Connection.py M2Crypto-0.21.1/M2Crypto/SSL/Connection.py
71c899
--- M2Crypto/M2Crypto/SSL/Connection.py	2013-11-26 20:01:02.591964970 +0100
71c899
+++ M2Crypto-0.21.1/M2Crypto/SSL/Connection.py	2013-11-26 20:01:19.204950349 +0100
71c899
@@ -47,9 +47,11 @@
71c899
             self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
71c899
             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
71c899
         self._fileno = self.socket.fileno()
71c899
-        
71c899
-        self.blocking = self.socket.gettimeout()
71c899
-        
71c899
+
71c899
+        self._timeout = self.socket.gettimeout()
71c899
+        if self._timeout is None:
71c899
+            self._timeout = -1.0
71c899
+
71c899
         self.ssl_close_flag = m2.bio_noclose
71c899
 
71c899
         
71c899
@@ -147,7 +149,7 @@
71c899
         m2.ssl_set_accept_state(self.ssl)
71c899
 
71c899
     def accept_ssl(self):
71c899
-        return m2.ssl_accept(self.ssl)
71c899
+        return m2.ssl_accept(self.ssl, self._timeout)
71c899
 
71c899
     def accept(self):
71c899
         """Accept an SSL connection. The return value is a pair (ssl, addr) where
71c899
@@ -169,7 +171,7 @@
71c899
         m2.ssl_set_connect_state(self.ssl)
71c899
 
71c899
     def connect_ssl(self):
71c899
-        return m2.ssl_connect(self.ssl)
71c899
+        return m2.ssl_connect(self.ssl, self._timeout)
71c899
 
71c899
     def connect(self, addr):
71c899
         self.socket.connect(addr)
71c899
@@ -196,7 +198,7 @@
71c899
         return m2.ssl_pending(self.ssl)
71c899
 
71c899
     def _write_bio(self, data):
71c899
-        return m2.ssl_write(self.ssl, data)
71c899
+        return m2.ssl_write(self.ssl, data, self._timeout)
71c899
 
71c899
     def _write_nbio(self, data):
71c899
         return m2.ssl_write_nbio(self.ssl, data)
71c899
@@ -204,7 +206,7 @@
71c899
     def _read_bio(self, size=1024):
71c899
         if size <= 0:
71c899
             raise ValueError, 'size <= 0'
71c899
-        return m2.ssl_read(self.ssl, size)
71c899
+        return m2.ssl_read(self.ssl, size, self._timeout)
71c899
 
71c899
     def _read_nbio(self, size=1024):
71c899
         if size <= 0:
71c899
@@ -212,13 +214,13 @@
71c899
         return m2.ssl_read_nbio(self.ssl, size)
71c899
 
71c899
     def write(self, data):
71c899
-        if self.blocking:
71c899
+        if self._timeout != 0.0:
71c899
             return self._write_bio(data)
71c899
         return self._write_nbio(data)
71c899
     sendall = send = write
71c899
     
71c899
     def read(self, size=1024):
71c899
-        if self.blocking:
71c899
+        if self._timeout != 0.0:
71c899
             return self._read_bio(size)
71c899
         return self._read_nbio(size)
71c899
     recv = read
71c899
@@ -226,7 +228,17 @@
71c899
     def setblocking(self, mode):
71c899
         """Set this connection's underlying socket to _mode_."""
71c899
         self.socket.setblocking(mode)
71c899
-        self.blocking = mode
71c899
+        if mode:
71c899
+            self._timeout = -1.0
71c899
+        else:
71c899
+            self._timeout = 0.0
71c899
+
71c899
+    def settimeout(self, timeout):
71c899
+        """Set this connection's underlying socket's timeout to _timeout_."""
71c899
+        self.socket.settimeout(timeout)
71c899
+        self._timeout = timeout
71c899
+        if self._timeout is None:
71c899
+            self._timeout = -1.0
71c899
 
71c899
     def fileno(self):
71c899
         return self.socket.fileno()
71c899
@@ -308,15 +320,8 @@
71c899
         """Set the cipher suites for this connection."""
71c899
         return m2.ssl_set_cipher_list(self.ssl, cipher_list)
71c899
 
71c899
-    def makefile(self, mode='rb', bufsize='ignored'):
71c899
-        r = 'r' in mode or '+' in mode
71c899
-        w = 'w' in mode or 'a' in mode or '+' in mode
71c899
-        b = 'b' in mode
71c899
-        m2mode = ['', 'r'][r] + ['', 'w'][w] + ['', 'b'][b]      
71c899
-        # XXX Need to dup().
71c899
-        bio = BIO.BIO(self.sslbio, _close_cb=self.close)
71c899
-        m2.bio_do_handshake(bio._ptr())
71c899
-        return BIO.IOBuffer(bio, m2mode, _pyfree=0)
71c899
+    def makefile(self, mode='rb', bufsize=-1):
71c899
+        return socket._fileobject(self, mode, bufsize)
71c899
 
71c899
     def getsockname(self):
71c899
         return self.socket.getsockname()
71c899
diff -urN M2Crypto/M2Crypto/SSL/__init__.py M2Crypto-0.21.1/M2Crypto/SSL/__init__.py
71c899
--- M2Crypto/M2Crypto/SSL/__init__.py	2013-11-26 20:01:02.590964971 +0100
71c899
+++ M2Crypto-0.21.1/M2Crypto/SSL/__init__.py	2013-11-26 20:01:19.204950349 +0100
71c899
@@ -2,11 +2,14 @@
71c899
 
71c899
 Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved."""
71c899
 
71c899
+import socket
71c899
+
71c899
 # M2Crypto
71c899
 from M2Crypto import m2
71c899
 
71c899
 class SSLError(Exception): pass
71c899
-m2.ssl_init(SSLError)
71c899
+class SSLTimeoutError(SSLError, socket.timeout): pass
71c899
+m2.ssl_init(SSLError, SSLTimeoutError)
71c899
 
71c899
 # M2Crypto.SSL
71c899
 from Cipher import Cipher, Cipher_Stack
71c899
diff -urN M2Crypto/SWIG/_ssl.i M2Crypto-0.21.1/SWIG/_ssl.i
71c899
--- M2Crypto/SWIG/_ssl.i	2013-11-26 20:01:02.612964952 +0100
71c899
+++ M2Crypto-0.21.1/SWIG/_ssl.i	2013-11-26 20:01:19.205950348 +0100
71c899
@@ -11,10 +11,13 @@
71c899
 
71c899
 %{
71c899
 #include <pythread.h>
71c899
+#include <limits.h>
71c899
 #include <openssl/bio.h>
71c899
 #include <openssl/dh.h>
71c899
 #include <openssl/ssl.h>
71c899
 #include <openssl/x509.h>
71c899
+#include <poll.h>
71c899
+#include <sys/time.h>
71c899
 %}
71c899
 
71c899
 %apply Pointer NONNULL { SSL_CTX * };
71c899
@@ -155,6 +158,11 @@
71c899
 %rename(ssl_session_get_timeout) SSL_SESSION_get_timeout;
71c899
 extern long SSL_SESSION_get_timeout(CONST SSL_SESSION *);
71c899
 
71c899
+extern PyObject *ssl_accept(SSL *ssl, double timeout = -1);
71c899
+extern PyObject *ssl_connect(SSL *ssl, double timeout = -1);
71c899
+extern PyObject *ssl_read(SSL *ssl, int num, double timeout = -1);
71c899
+extern int ssl_write(SSL *ssl, PyObject *blob, double timeout = -1);
71c899
+
71c899
 %constant int ssl_error_none              = SSL_ERROR_NONE;
71c899
 %constant int ssl_error_ssl               = SSL_ERROR_SSL;
71c899
 %constant int ssl_error_want_read         = SSL_ERROR_WANT_READ;
71c899
@@ -210,14 +218,19 @@
71c899
 %constant int SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER = SSL_MODE_ENABLE_PARTIAL_WRITE;
71c899
 %constant int SSL_MODE_AUTO_RETRY           = SSL_MODE_AUTO_RETRY;
71c899
 
71c899
+%ignore ssl_handle_error;
71c899
+%ignore ssl_sleep_with_timeout;
71c899
 %inline %{
71c899
 static PyObject *_ssl_err;
71c899
+static PyObject *_ssl_timeout_err;
71c899
 
71c899
-void ssl_init(PyObject *ssl_err) {
71c899
+void ssl_init(PyObject *ssl_err, PyObject *ssl_timeout_err) {
71c899
     SSL_library_init();
71c899
     SSL_load_error_strings();
71c899
     Py_INCREF(ssl_err);
71c899
+    Py_INCREF(ssl_timeout_err);
71c899
     _ssl_err = ssl_err;
71c899
+    _ssl_timeout_err = ssl_timeout_err;
71c899
 }
71c899
 
71c899
 void ssl_ctx_passphrase_callback(SSL_CTX *ctx, PyObject *pyfunc) {
71c899
@@ -403,36 +416,130 @@
71c899
     return ret;
71c899
 }
71c899
 
71c899
-PyObject *ssl_accept(SSL *ssl) {
71c899
+static void ssl_handle_error(int ssl_err, int ret) {
71c899
+    int err;
71c899
+
71c899
+    switch (ssl_err) {
71c899
+        case SSL_ERROR_SSL:
71c899
+            PyErr_SetString(_ssl_err,
71c899
+                            ERR_reason_error_string(ERR_get_error()));
71c899
+            break;
71c899
+        case SSL_ERROR_SYSCALL:
71c899
+            err = ERR_get_error();
71c899
+            if (err)
71c899
+                PyErr_SetString(_ssl_err, ERR_reason_error_string(err));
71c899
+            else if (ret == 0)
71c899
+                PyErr_SetString(_ssl_err, "unexpected eof");
71c899
+            else if (ret == -1)
71c899
+                PyErr_SetFromErrno(_ssl_err);
71c899
+            else
71c899
+                assert(0);
71c899
+            break;
71c899
+		default:
71c899
+            PyErr_SetString(_ssl_err, "unexpected SSL error");
71c899
+     }
71c899
+}
71c899
+
71c899
+static int ssl_sleep_with_timeout(SSL *ssl, const struct timeval *start,
71c899
+                                  double timeout, int ssl_err) {
71c899
+    struct pollfd fd;
71c899
+    struct timeval tv;
71c899
+    int ms, tmp;
71c899
+
71c899
+    assert(timeout > 0);
71c899
+ again:
71c899
+    gettimeofday(&tv, NULL);
71c899
+    /* tv >= start */
71c899
+    if ((timeout + start->tv_sec - tv.tv_sec) > INT_MAX / 1000)
71c899
+        ms = -1;
71c899
+    else {
71c899
+        int fract;
71c899
+
71c899
+        ms = ((start->tv_sec + (int)timeout) - tv.tv_sec) * 1000;
71c899
+        fract = (start->tv_usec + (timeout - (int)timeout) * 1000000
71c899
+                 - tv.tv_usec + 999) / 1000;
71c899
+        if (ms > 0 && fract > INT_MAX - ms)
71c899
+            ms = -1;
71c899
+        else {
71c899
+            ms += fract;
71c899
+            if (ms <= 0)
71c899
+                goto timeout;
71c899
+        }
71c899
+    }
71c899
+    switch (ssl_err) {
71c899
+	    case SSL_ERROR_WANT_READ:
71c899
+            fd.fd = SSL_get_rfd(ssl);
71c899
+            fd.events = POLLIN;
71c899
+            break;
71c899
+
71c899
+	    case SSL_ERROR_WANT_WRITE:
71c899
+            fd.fd = SSL_get_wfd(ssl);
71c899
+            fd.events = POLLOUT;
71c899
+            break;
71c899
+
71c899
+	    case SSL_ERROR_WANT_X509_LOOKUP:
71c899
+            return 0; /* FIXME: is this correct? */
71c899
+
71c899
+	    default:
71c899
+            assert(0);
71c899
+    }
71c899
+    if (fd.fd == -1) {
71c899
+        PyErr_SetString(_ssl_err, "timeout on a non-FD SSL");
71c899
+        return -1;
71c899
+    }
71c899
+    Py_BEGIN_ALLOW_THREADS
71c899
+    tmp = poll(&fd, 1, ms);
71c899
+    Py_END_ALLOW_THREADS
71c899
+    switch (tmp) {
71c899
+    	case 1:
71c899
+            return 0;
71c899
+    	case 0:
71c899
+            goto timeout;
71c899
+    	case -1:
71c899
+            if (errno == EINTR)
71c899
+                goto again;
71c899
+            PyErr_SetFromErrno(_ssl_err);
71c899
+            return -1;
71c899
+    }
71c899
+    return 0;
71c899
+
71c899
+ timeout:
71c899
+    PyErr_SetString(_ssl_timeout_err, "timed out");
71c899
+    return -1;
71c899
+}
71c899
+
71c899
+PyObject *ssl_accept(SSL *ssl, double timeout) {
71c899
     PyObject *obj = NULL;
71c899
-    int r, err;
71c899
+    int r, ssl_err;
71c899
+    struct timeval tv;
71c899
 
71c899
+    if (timeout > 0)
71c899
+        gettimeofday(&tv, NULL);
71c899
+ again:
71c899
     Py_BEGIN_ALLOW_THREADS
71c899
     r = SSL_accept(ssl);
71c899
+    ssl_err = SSL_get_error(ssl, r);
71c899
     Py_END_ALLOW_THREADS
71c899
 
71c899
 
71c899
-    switch (SSL_get_error(ssl, r)) {
71c899
+    switch (ssl_err) {
71c899
         case SSL_ERROR_NONE:
71c899
         case SSL_ERROR_ZERO_RETURN:
71c899
             obj = PyInt_FromLong((long)1);
71c899
             break;
71c899
         case SSL_ERROR_WANT_WRITE:
71c899
         case SSL_ERROR_WANT_READ:
71c899
-            obj = PyInt_FromLong((long)0);
71c899
-            break;
71c899
-        case SSL_ERROR_SSL:
71c899
-            PyErr_SetString(_ssl_err, ERR_reason_error_string(ERR_get_error()));
71c899
+            if (timeout <= 0) {
71c899
+                obj = PyInt_FromLong((long)0);
71c899
+                break;
71c899
+            }
71c899
+            if (ssl_sleep_with_timeout(ssl, &tv, timeout, ssl_err) == 0)
71c899
+                goto again;
71c899
             obj = NULL;
71c899
             break;
71c899
+        case SSL_ERROR_SSL:
71c899
         case SSL_ERROR_SYSCALL:
71c899
-            err = ERR_get_error();
71c899
-            if (err)
71c899
-                PyErr_SetString(_ssl_err, ERR_reason_error_string(err));
71c899
-            else if (r == 0)
71c899
-                PyErr_SetString(_ssl_err, "unexpected eof");
71c899
-            else if (r == -1)
71c899
-                PyErr_SetFromErrno(_ssl_err);
71c899
+            ssl_handle_error(ssl_err, r);
71c899
             obj = NULL;
71c899
             break;
71c899
     }
71c899
@@ -441,36 +548,38 @@
71c899
     return obj;
71c899
 }
71c899
 
71c899
-PyObject *ssl_connect(SSL *ssl) {
71c899
+PyObject *ssl_connect(SSL *ssl, double timeout) {
71c899
     PyObject *obj = NULL;
71c899
-    int r, err;
71c899
+    int r, ssl_err;
71c899
+    struct timeval tv;
71c899
 
71c899
+    if (timeout > 0)
71c899
+        gettimeofday(&tv, NULL);
71c899
+ again:
71c899
     Py_BEGIN_ALLOW_THREADS
71c899
     r = SSL_connect(ssl);
71c899
+    ssl_err = SSL_get_error(ssl, r);
71c899
     Py_END_ALLOW_THREADS
71c899
 
71c899
     
71c899
-    switch (SSL_get_error(ssl, r)) {
71c899
+    switch (ssl_err) {
71c899
         case SSL_ERROR_NONE:
71c899
         case SSL_ERROR_ZERO_RETURN:
71c899
             obj = PyInt_FromLong((long)1);
71c899
             break;
71c899
         case SSL_ERROR_WANT_WRITE:
71c899
         case SSL_ERROR_WANT_READ:
71c899
-            obj = PyInt_FromLong((long)0);
71c899
-            break;
71c899
-        case SSL_ERROR_SSL:
71c899
-            PyErr_SetString(_ssl_err, ERR_reason_error_string(ERR_get_error()));
71c899
+            if (timeout <= 0) {
71c899
+                obj = PyInt_FromLong((long)0);
71c899
+                break;
71c899
+            }
71c899
+            if (ssl_sleep_with_timeout(ssl, &tv, timeout, ssl_err) == 0)
71c899
+                goto again;
71c899
             obj = NULL;
71c899
             break;
71c899
+        case SSL_ERROR_SSL:
71c899
         case SSL_ERROR_SYSCALL:
71c899
-            err = ERR_get_error();
71c899
-            if (err)
71c899
-                PyErr_SetString(_ssl_err, ERR_reason_error_string(err));
71c899
-            else if (r == 0)
71c899
-                PyErr_SetString(_ssl_err, "unexpected eof");
71c899
-            else if (r == -1)
71c899
-                PyErr_SetFromErrno(_ssl_err);
71c899
+            ssl_handle_error(ssl_err, r);
71c899
             obj = NULL;
71c899
             break;
71c899
     }
71c899
@@ -483,10 +592,11 @@
71c899
     SSL_set_shutdown(ssl, mode);
71c899
 }
71c899
 
71c899
-PyObject *ssl_read(SSL *ssl, int num) {
71c899
+PyObject *ssl_read(SSL *ssl, int num, double timeout) {
71c899
     PyObject *obj = NULL;
71c899
     void *buf;
71c899
-    int r, err;
71c899
+    int r;
71c899
+    struct timeval tv;
71c899
 
71c899
     if (!(buf = PyMem_Malloc(num))) {
71c899
         PyErr_SetString(PyExc_MemoryError, "ssl_read");
71c899
@@ -494,37 +604,44 @@
71c899
     }
71c899
 
71c899
 
71c899
+    if (timeout > 0)
71c899
+        gettimeofday(&tv, NULL);
71c899
+ again:
71c899
     Py_BEGIN_ALLOW_THREADS
71c899
     r = SSL_read(ssl, buf, num);
71c899
     Py_END_ALLOW_THREADS
71c899
 
71c899
 
71c899
-    switch (SSL_get_error(ssl, r)) {
71c899
-        case SSL_ERROR_NONE:
71c899
-        case SSL_ERROR_ZERO_RETURN:
71c899
-            buf = PyMem_Realloc(buf, r);
71c899
-            obj = PyString_FromStringAndSize(buf, r);
71c899
-            break;
71c899
-        case SSL_ERROR_WANT_WRITE:
71c899
-        case SSL_ERROR_WANT_READ:
71c899
-        case SSL_ERROR_WANT_X509_LOOKUP:
71c899
-            Py_INCREF(Py_None);
71c899
-            obj = Py_None;
71c899
-            break;
71c899
-        case SSL_ERROR_SSL:
71c899
-            PyErr_SetString(_ssl_err, ERR_reason_error_string(ERR_get_error()));
71c899
-            obj = NULL;
71c899
-            break;
71c899
-        case SSL_ERROR_SYSCALL:
71c899
-            err = ERR_get_error();
71c899
-            if (err)
71c899
-                PyErr_SetString(_ssl_err, ERR_reason_error_string(err));
71c899
-            else if (r == 0)
71c899
-                PyErr_SetString(_ssl_err, "unexpected eof");
71c899
-            else if (r == -1)
71c899
-                PyErr_SetFromErrno(_ssl_err);
71c899
-            obj = NULL;
71c899
-            break;
71c899
+    if (r >= 0) {
71c899
+        buf = PyMem_Realloc(buf, r);
71c899
+        obj = PyString_FromStringAndSize(buf, r);
71c899
+    } else {
71c899
+        int ssl_err;
71c899
+
71c899
+        ssl_err = SSL_get_error(ssl, r);
71c899
+        switch (ssl_err) {
71c899
+            case SSL_ERROR_NONE:
71c899
+            case SSL_ERROR_ZERO_RETURN:
71c899
+                assert(0);
71c899
+
71c899
+            case SSL_ERROR_WANT_WRITE:
71c899
+            case SSL_ERROR_WANT_READ:
71c899
+            case SSL_ERROR_WANT_X509_LOOKUP:
71c899
+                if (timeout <= 0) {
71c899
+                    Py_INCREF(Py_None);
71c899
+                    obj = Py_None;
71c899
+                    break;
71c899
+                }
71c899
+                if (ssl_sleep_with_timeout(ssl, &tv, timeout, ssl_err) == 0)
71c899
+                    goto again;
71c899
+                obj = NULL;
71c899
+                break;
71c899
+            case SSL_ERROR_SSL:
71c899
+            case SSL_ERROR_SYSCALL:
71c899
+                ssl_handle_error(ssl_err, r);
71c899
+                obj = NULL;
71c899
+                break;
71c899
+        }
71c899
     }
71c899
     PyMem_Free(buf);
71c899
 
71c899
@@ -582,22 +699,26 @@
71c899
     return obj;
71c899
 }
71c899
 
71c899
-int ssl_write(SSL *ssl, PyObject *blob) {
71c899
+int ssl_write(SSL *ssl, PyObject *blob, double timeout) {
71c899
     const void *buf;
71c899
-    int len, r, err, ret;
71c899
+    int len, r, ssl_err, ret;
71c899
+    struct timeval tv;
71c899
 
71c899
 
71c899
     if (m2_PyObject_AsReadBufferInt(blob, &buf, &len) == -1) {
71c899
         return -1;
71c899
     }
71c899
 
71c899
-    
71c899
+    if (timeout > 0)
71c899
+        gettimeofday(&tv, NULL);
71c899
+ again:
71c899
     Py_BEGIN_ALLOW_THREADS
71c899
     r = SSL_write(ssl, buf, len);
71c899
+    ssl_err = SSL_get_error(ssl, r);
71c899
     Py_END_ALLOW_THREADS
71c899
 
71c899
 
71c899
-    switch (SSL_get_error(ssl, r)) {
71c899
+    switch (ssl_err) {
71c899
         case SSL_ERROR_NONE:
71c899
         case SSL_ERROR_ZERO_RETURN:
71c899
             ret = r;
71c899
@@ -605,20 +726,17 @@
71c899
         case SSL_ERROR_WANT_WRITE:
71c899
         case SSL_ERROR_WANT_READ:
71c899
         case SSL_ERROR_WANT_X509_LOOKUP:
71c899
+            if (timeout <= 0) {
71c899
+                ret = -1;
71c899
+                break;
71c899
+            }
71c899
+            if (ssl_sleep_with_timeout(ssl, &tv, timeout, ssl_err) == 0)
71c899
+                goto again;
71c899
             ret = -1;
71c899
             break;
71c899
         case SSL_ERROR_SSL:
71c899
-            PyErr_SetString(_ssl_err, ERR_reason_error_string(ERR_get_error()));
71c899
-            ret = -1;
71c899
-            break;
71c899
         case SSL_ERROR_SYSCALL:
71c899
-            err = ERR_get_error();
71c899
-            if (err)
71c899
-                PyErr_SetString(_ssl_err, ERR_reason_error_string(ERR_get_error()));
71c899
-            else if (r == 0)
71c899
-                PyErr_SetString(_ssl_err, "unexpected eof");
71c899
-            else if (r == -1)
71c899
-                PyErr_SetFromErrno(_ssl_err);
71c899
+            ssl_handle_error(ssl_err, r);
71c899
         default:
71c899
             ret = -1;
71c899
     }
71c899
diff -urN M2Crypto/tests/test_ssl.py M2Crypto-0.21.1/tests/test_ssl.py
71c899
--- M2Crypto/tests/test_ssl.py	2013-11-26 20:01:02.582964980 +0100
71c899
+++ M2Crypto-0.21.1/tests/test_ssl.py	2013-11-26 20:01:33.268937969 +0100
71c899
@@ -972,6 +972,77 @@
71c899
 
71c899
 class TwistedSSLClientTestCase(BaseSSLClientTestCase):
71c899
 
71c899
+    def test_timeout(self):
71c899
+        pid = self.start_server(self.args)
71c899
+        try:
71c899
+            ctx = SSL.Context()
71c899
+            s = SSL.Connection(ctx)
71c899
+            # Just a really small number so we can timeout
71c899
+            s.settimeout(0.000000000000000000000000000001)
71c899
+            self.assertRaises(SSL.SSLTimeoutError, s.connect, self.srv_addr)
71c899
+            s.close()
71c899
+        finally:
71c899
+            self.stop_server(pid)
71c899
+
71c899
+    def test_makefile_timeout(self):
71c899
+        # httpslib uses makefile to read the response
71c899
+        pid = self.start_server(self.args)
71c899
+        try:
71c899
+            from M2Crypto import httpslib
71c899
+            c = httpslib.HTTPS(srv_host, srv_port)
71c899
+            c.putrequest('GET', '/')
71c899
+            c.putheader('Accept', 'text/html')
71c899
+            c.putheader('Accept', 'text/plain')
71c899
+            c.endheaders()
71c899
+            c._conn.sock.settimeout(100)
71c899
+            err, msg, headers = c.getreply()
71c899
+            assert err == 200, err
71c899
+            f = c.getfile()
71c899
+            data = f.read()
71c899
+            c.close()
71c899
+        finally:
71c899
+            self.stop_server(pid)
71c899
+        self.failIf(string.find(data, 's_server -quiet -www') == -1)
71c899
+
71c899
+    def test_makefile_timeout_fires(self):
71c899
+        # This is convoluted because (openssl s_server -www) starts writing the
71c899
+        # response as soon as it receives the first line of the request, so it's
71c899
+        # possible for it to send the response before the request is sent and
71c899
+        # there would be no timeout.  So, let the server spend time reading from
71c899
+        # an empty pipe
71c899
+        FIFO_NAME = 'test_makefile_timeout_fires_fifo'
71c899
+        os.mkfifo('tests/' + FIFO_NAME)
71c899
+        pipe_pid = os.fork()
71c899
+        try:
71c899
+            if pipe_pid == 0:
71c899
+                try:
71c899
+                    f = open('tests/' + FIFO_NAME, 'w')
71c899
+                    try:
71c899
+                        time.sleep(sleepTime + 1)
71c899
+                        f.write('Content\n')
71c899
+                    finally:
71c899
+                        f.close()
71c899
+                finally:
71c899
+                    os._exit(0)
71c899
+            self.args[self.args.index('-www')] = '-WWW'
71c899
+            pid = self.start_server(self.args)
71c899
+            try:
71c899
+                from M2Crypto import httpslib
71c899
+                c = httpslib.HTTPS(srv_host, srv_port)
71c899
+                c.putrequest('GET', '/' + FIFO_NAME)
71c899
+                c.putheader('Accept', 'text/html')
71c899
+                c.putheader('Accept', 'text/plain')
71c899
+                c.endheaders()
71c899
+                c._conn.sock.settimeout(0.0000000001)
71c899
+                self.assertRaises(socket.timeout, c.getreply)
71c899
+                c.close()
71c899
+            finally:
71c899
+                self.stop_server(pid)
71c899
+        finally:
71c899
+            os.kill(pipe_pid, 1)
71c899
+            os.waitpid(pipe_pid, 0)
71c899
+            os.unlink('tests/' + FIFO_NAME)
71c899
+
71c899
     def test_twisted_wrapper(self):
71c899
         # Test only when twisted and ZopeInterfaces are present
71c899
         try: