summaryrefslogtreecommitdiff
path: root/test/proxy.py
blob: cdf62f3fb855f4723aa70c6f95e990b9370f72bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from twisted.internet import reactor
from twisted.web import http
from twisted.web.proxy import Proxy, ProxyRequest, ProxyClient, ProxyClientFactory
from twisted.python import log
import sys

class ContentWrapper:
    def __init__(self, content, parent):
        self.content = content
        self.parent = parent

    def seek(self, a, b):
        self.content.seek(a,b)

    def read(self):
        s = self.content.read()
        if True:
            log.msg("interrupt before send")
            # This exception will abort contacting the server and appear as
            # "Unhandled Error" in the proxy log. The proxy client will get
            # an empty reply.
            raise EOFError()
        return s

    def close(self):
        self.content.close()

class MyProxyClient(ProxyClient):
    def __init__(self, command, rest, version, headers, data, father):
        ProxyClient.__init__(self, command, rest, version, headers, data, father)

    def connectionMade(self):
        ProxyClient.connectionMade(self)
        log.msg("message sent")
        # interrupt now before server can reply?
        if self.father.mode == MyProxyRequest.INTERRUPT_AFTER_SEND:
            log.msg("interrupt after sending")
            # finish writing, but never read
            self.transport.loseConnection()
            # Be nice and report a real error back to the proxy client.
            self.father.setResponseCode(501, "Gateway error")
            self.father.responseHeaders.addRawHeader("Content-Type", "text/plain")
            self.father.write("connection intentionally interrupted after sending and before receiving")

class MyProxyClientFactory(ProxyClientFactory):
    protocol = MyProxyClient

    def __init__(self, command, rest, version, headers, data, father):
        ProxyClientFactory.__init__(self, command, rest, version, headers, data, father)

class MyProxyRequest(ProxyRequest):
    protocols = {"http": MyProxyClientFactory}
    INTERRUPT_BEFORE_SEND = 1
    INTERRUPT_AFTER_SEND = 2
    INTERRUPT_AFTER_RECEIVE = 3
    baseport = 10000

    def __init__(self, channel, queued, reactor=reactor):
        ProxyRequest.__init__(self, channel, queued, reactor)
        self.mode = channel.transport.server.port - self.baseport

    def process(self):
        log.msg("mode is", self.mode)

        # override read() method so that we can influence the original
        # process() without having to copy it; just replacing
        # the read method inside the existing content instance
        # would be easier, but turned out to be impossible (read-only
        # attribute)
        if self.mode == self.INTERRUPT_BEFORE_SEND:
            # ContentWrapper will raise exception instead of delivering data
            self.content = ContentWrapper(self.content, self)
        ProxyRequest.process(self)

    def write(self, content):
        log.msg("reply:", content)
        if self.mode == self.INTERRUPT_AFTER_RECEIVE:
            # TODO: suppress original headers
            # Original headers already sent to proxy client, but we
            # can still suppress the actual data and close the
            # connection to simulate a failure.
            log.msg("interrupt after receive")
            ProxyRequest.write(self, "")
            self.transport.loseConnection()
        else:
            ProxyRequest.write(self, content)

class MyProxy(Proxy):
    requestFactory = MyProxyRequest

if __name__ == '__main__':
    log.startLogging(sys.stdout)

    f = http.HTTPFactory()
    f.protocol = MyProxy
    reactor.listenTCP(MyProxyRequest.baseport + 0, f)
    reactor.listenTCP(MyProxyRequest.baseport + MyProxyRequest.INTERRUPT_BEFORE_SEND, f)
    reactor.listenTCP(MyProxyRequest.baseport + MyProxyRequest.INTERRUPT_AFTER_SEND, f)
    reactor.listenTCP(MyProxyRequest.baseport + MyProxyRequest.INTERRUPT_AFTER_RECEIVE, f)

    reactor.run()