(cut: str, f: flow.Flow)
| 29 | |
| 30 | |
| 31 | def extract(cut: str, f: flow.Flow) -> str | bytes: |
| 32 | # Hack for https://github.com/mitmproxy/mitmproxy/issues/6721: |
| 33 | # Make "save body" keybind work for WebSocket flows. |
| 34 | # Ideally the keybind would be smarter and this here can get removed. |
| 35 | if ( |
| 36 | isinstance(f, http.HTTPFlow) |
| 37 | and f.websocket |
| 38 | and cut in ("request.content", "response.content") |
| 39 | ): |
| 40 | return f.websocket._get_formatted_messages() |
| 41 | |
| 42 | path = cut.split(".") |
| 43 | current: Any = f |
| 44 | for i, spec in enumerate(path): |
| 45 | if spec.startswith("_"): |
| 46 | raise exceptions.CommandError("Can't access internal attribute %s" % spec) |
| 47 | |
| 48 | part = getattr(current, spec, None) |
| 49 | if i == len(path) - 1: |
| 50 | if spec == "port" and is_addr(current): |
| 51 | return str(current[1]) |
| 52 | if spec == "host" and is_addr(current): |
| 53 | return str(current[0]) |
| 54 | elif spec.startswith("header["): |
| 55 | if not current: |
| 56 | return "" |
| 57 | return current.headers.get(headername(spec), "") |
| 58 | elif isinstance(part, bytes): |
| 59 | return part |
| 60 | elif isinstance(part, bool): |
| 61 | return "true" if part else "false" |
| 62 | elif isinstance(part, certs.Cert): # pragma: no cover |
| 63 | return part.to_pem().decode("ascii") |
| 64 | elif ( |
| 65 | isinstance(part, list) |
| 66 | and len(part) > 0 |
| 67 | and isinstance(part[0], certs.Cert) |
| 68 | ): |
| 69 | # TODO: currently this extracts only the very first cert as PEM-encoded string. |
| 70 | return part[0].to_pem().decode("ascii") |
| 71 | current = part |
| 72 | return str(current or "") |
| 73 | |
| 74 | |
| 75 | def extract_str(cut: str, f: flow.Flow) -> str: |
no test coverage detected
searching dependent graphs…