MCPcopy
hub / github.com/encode/uvicorn / send

Method send

uvicorn/protocols/websockets/wsproto_impl.py:341–451  ·  view source on GitHub ↗
(self, message: ASGISendEvent)

Source from the content-addressed store, hash-verified

339 self.transport.close()
340
341 async def send(self, message: ASGISendEvent) -> None:
342 await self.writable.wait()
343
344 if not self.handshake_complete:
345 if message["type"] == "websocket.accept":
346 self.logger.info(
347 '%s - "WebSocket %s" [accepted]',
348 get_client_addr(self.scope),
349 get_path_with_query_string(self.scope),
350 )
351 subprotocol = message.get("subprotocol")
352 extra_headers = self.default_headers + list(message.get("headers", []))
353 extensions: list[Extension] = []
354 if self.config.ws_per_message_deflate:
355 extensions.append(PerMessageDeflate())
356 if not self.transport.is_closing():
357 self.handshake_complete = True
358 output = self.conn.send(
359 wsproto.events.AcceptConnection(
360 subprotocol=subprotocol,
361 extensions=extensions,
362 extra_headers=extra_headers,
363 )
364 )
365 self.transport.write(output)
366 self.start_keepalive()
367
368 elif message["type"] == "websocket.close":
369 self.queue.put_nowait({"type": "websocket.disconnect", "code": 1006})
370 self.logger.info(
371 '%s - "WebSocket %s" 403',
372 get_client_addr(self.scope),
373 get_path_with_query_string(self.scope),
374 )
375 self.handshake_complete = True
376 self.close_sent = True
377 event = events.RejectConnection(status_code=403, headers=[])
378 output = self.conn.send(event)
379 self.transport.write(output)
380 self.transport.close()
381
382 elif message["type"] == "websocket.http.response.start":
383 # ensure status code is in the valid range
384 if not (100 <= message["status"] < 600):
385 msg = "Invalid HTTP status code '%d' in response."
386 raise RuntimeError(msg % message["status"])
387 self.logger.info(
388 '%s - "WebSocket %s" %d',
389 get_client_addr(self.scope),
390 get_path_with_query_string(self.scope),
391 message["status"],
392 )
393 self.handshake_complete = True
394 event = events.RejectConnection(
395 status_code=message["status"],
396 headers=list(message["headers"]),
397 has_body=True,
398 )

Callers 9

data_receivedMethod · 0.45
shutdownMethod · 0.45
handle_messageMethod · 0.45
handle_closeMethod · 0.45
handle_pingMethod · 0.45
send_keepalive_pingMethod · 0.45
keepalive_timeoutMethod · 0.45
send_500_responseMethod · 0.45
asgi_sendMethod · 0.45

Calls 6

start_keepaliveMethod · 0.95
get_client_addrFunction · 0.90
is_closingMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected