/
message.py
49 lines (46 loc) · 2.04 KB
/
message.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
class Message(object):
'''
Contains a dpkt.http.Request/Response, as well as other data required to
build a HAR, including (mostly) start and end time.
* msg: underlying dpkt class
* data_consumed: how many bytes of input were consumed
* seq_start: first sequence number of the Message's data in the tcpdir
* seq_end: first sequence number past Message's data (slice-style indices)
* ts_start: when Message started arriving (dpkt timestamp)
* ts_end: when Message had fully arrived (dpkt timestamp)
* raw_body: body before compression is taken into account
* tcpdir: The tcp.Direction corresponding to the HTTP message
'''
def __init__(self, tcpdir, pointer, msgclass):
'''
Args:
tcpdir = tcp.Direction
pointer = position within tcpdir.data to start parsing from. byte index
msgclass = dpkt.http.Request/Response
'''
self.tcpdir = tcpdir
# attempt to parse as http. let exception fall out to caller
self.msg = msgclass(tcpdir.data[pointer:])
self.data_consumed = (len(tcpdir.data) - pointer) - len(self.msg.data)
# save memory by deleting data attribute; it's useless
self.msg.data = None
# calculate sequence numbers of data
self.seq_start = tcpdir.byte_to_seq(pointer)
self.seq_end = tcpdir.byte_to_seq(pointer + self.data_consumed) # past-the-end
# calculate arrival_times
self.ts_start = tcpdir.seq_final_arrival(self.seq_start)
self.ts_end = tcpdir.seq_final_arrival(self.seq_end - 1)
# get raw body
self.raw_body = self.msg.body
self.__pointer = pointer
# Access self.__raw_msg via raw_msg @property, which will set it if None
self.__raw_msg = None
@property
def raw_msg(self):
'''
Returns the message (including header) as a byte string.
'''
if not self.__raw_msg:
self.__raw_msg = self.tcpdir.data[
self.__pointer:(self.__pointer+self.data_consumed)]
return self.__raw_msg