Ticket #6768: proxy_cr_fixes_20130304.patch

File proxy_cr_fixes_20130304.patch, 4.0 KB (added by Andy Lutomirski, 6 years ago)

More proxy fixes

  • twisted/topfiles/6768.feature

     
    1 twisted.web.proxy.ProxyClient (and all of its users in twisted.web.proxy) will now close HTTP connections that they initiate if the incoming connection to the proxy dies before receiving a response.
     1twisted.web.proxy.ProxyClient (and all of its users in twisted.web.proxy) now closes HTTP connections that it initiates if the incoming connection to the proxy dies before receiving a response.
  • twisted/web/proxy.py

     
    9595        This is called when the proxy's client disconnects or otherwise aborts.
    9696        We want to abort, too, so that we don't tie up resources on the
    9797        server we're connecting to.
     98
     99        @param failure: the failure object
     100        @type failure: L{twisted.python.failure.Failure}
    98101        """
    99102        self._finished = True
    100         self.transport.loseConnection()
    101103
     104        abort = getattr(self.transport, 'abortConnection', None)
     105        if abort is not None:
     106            abort(self.transport)
     107        else:
     108            self.transport.loseConnection()
    102109
    103110
    104111class ProxyClientFactory(ClientFactory):
  • twisted/web/test/test_proxy.py

     
    55Test for L{twisted.web.proxy}.
    66"""
    77
     8import types
     9
    810from twisted.trial.unittest import TestCase
    911from twisted.test.proto_helpers import StringTransportWithDisconnection
    1012from twisted.test.proto_helpers import MemoryReactor
     
    378380
    379381    def test_parentDisconnected(self):
    380382        """
    381         The client should disconnect if the parent request is dropped.
     383        The client disconnects if the parent request is dropped.
    382384        """
    383385        request = self.makeRequest('foo')
    384386        client = self.makeProxyClient(request, headers={"accept": "text/html"})
     
    392394        self.assertFalse(client.transport.connected)
    393395
    394396
    395     def test_parentDisconnectedEarly(self):
     397    def test_parentDisconnectedAbort(self):
    396398        """
    397         The proxy should survive a parent disconnection before the client
    398         connects.
     399        The client aborts if the parent request is dropped and the client's
     400        transport supports abortConnection.
    399401        """
    400402        request = self.makeRequest('foo')
    401403        client = self.makeProxyClient(request, headers={"accept": "text/html"})
     404
     405        self.assertForwardsHeaders(
     406            client, 'GET /foo HTTP/1.0',
     407            {'connection': 'close', 'accept': 'text/html'})
     408
     409        # Monkey patch the transport object to add an abortConnection
     410        # function.  (This is too specialized to be worth adding to the
     411        # core testing code.)
     412        self.assertFalse(hasattr(client.transport, 'abortConnection'))
     413        aborted = [False]
     414        def abortConnection(self):
     415            self.loseConnection()
     416            aborted[0] = True
     417        client.transport.abortConnection = (
     418            types.MethodType(abortConnection, None, type(client.transport)))
     419
     420        self.assertTrue(client.transport.connected)
    402421        request.processingFailed(main.CONNECTION_LOST)
     422        self.assertFalse(client.transport.connected)
     423        self.assertTrue(aborted[0])
    403424
     425
     426    def test_parentDisconnectedEarly(self):
     427        """
     428        The proxy survives a parent disconnection before the client connects.
     429        """
     430        request = self.makeRequest('foo')
     431        self.makeProxyClient(request, headers={"accept": "text/html"})
     432        request.processingFailed(main.CONNECTION_LOST)
     433
    404434        # Note: this test is a little bit odd.  We're testing that the
    405435        # processingFailed call doesn't raise an exception.
    406436