Yields Flow objects from the dump.
(self)
| 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 | |
| 84 | class FilteredFlowWriter: |