MCPcopy
hub / github.com/mitmproxy/mitmproxy / handle_client

Method handle_client

mitmproxy/proxy/server.py:127–180  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

125 self._drain_lock = asyncio.Lock()
126
127 async def handle_client(self) -> None:
128 asyncio_utils.set_current_task_debug_info(
129 name=f"client handler",
130 client=self.client.peername,
131 )
132 watch = asyncio_utils.create_task(
133 self.timeout_watchdog.watch(),
134 name="timeout watchdog",
135 keep_ref=False,
136 client=self.client.peername,
137 )
138
139 self.log("client connect")
140 await self.handle_hook(server_hooks.ClientConnectedHook(self.client))
141 if self.client.error:
142 self.log("client kill connection")
143 writer = self.transports.pop(self.client).writer
144 assert writer
145 writer.close()
146 else:
147 await self.server_event(events.Start())
148 handler = asyncio_utils.create_task(
149 self.handle_connection(self.client),
150 name=f"client connection handler",
151 keep_ref=False,
152 client=self.client.peername,
153 )
154 self.transports[self.client].handler = handler
155 await asyncio.wait([handler])
156 if not handler.cancelled() and (e := handler.exception()):
157 self.log(
158 f"connection handler has crashed: {e}",
159 logging.ERROR,
160 exc_info=(type(e), e, e.__traceback__),
161 )
162
163 watch.cancel()
164 while self.wakeup_timer:
165 timer = self.wakeup_timer.pop()
166 timer.cancel()
167
168 self.log("client disconnect")
169 self.client.timestamp_end = time.time()
170 await self.handle_hook(server_hooks.ClientDisconnectedHook(self.client))
171
172 if self.transports:
173 self.log("closing transports...", logging.DEBUG)
174 for io in self.transports.values():
175 if io.handler:
176 io.handler.cancel("client disconnected")
177 await asyncio.wait(
178 [x.handler for x in self.transports.values() if x.handler]
179 )
180 self.log("transports closed!", logging.DEBUG)
181
182 async def open_connection(self, command: commands.OpenConnection) -> None:
183 if not command.connection.address:

Callers 2

handle_streamMethod · 0.80
handleFunction · 0.80

Calls 9

logMethod · 0.95
handle_hookMethod · 0.95
server_eventMethod · 0.95
handle_connectionMethod · 0.95
waitMethod · 0.80
valuesMethod · 0.80
watchMethod · 0.45
popMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected