MCPcopy Create free account
hub / github.com/mitmproxy/mitmproxy / _handle_event2

Method _handle_event2

mitmproxy/proxy/layers/http/_http2.py:539–578  ·  view source on GitHub ↗
(self, event: Event)

Source from the content-addressed store, hash-verified

537 yield from self._handle_event(event)
538
539 def _handle_event2(self, event: Event) -> CommandGenerator[None]:
540 if isinstance(event, Wakeup):
541 send_ping_now = (
542 # add one second to avoid unnecessary roundtrip, we don't need to be super correct here.
543 time.time() - self.last_activity + 1
544 > self.context.options.http2_ping_keepalive
545 )
546 if send_ping_now:
547 # PING frames MUST contain 8 octets of opaque data in the payload.
548 # A sender can include any value it chooses and use those octets in any fashion.
549 self.last_activity = time.time()
550 self.h2_conn.ping(b"0" * 8)
551 data = self.h2_conn.data_to_send()
552 if data is not None:
553 yield Log(
554 f"Send HTTP/2 keep-alive PING to {human.format_address(self.conn.peername)}",
555 DEBUG,
556 )
557 yield SendData(self.conn, data)
558 time_until_next_ping = self.context.options.http2_ping_keepalive - (
559 time.time() - self.last_activity
560 )
561 yield RequestWakeup(time_until_next_ping)
562 return
563
564 self.last_activity = time.time()
565 if isinstance(event, Start):
566 if self.context.options.http2_ping_keepalive > 0:
567 yield RequestWakeup(self.context.options.http2_ping_keepalive)
568 yield from super()._handle_event(event)
569 elif isinstance(event, RequestHeaders):
570 self.h2_conn.send_headers(
571 event.stream_id,
572 headers=(yield from format_h2_request_headers(self.context, event)),
573 end_stream=event.end_stream,
574 )
575 self.streams[event.stream_id] = StreamState.EXPECTING_HEADERS
576 yield SendData(self.conn, self.h2_conn.data_to_send())
577 else:
578 yield from super()._handle_event(event)
579
580 def handle_h2_event(self, event: h2.events.Event) -> CommandGenerator[bool]:
581 if isinstance(event, h2.events.ResponseReceived):

Callers 1

_handle_eventMethod · 0.95

Calls 6

SendDataClass · 0.85
RequestWakeupClass · 0.85
LogClass · 0.50
_handle_eventMethod · 0.45
send_headersMethod · 0.45

Tested by

no test coverage detected