(self, event: Event)
| 121 | return False |
| 122 | |
| 123 | def _handle_event(self, event: Event) -> CommandGenerator[None]: |
| 124 | if isinstance(event, Start): |
| 125 | self.h2_conn.initiate_connection() |
| 126 | yield SendData(self.conn, self.h2_conn.data_to_send()) |
| 127 | |
| 128 | elif isinstance(event, HttpEvent): |
| 129 | if isinstance(event, (RequestData, ResponseData)): |
| 130 | if self.is_open_for_us(event.stream_id): |
| 131 | self.h2_conn.send_data(event.stream_id, event.data) |
| 132 | elif isinstance(event, (RequestTrailers, ResponseTrailers)): |
| 133 | if self.is_open_for_us(event.stream_id): |
| 134 | trailers = [*event.trailers.fields] |
| 135 | self.h2_conn.send_trailers(event.stream_id, trailers) |
| 136 | elif isinstance(event, (RequestEndOfMessage, ResponseEndOfMessage)): |
| 137 | if self.is_open_for_us(event.stream_id): |
| 138 | self.h2_conn.end_stream(event.stream_id) |
| 139 | elif isinstance(event, (RequestProtocolError, ResponseProtocolError)): |
| 140 | if not self.is_closed(event.stream_id): |
| 141 | stream: h2.stream.H2Stream = self.h2_conn.streams[event.stream_id] |
| 142 | status = event.code.http_status_code() |
| 143 | if ( |
| 144 | isinstance(event, ResponseProtocolError) |
| 145 | and self.is_open_for_us(event.stream_id) |
| 146 | and not stream.state_machine.headers_sent |
| 147 | and status is not None |
| 148 | ): |
| 149 | self.h2_conn.send_headers( |
| 150 | event.stream_id, |
| 151 | [ |
| 152 | (b":status", b"%d" % status), |
| 153 | (b"server", version.MITMPROXY.encode()), |
| 154 | (b"content-type", b"text/html"), |
| 155 | ], |
| 156 | ) |
| 157 | self.h2_conn.send_data( |
| 158 | event.stream_id, |
| 159 | format_error(status, event.message), |
| 160 | end_stream=True, |
| 161 | ) |
| 162 | else: |
| 163 | match event.code: |
| 164 | case ErrorCode.CANCEL | ErrorCode.CLIENT_DISCONNECTED: |
| 165 | error_code = h2.errors.ErrorCodes.CANCEL |
| 166 | case ErrorCode.KILL: |
| 167 | # XXX: Debateable whether this is the best error code. |
| 168 | error_code = h2.errors.ErrorCodes.INTERNAL_ERROR |
| 169 | case ErrorCode.HTTP_1_1_REQUIRED: |
| 170 | error_code = h2.errors.ErrorCodes.HTTP_1_1_REQUIRED |
| 171 | case ErrorCode.PASSTHROUGH_CLOSE: |
| 172 | # FIXME: This probably shouldn't be a protocol error, but an EOM event. |
| 173 | error_code = h2.errors.ErrorCodes.CANCEL |
| 174 | case ( |
| 175 | ErrorCode.GENERIC_CLIENT_ERROR |
| 176 | | ErrorCode.GENERIC_SERVER_ERROR |
| 177 | | ErrorCode.REQUEST_TOO_LARGE |
| 178 | | ErrorCode.RESPONSE_TOO_LARGE |
| 179 | | ErrorCode.CONNECT_FAILED |
| 180 | | ErrorCode.DESTINATION_UNKNOWN |
nothing calls this directly
no test coverage detected