MCPcopy
hub / github.com/mitmproxy/mitmproxy / stream

Method stream

mitmproxy/io/io.py:43–81  ·  view source on GitHub ↗

Yields Flow objects from the dump.

(self)

Source from the content-addressed store, hash-verified

41 return ret
42
43 def stream(self) -> Iterable[flow.Flow]:
44 """
45 Yields Flow objects from the dump.
46 """
47
48 if self.peek(4).startswith(
49 b"\xef\xbb\xbf{"
50 ): # skip BOM, usually added by Fiddler
51 self.fo.read(3)
52 if self.peek(1).startswith(b"{"):
53 try:
54 har_file = json.loads(self.fo.read().decode("utf-8"))
55
56 for request_json in har_file["log"]["entries"]:
57 yield request_to_flow(request_json)
58
59 except Exception:
60 raise exceptions.FlowReadException(
61 "Unable to read HAR file. Please provide a valid HAR file"
62 )
63
64 else:
65 try:
66 while True:
67 # FIXME: This cast hides a lack of dynamic type checking
68 loaded = cast(
69 dict[Union[bytes, str], Any],
70 tnetstring.load(self.fo),
71 )
72 try:
73 if not isinstance(loaded, dict):
74 raise ValueError(f"Invalid flow: {loaded=}")
75 yield flow.Flow.from_state(compat.migrate_flow(loaded))
76 except ValueError as e:
77 raise exceptions.FlowReadException(e) from e
78 except (ValueError, TypeError, IndexError) as e:
79 if str(e) == "not a tnetstring: empty file":
80 return # Error is due to EOF
81 raise exceptions.FlowReadException("Invalid data format.") from e
82
83
84class FilteredFlowWriter:

Callers 15

test_roundtripMethod · 0.95
test_filterMethod · 0.95
test_errorMethod · 0.95
test_versioncheckMethod · 0.95
test_loadFunction · 0.95
test_cannot_convertFunction · 0.95
test_fuzzMethod · 0.95
test_harMethod · 0.95
rdFunction · 0.95
load_flowsMethod · 0.95
test_emptyMethod · 0.80
test_unknown_typeMethod · 0.80

Calls 7

peekMethod · 0.95
request_to_flowFunction · 0.90
loadsMethod · 0.80
readMethod · 0.45
decodeMethod · 0.45
loadMethod · 0.45
from_stateMethod · 0.45

Tested by 12

test_roundtripMethod · 0.76
test_filterMethod · 0.76
test_errorMethod · 0.76
test_versioncheckMethod · 0.76
test_loadFunction · 0.76
test_cannot_convertFunction · 0.76
test_fuzzMethod · 0.76
test_harMethod · 0.76
rdFunction · 0.76
test_emptyMethod · 0.64
test_unknown_typeMethod · 0.64
test_cannot_migrateMethod · 0.64