forked from artilleryio/recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_writer.py
68 lines (54 loc) · 1.57 KB
/
request_writer.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
57
58
59
60
61
62
63
64
65
66
67
68
import datetime
import os
import json
exclude_extensions = [ "jpg", "png", "gif", "txt", "css", "js", "html" ]
exclude_headers = [ "proxy-connection", "host", "user-agent", "content-length", "connection" ]
target = ""
if "TARGET" in os.environ:
target = os.environ["TARGET"]
def response(context, flow):
if not(target in flow.request.host):
print target
print flow.request.host
return
for ext in exclude_extensions:
if flow.request.path.endswith(ext):
return
with open("recording.json", "a") as f:
#
# headers
#
headers = []
for h in flow.request.headers:
if not(h[0].lower() in exclude_headers):
headers.append([h[0].lower(), h[1]])
headers_str = "{"
for h in headers:
headers_str += """ "%s" : "%s" """ % (h[0].lower(), h[1])
headers_str += "}"
#
# request body
#
is_json = False
for h in headers:
if (h[0] == "content-type") and ("json" in h[0]):
is_json = True
break
#
# write out JSON
#
opts = {}
opts["url"] = flow.request.path
if len(h) > 0:
opts["headers"] = {}
for h in headers:
opts["headers"][h[0]] = h[1]
if "content-type" in opts["headers"]:
if "json" in opts["headers"]["content-type"]:
if len(flow.request.content) > 0:
opts["json"] = json.loads(flow.request.content)
else:
opts["body"] = flow.request.content
request_spec = { flow.request.method.lower() : opts }
#f.write(json.dumps(request_spec, indent=2) + "\n")
f.write(json.dumps(request_spec) + "\n")