From bfb09b7715be5e44c65799e121d9920d056610e3 Mon Sep 17 00:00:00 2001 From: David Wolever Date: Sun, 6 Mar 2016 13:32:14 -0500 Subject: [PATCH 1/2] Fix broken Connection.sendall (#12) --- gevent_openssl/SSL.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gevent_openssl/SSL.py b/gevent_openssl/SSL.py index ce9f4e9..e8fe39f 100644 --- a/gevent_openssl/SSL.py +++ b/gevent_openssl/SSL.py @@ -46,7 +46,12 @@ def send(self, data, flags=0): return self.__send(self._connection.send, data, flags) def sendall(self, data, flags=0): - return self.__send(self._connection.sendall, data, flags) + # Note: all of the types supported by OpenSSL's Connection.sendall, + # basestring, memoryview, and buffer, support len(...) and slicing, + # so they are safe to use here. + while len(data) > 0: + res = self.send(data, flags) + data = data[res:] def __send(self, send_method, data, flags=0): try: From 1d020a394676841e063f9e6517d9f80b0a6f9c1f Mon Sep 17 00:00:00 2001 From: David Wolever Date: Thu, 1 Jun 2017 17:37:06 -0400 Subject: [PATCH 2/2] Catch EPIPE on connection shutdown PyOpenSSL will raise an EPIPE exception if the connection was already closed when shutdown() is called. Catch and ignore that exception. --- gevent_openssl/SSL.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gevent_openssl/SSL.py b/gevent_openssl/SSL.py index e8fe39f..7c7d1f4 100644 --- a/gevent_openssl/SSL.py +++ b/gevent_openssl/SSL.py @@ -79,4 +79,11 @@ def recv(self, bufsiz, flags=0): raise def shutdown(self): - return self.__iowait(self._connection.shutdown) + try: + return self.__iowait(self._connection.shutdown) + except OpenSSL.SSL.SysCallError as e: + # PyOpenSSL will raise an EPIPE if the connection was already + # closed, and that's safe to ignore here. + if e[1] == 'EPIPE': + return True + raise