MCPcopy Index your code
hub / github.com/mitmproxy/mitmproxy / FlowReader

Class FlowReader

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

Source from the content-addressed store, hash-verified

25
26
27class FlowReader:
28 fo: BinaryIO
29
30 def __init__(self, fo: BinaryIO):
31 self.fo = fo
32
33 def peek(self, n: int) -> bytes:
34 try:
35 return cast(BufferedReader, self.fo).peek(n)
36 except AttributeError:
37 # https://github.com/python/cpython/issues/90533: io.BytesIO does not have peek()
38 pos = self.fo.tell()
39 ret = self.fo.read(n)
40 self.fo.seek(pos)
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 6

test_fuzzMethod · 0.90
test_harMethod · 0.90
test_emptyMethod · 0.90
test_unknown_typeMethod · 0.90
test_cannot_migrateMethod · 0.90
read_flows_from_pathsFunction · 0.85

Calls

no outgoing calls

Tested by 5

test_fuzzMethod · 0.72
test_harMethod · 0.72
test_emptyMethod · 0.72
test_unknown_typeMethod · 0.72
test_cannot_migrateMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…