From 92b3dd6f2103518cef834e133ab7a4883d41cf54 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 21 Jul 2015 14:12:01 +1200 Subject: [PATCH] Implement more in IMAP4_TLS to work around backports.ssl limitations --- imapclient/tls.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/imapclient/tls.py b/imapclient/tls.py index 42d8d79..344b489 100644 --- a/imapclient/tls.py +++ b/imapclient/tls.py @@ -107,14 +107,12 @@ def wrap_socket(sock, ssl_context, hostname): ssl_context = create_default_context() try: - newsock = ssl_context.wrap_socket(sock, server_hostname=hostname) + return ssl_context.wrap_socket(sock, server_hostname=hostname) except ssl.CertificateError: sock.shutdown() sock.close() raise imaplib.Error("server certificate not valid for %s" % hostname) - return _SSLSocketWithShutdown(newsock) - class IMAP4_TLS(imaplib.IMAP4): """IMAP4 client class for TLS/SSL connections. @@ -133,15 +131,21 @@ class IMAP4_TLS(imaplib.IMAP4): self.sock = wrap_socket(sock, self.ssl_context, host) self.file = self.sock.makefile('rb') + def read(self, size): + return self.file.read(size) -# TODO: get shutdown added in backports.ssl.SSLSocket -class _SSLSocketWithShutdown(object): - - def __init__(self, sslsock): - self.sslsock = sslsock + def readline(self): + return self.file.readline() - def shutdown(self, _): - return self.sslsock._conn.shutdown() + def send(self, data): + remaining = len(data) + while remaining > 0: + sent = self.sock.send(data) + if sent == remaining: + break + data = data[sent:] + remaining -= sent - def __getattr__(self, name): - return getattr(self.sslsock, name) + def shutdown(self): + self.file.close() + self.sock.close() -- 2.43.0