MCPcopy Create free account
hub / github.com/mitmproxy/mitmproxy / StreamSaver

Class StreamSaver

examples/contrib/save_streamed_data.py:31–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29
30
31class StreamSaver:
32 TAG = "save_streamed_data: "
33
34 def __init__(self, flow, direction):
35 self.flow = flow
36 self.direction = direction
37 self.fh = None
38 self.path = None
39
40 def done(self):
41 if self.fh:
42 self.fh.close()
43 self.fh = None
44 # Make sure we have no circular references
45 self.flow = None
46
47 def __call__(self, data):
48 # End of stream?
49 if len(data) == 0:
50 self.done()
51 return data
52
53 # Just in case the option changes while a stream is in flight
54 if not ctx.options.save_streamed_data:
55 return data
56
57 # This is a safeguard but should not be needed
58 if not self.flow or not self.flow.request:
59 return data
60
61 if not self.fh:
62 self.path = datetime.fromtimestamp(
63 self.flow.request.timestamp_start
64 ).strftime(ctx.options.save_streamed_data)
65 self.path = self.path.replace("%+T", str(self.flow.request.timestamp_start))
66 self.path = self.path.replace("%+I", str(self.flow.client_conn.id))
67 self.path = self.path.replace("%+D", self.direction)
68 self.path = self.path.replace("%+C", self.flow.client_conn.address[0])
69 self.path = os.path.expanduser(self.path)
70
71 parent = Path(self.path).parent
72 try:
73 if not parent.exists():
74 parent.mkdir(parents=True, exist_ok=True)
75 except OSError:
76 logging.error(f"{self.TAG}Failed to create directory: {parent}")
77
78 try:
79 self.fh = open(self.path, "wb", buffering=0)
80 except OSError:
81 logging.error(f"{self.TAG}Failed to open for writing: {self.path}")
82
83 if self.fh:
84 try:
85 self.fh.write(data)
86 except OSError:
87 logging.error(f"{self.TAG}Failed to write to: {self.path}")
88

Callers 2

requestheadersFunction · 0.85
responseheadersFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…