forked from andrewf/pcap2har
-
Notifications
You must be signed in to change notification settings - Fork 0
/
har.py
76 lines (70 loc) · 2.13 KB
/
har.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
69
70
71
72
73
74
75
76
import http
import json
'''
functions and classes for generating HAR data from parsed http data
'''
# json_repr for HTTP header dicts
def header_json_repr(d):
return [
{
'name': k,
'value': v
} for k, v in sorted(d.items(), key=lambda x: (-1*x[1], x[0]))
]
def query_json_repr(d):
# d = {string: [string]}
# we need to print all values of the list
output = []
for k, l in sorted(d.items(), key=lambda x: (-1*x[1], x[0])):
for v in l:
output.append({
'name': k,
'value': v
})
return output
# add json_repr methods to http classes
def HTTPRequestJsonRepr(self):
'''
self = http.Request
'''
return {
'method': self.msg.method,
'url': self.url,
'httpVersion': self.msg.version,
'cookies': [],
'queryString': query_json_repr(self.query),
'headersSize': -1,
'headers': header_json_repr(self.msg.headers),
'bodySize': len(self.msg.body),
}
http.Request.json_repr = HTTPRequestJsonRepr
def HTTPResponseJsonRepr(self):
content = {
'size': len(self.body),
'compression': len(self.body) - len(self.raw_body),
'mimeType': self.mimeType
}
if self.text:
content['text'] = self.text.encode('utf8') # must transcode to utf8
return {
'status': int(self.msg.status),
'statusText': self.msg.reason,
'httpVersion': self.msg.version,
'cookies': [],
'headersSize': -1,
'bodySize': len(self.msg.body),
'redirectURL': self.msg.headers['location'] if 'location' in self.msg.headers else '',
'headers': header_json_repr(self.msg.headers),
'content': content,
}
http.Response.json_repr = HTTPResponseJsonRepr
# custom json encoder
class JsonReprEncoder(json.JSONEncoder):
'''
Custom Json Encoder that attempts to call json_repr on every object it
encounters.
'''
def default(self, obj):
if hasattr(obj, 'json_repr'):
return obj.json_repr()
return json.JSONEncoder.default(self, obj) # should call super instead?