MCPcopy
hub / github.com/mitmproxy/mitmproxy / get_connection

Method get_connection

mitmproxy/proxy/layers/http/__init__.py:1067–1159  ·  view source on GitHub ↗
(
        self, event: GetHttpConnection, *, reuse: bool = True
    )

Source from the content-addressed store, hash-verified

1065 yield from self.event_to_child(self.streams[stream_id], events.Start())
1066
1067 def get_connection(
1068 self, event: GetHttpConnection, *, reuse: bool = True
1069 ) -> layer.CommandGenerator[None]:
1070 # Do we already have a connection we can re-use?
1071 if reuse:
1072 for connection in self.connections:
1073 connection_suitable = event.connection_spec_matches(connection)
1074 if connection_suitable:
1075 if connection in self.waiting_for_establishment:
1076 self.waiting_for_establishment[connection].append(event)
1077 return
1078 elif connection.error:
1079 stream = self.command_sources.pop(event)
1080 yield from self.event_to_child(
1081 stream,
1082 GetHttpConnectionCompleted(event, (None, connection.error)),
1083 )
1084 return
1085 elif connection.connected:
1086 # see "tricky multiplexing edge case" in make_http_connection for an explanation
1087 h2_to_h1 = (
1088 self.context.client.alpn == b"h2"
1089 and connection.alpn != b"h2"
1090 )
1091 if not h2_to_h1:
1092 stream = self.command_sources.pop(event)
1093 yield from self.event_to_child(
1094 stream,
1095 GetHttpConnectionCompleted(event, (connection, None)),
1096 )
1097 return
1098 else:
1099 pass # the connection is at least half-closed already, we want a new one.
1100
1101 context_connection_matches = (
1102 self.context.server not in self.connections
1103 and event.connection_spec_matches(self.context.server)
1104 )
1105 can_use_context_connection = (
1106 context_connection_matches and self.context.server.connected
1107 )
1108 if context_connection_matches and self.context.server.error:
1109 stream = self.command_sources.pop(event)
1110 yield from self.event_to_child(
1111 stream,
1112 GetHttpConnectionCompleted(event, (None, self.context.server.error)),
1113 )
1114 return
1115
1116 context = self.context.fork()
1117
1118 stack = tunnel.LayerStack()
1119
1120 if not can_use_context_connection:
1121 context.server = Server(
1122 address=event.address, transport_protocol=event.transport_protocol
1123 )
1124

Callers 2

event_to_childMethod · 0.95
register_connectionMethod · 0.95

Calls 9

event_to_childMethod · 0.95
ServerClass · 0.90
HttpClientClass · 0.85
appendMethod · 0.45
popMethod · 0.45
forkMethod · 0.45
makeMethod · 0.45

Tested by

no test coverage detected