diff options
author | Christophe Haen <christophe.haen@cern.ch> | 2020-06-17 12:22:59 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2020-07-13 22:40:49 +0200 |
commit | d070a8a672ec4c2e326ecf83cc3b3df71dc01033 (patch) | |
tree | fe1bf4bd1de3116df3eee3963e08e499748f9831 | |
parent | 35d463d8eee7f9ab5d2b431caad8f4cd4dd0f946 (diff) | |
download | m2crypto-d070a8a672ec4c2e326ecf83cc3b3df71dc01033.tar.gz |
SSL.Connection.close accepts an argument to force the socket closing
-rw-r--r-- | M2Crypto/SSL/Connection.py | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/M2Crypto/SSL/Connection.py b/M2Crypto/SSL/Connection.py index a5863ff..c839b30 100644 --- a/M2Crypto/SSL/Connection.py +++ b/M2Crypto/SSL/Connection.py @@ -51,10 +51,11 @@ class Connection(object): :param sock: socket to be used :param family: socket family """ - # The Checker needs to be an instance attribute + # The Checker needs to be an instance attribute # and not a class attribute for thread safety reason self.clientPostConnectionCheck = Checker.Checker() + self._bio_freed = False self.ctx = ctx self.ssl = m2.ssl_new(self.ctx.ctx) # type: bytes if sock is not None: @@ -76,23 +77,38 @@ class Connection(object): self.host = None + def _free_bio(self): + """ + Free the sslbio and sockbio, and close the socket. + """ + # Do not do it twice + if not self._bio_freed: + if getattr(self, 'sslbio', None): + self.m2_bio_free(self.sslbio) + if getattr(self, 'sockbio', None): + self.m2_bio_free(self.sockbio) + if self.ssl_close_flag == self.m2_bio_noclose and \ + getattr(self, 'ssl', None): + self.m2_ssl_free(self.ssl) + self.socket.close() + self._bio_freed = True + + def __del__(self): # type: () -> None # Notice that M2Crypto doesn't automatically shuts down the # connection here. You have to call self.close() in your # program, M2Crypto won't do it automatically for you. - if getattr(self, 'sslbio', None): - self.m2_bio_free(self.sslbio) - if getattr(self, 'sockbio', None): - self.m2_bio_free(self.sockbio) - if self.ssl_close_flag == self.m2_bio_noclose and \ - getattr(self, 'ssl', None): - self.m2_ssl_free(self.ssl) - self.socket.close() - - def close(self): + self._free_bio() + + def close(self, freeBio=False): + """ + if freeBio is true, call _free_bio + """ # type: () -> None m2.ssl_shutdown(self.ssl) + if freeBio: + self._free_bio() def clear(self): # type: () -> int |