Ticket #6768: twisted_proxy_disconnect.2.patch

File twisted_proxy_disconnect.2.patch, 3.3 KB (added by Andy Lutomirski, 7 years ago)

v2, now with a test case

  • twisted/web/proxy.py

     
    4949        headers.pop('keep-alive', None)
    5050        self.headers = headers
    5151        self.data = data
     52        father.notifyFinish().addErrback(self.fatherFailed)
    5253
    5354
    5455    def connectionMade(self):
     
    8990            self.transport.loseConnection()
    9091
    9192
     93    def fatherFailed(self, failure):
     94        if not self._finished:
     95            self.transport.loseConnection()
     96            self._finished = True
    9297
     98
     99
    93100class ProxyClientFactory(ClientFactory):
    94101    """
    95102    Used by ProxyRequest to implement a simple web proxy.
  • twisted/web/test/test_proxy.py

     
    99from twisted.test.proto_helpers import StringTransportWithDisconnection
    1010from twisted.test.proto_helpers import MemoryReactor
    1111
     12from twisted.internet import main
    1213from twisted.web.resource import Resource
    1314from twisted.web.server import Site
    1415from twisted.web.proxy import ReverseProxyResource, ProxyClientFactory
     
    326327        should be added.
    327328        """
    328329        client = ProxyClient('GET', '/foo', 'HTTP/1.0',
    329                 {"accept": "text/html", "proxy-connection": "foo"}, '', None)
     330                {"accept": "text/html", "proxy-connection": "foo"}, '',
     331                self.makeRequest('foo'))
    330332        self.assertEqual(client.headers,
    331333                {"accept": "text/html", "connection": "close"})
    332334
     
    345347        expectedHeaders = headers.copy()
    346348        expectedHeaders['connection'] = 'close'
    347349        del expectedHeaders['keep-alive']
    348         client = ProxyClient('GET', '/foo', 'HTTP/1.0', headers, '', None)
     350        client = ProxyClient('GET', '/foo', 'HTTP/1.0', headers, '',
     351                             self.makeRequest('foo'))
    349352        self.assertForwardsHeaders(
    350353            client, 'GET /foo HTTP/1.0', expectedHeaders)
    351354
     
    372375        self.assertForwardsResponse(
    373376            request, 200, 'OK', headers.items(), '')
    374377
     378    def test_parentDisconnected(self):
     379        """
     380        Check that the client disconnects if the parent request is dropped.
     381        """
     382        request = self.makeRequest('foo')
     383        client = self.makeProxyClient(request, headers={"accept": "text/html"})
    375384
     385        self.assertForwardsHeaders(
     386            client, 'GET /foo HTTP/1.0',
     387            {'connection': 'close', 'accept': 'text/html'})
    376388
     389        self.assertTrue(client.transport.connected)
     390        request.processingFailed(main.CONNECTION_LOST)
     391        self.assertFalse(client.transport.connected)
     392
     393
     394
    377395class ProxyClientFactoryTestCase(TestCase):
    378396    """
    379397    Tests for L{ProxyClientFactory}.
     
    407425        """
    408426        factory = ProxyClientFactory('GET', '/foo', 'HTTP/1.0',
    409427                                     {"accept": "text/html"}, 'Some data',
    410                                      None)
     428                                     DummyRequest('foo'))
    411429        proto = factory.buildProtocol(None)
    412430        self.assertIsInstance(proto, ProxyClient)
    413431        self.assertEqual(proto.command, 'GET')