Blame SOURCES/m2crypto-0.21.1-timeouts.patch

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