Ticket #6024: tls-when-handshake-done-6204-2-reviewfixes.patch

File tls-when-handshake-done-6204-2-reviewfixes.patch, 7.2 KB (added by Andy Lutomirski, 7 years ago)

follow-up patch to address some review comments

  • twisted/internet/interfaces.py

    From ac8953b397664aef3efef3aedbe3c3cf60340c98 Mon Sep 17 00:00:00 2001
    Message-Id: <ac8953b397664aef3efef3aedbe3c3cf60340c98.1384213241.git.luto@amacapital.net>
    From: Andy Lutomirski <luto@amacapital.net>
    Date: Mon, 11 Nov 2013 15:21:02 -0800
    Subject: [PATCH 1/2] Fix comments from review.
    
    ---
     twisted/internet/interfaces.py     |  2 +-
     twisted/protocols/test/test_tls.py | 14 +++++++-------
     twisted/protocols/tls.py           | 20 +++++++++++---------
     twisted/web/topfiles/6204.feature  |  2 +-
     4 files changed, 20 insertions(+), 18 deletions(-)
    
    diff --git a/twisted/internet/interfaces.py b/twisted/internet/interfaces.py
    index 1c12c73..43762d7 100644
    a b class ISSLTransport(ITCPTransport): 
    21522152        Return an object with the peer's certificate info.
    21532153        """
    21542154
    2155     def notifyHandshakeDone():
     2155    def whenHandshakeDone():
    21562156        """
    21572157        Returns a Deferred that will complete when the initial handshake
    21582158        is done and will errback if the handshake fails.  (Connection
  • twisted/protocols/test/test_tls.py

    diff --git a/twisted/protocols/test/test_tls.py b/twisted/protocols/test/test_tls.py
    index 4545713..b170846 100644
    a b class TLSMemoryBIOTests(TestCase): 
    295295        wrapperFactory = TLSMemoryBIOFactory(
    296296            clientContextFactory, True, clientFactory)
    297297        sslClientProtocol = wrapperFactory.buildProtocol(None)
    298         handshakeDeferred = sslClientProtocol.notifyHandshakeDone()
     298        handshakeDeferred = sslClientProtocol.whenHandshakeDone()
    299299
    300300        serverFactory = ServerFactory()
    301301        serverFactory.protocol = Protocol
    class TLSMemoryBIOTests(TestCase): 
    374374
    375375    def test_notifyAfterSuccessfulHandshake(self):
    376376        """
    377         Calling L{TLSMemoryBIOProtocol.notifyHandshakeDone} after a
     377        Calling L{TLSMemoryBIOProtocol.whenHandshakeDone} after a
    378378        successful handshake should work.
    379379        """
    380380        tlsClient, tlsServer, handshakeDeferred, _ = self.handshakeProtocols()
    class TLSMemoryBIOTests(TestCase): 
    382382        result = Deferred()
    383383
    384384        def check(_):
    385             d = tlsClient.notifyHandshakeDone()
     385            d = tlsClient.whenHandshakeDone()
    386386            d.addCallback(result.callback)
    387387            d.addErrback(result.errback)
    388388
    class TLSMemoryBIOTests(TestCase): 
    392392
    393393    def test_notifyAfterFailedHandshake(self):
    394394        """
    395         Calling L{TLSMemoryBIOProtocol.notifyHandshakeDone} after a
     395        Calling L{TLSMemoryBIOProtocol.whenHandshakeDone} after a
    396396        failed handshake should work.
    397397        """
    398398        clientConnectionLost = Deferred()
    class TLSMemoryBIOTests(TestCase): 
    425425            result.errback(False)
    426426
    427427        def check(reason):
    428             d = sslClientProtocol.notifyHandshakeDone()
     428            d = sslClientProtocol.whenHandshakeDone()
    429429            if not d.called:
    430430                result.errback(Exception('notification should be called'))
    431431                return
    432432            d.addCallback(fail)
    433433            d.addErrback(lambda _: result.callback(None))
    434434
    435         sslClientProtocol.notifyHandshakeDone().addCallbacks(fail, check)
     435        sslClientProtocol.whenHandshakeDone().addCallbacks(fail, check)
    436436
    437437        return gatherResults([connectionDeferred, result])
    438438
    class TLSMemoryBIOTests(TestCase): 
    468468        result = Deferred()
    469469
    470470        def checkSide(side):
    471             return self.assertFailure(side.notifyHandshakeDone(), Error)
     471            return self.assertFailure(side.whenHandshakeDone(), Error)
    472472
    473473        return gatherResults([connectionDeferred, checkSide(sslClientProtocol),
    474474                              checkSide(sslServerProtocol)])
  • twisted/protocols/tls.py

    diff --git a/twisted/protocols/tls.py b/twisted/protocols/tls.py
    index dd17ab3..57a38b5 100644
    a b class TLSMemoryBIOProtocol(ProtocolWrapper): 
    248248        complete (C{True}) or not (C{False}).
    249249
    250250    @ivar _handshakeError: If the handshake failed, then this will store
    251         the reason.  Otherwise it will be C{None}.
     251        the reason (a L{twisted.python.failure.Failure} object).
     252        Otherwise it will be C{None}.
    252253
    253254    @ivar _handshakeDeferreds: If the handshake is not done, then this
    254255        is a list of L{twisted.internet.defer.Deferred} instances to
    255         be completed when the handshake finishes.
     256        be completed when the handshake finishes.  Once the handshake
     257        is done, this is C{None}.
    256258
    257259    @ivar _reason: If an unexpected L{OpenSSL.SSL.Error} occurs which causes
    258260        the connection to be lost, it is saved here.  If appropriate, this may
    class TLSMemoryBIOProtocol(ProtocolWrapper): 
    319321        # Now that we ourselves have a transport (initialized by the
    320322        # ProtocolWrapper.makeConnection call above), kick off the TLS
    321323        # handshake.
    322         self.__tryHandshake()
     324        self._tryHandshake()
    323325
    324326
    325     def notifyHandshakeDone(self):
     327    def whenHandshakeDone(self):
    326328        d = defer.Deferred()
    327329        if self._handshakeDone:
    328330            if self._handshakeError is None:
    class TLSMemoryBIOProtocol(ProtocolWrapper): 
    334336        return d
    335337
    336338
    337     def __tryHandshake(self):
     339    def _tryHandshake(self):
    338340        """
    339341        Attempts to handshake.  OpenSSL wants us to keep trying to
    340342        handshake until either it works or fails (as opposed to needing
    class TLSMemoryBIOProtocol(ProtocolWrapper): 
    353355                self._tlsShutdownFinished(Failure())
    354356                return
    355357            else:
    356                 self.__handshakeSucceeded()
     358                self._handshakeSucceeded()
    357359                return
    358360
    359361
    360     def __handshakeSucceeded(self):
     362    def _handshakeSucceeded(self):
    361363        """
    362364        Mark the handshake done and notify everyone.  It's okay to call
    363365        this more than once.
    class TLSMemoryBIOProtocol(ProtocolWrapper): 
    396398        # rely on it: if the handshake is done but there's no application
    397399        # data, then SSL_read won't tell us.
    398400        if not self._handshakeDone:
    399             self.__tryHandshake()
     401            self._tryHandshake()
    400402        if not self._handshakeDone:
    401403            return  # Save some effort: SSL_read can't possibly work
    402404
    class TLSMemoryBIOProtocol(ProtocolWrapper): 
    599601            else:
    600602                # SSL_write can transparently complete a handshake.  If we
    601603                # get here, then we're done handshaking.
    602                 self.__handshakeSucceeded()
     604                self._handshakeSucceeded()
    603605                self._flushSendBIO()
    604606                alreadySent += sent
    605607
  • twisted/web/topfiles/6204.feature

    diff --git a/twisted/web/topfiles/6204.feature b/twisted/web/topfiles/6204.feature
    index 1aed481..46cfabb 100644
    a b  
    1 twisted.internet.interfaces.ISSLTransport now has a notifyHandshakeDone method to request notification when the handshake succeeds or fails.
     1twisted.internet.interfaces.ISSLTransport now has a whenHandshakeDone method to request notification when the handshake succeeds or fails.