A single WebSocket message sent from one peer to the other. Fragmented WebSocket messages are reassembled by mitmproxy and then represented as a single instance of this class. The [WebSocket RFC](https://tools.ietf.org/html/rfc6455) specifies both text and binary messages. To
| 19 | |
| 20 | |
| 21 | class WebSocketMessage(serializable.Serializable): |
| 22 | """ |
| 23 | A single WebSocket message sent from one peer to the other. |
| 24 | |
| 25 | Fragmented WebSocket messages are reassembled by mitmproxy and then |
| 26 | represented as a single instance of this class. |
| 27 | |
| 28 | The [WebSocket RFC](https://tools.ietf.org/html/rfc6455) specifies both |
| 29 | text and binary messages. To avoid a whole class of nasty type confusion bugs, |
| 30 | mitmproxy stores all message contents as `bytes`. If you need a `str`, you can access the `text` property |
| 31 | on text messages: |
| 32 | |
| 33 | >>> if message.is_text: |
| 34 | >>> text = message.text |
| 35 | """ |
| 36 | |
| 37 | from_client: bool |
| 38 | """True if this messages was sent by the client.""" |
| 39 | type: Opcode |
| 40 | """ |
| 41 | The message type, as per RFC 6455's [opcode](https://tools.ietf.org/html/rfc6455#section-5.2). |
| 42 | |
| 43 | Mitmproxy currently only exposes messages assembled from `TEXT` and `BINARY` frames. |
| 44 | """ |
| 45 | content: bytes |
| 46 | """A byte-string representing the content of this message.""" |
| 47 | timestamp: float |
| 48 | """Timestamp of when this message was received or created.""" |
| 49 | dropped: bool |
| 50 | """True if the message has not been forwarded by mitmproxy, False otherwise.""" |
| 51 | injected: bool |
| 52 | """True if the message was injected and did not originate from a client/server, False otherwise""" |
| 53 | |
| 54 | def __init__( |
| 55 | self, |
| 56 | type: int | Opcode, |
| 57 | from_client: bool, |
| 58 | content: bytes, |
| 59 | timestamp: float | None = None, |
| 60 | dropped: bool = False, |
| 61 | injected: bool = False, |
| 62 | ) -> None: |
| 63 | self.from_client = from_client |
| 64 | self.type = Opcode(type) |
| 65 | self.content = content |
| 66 | self.timestamp: float = timestamp or time.time() |
| 67 | self.dropped = dropped |
| 68 | self.injected = injected |
| 69 | |
| 70 | @classmethod |
| 71 | def from_state(cls, state: WebSocketMessageState): |
| 72 | return cls(*state) |
| 73 | |
| 74 | def get_state(self) -> WebSocketMessageState: |
| 75 | return ( |
| 76 | int(self.type), |
| 77 | self.from_client, |
| 78 | self.content, |
no outgoing calls
searching dependent graphs…