-
Notifications
You must be signed in to change notification settings - Fork 1
/
slowreq.py
56 lines (49 loc) · 1.83 KB
/
slowreq.py
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
# Copyright ClusterHQ Limited. See LICENSE file for details.
from twisted.internet import reactor
from twisted.internet.task import deferLater
from twisted.web import server, resource
import json
class AdapterResource(resource.Resource):
isLeaf = True
def render_POST(self, request):
"""
Handle a pre-hook.
"""
requestJson = json.loads(request.content.read())
if requestJson["Type"] == "pre-hook":
return self._handlePreHook(request, requestJson)
elif requestJson["Type"] == "post-hook":
return self._handlePostHook(request, requestJson)
else:
raise Exception("unsupported hook type %s" %
(requestJson["Type"],))
def _handlePreHook(self, request, requestJson):
# The desired response is the entire client request
# payload, unmodified.
def waited():
request.write(json.dumps({
"PowerstripProtocolVersion": 1,
"ModifiedClientRequest":
requestJson["ClientRequest"]}))
request.finish()
deferLater(reactor, 1, waited)
return server.NOT_DONE_YET
def _handlePostHook(self, request, requestJson):
# The desired response is the entire client request
# payload, unmodified.
def waited():
request.write(json.dumps({
"PowerstripProtocolVersion": 1,
"ModifiedServerResponse":
requestJson["ServerResponse"]}))
request.finish()
deferLater(reactor, 1, waited)
return server.NOT_DONE_YET
def getAdapter():
root = resource.Resource()
root.putChild("slowreq-adapter", AdapterResource())
site = server.Site(root)
return site
if __name__ == "__main__":
reactor.listenTCP(80, getAdapter())
reactor.run()