Create a handshake response to reject the connection. A short plain text response is the best fallback when failing to establish a WebSocket connection. You must send the handshake response with :meth:`send_response`. You may modify the response before sen
(self, status: StatusLike, text: str)
| 475 | ) |
| 476 | |
| 477 | def reject(self, status: StatusLike, text: str) -> Response: |
| 478 | """ |
| 479 | Create a handshake response to reject the connection. |
| 480 | |
| 481 | A short plain text response is the best fallback when failing to |
| 482 | establish a WebSocket connection. |
| 483 | |
| 484 | You must send the handshake response with :meth:`send_response`. |
| 485 | |
| 486 | You may modify the response before sending it, for example by changing |
| 487 | HTTP headers. |
| 488 | |
| 489 | Args: |
| 490 | status: HTTP status code. |
| 491 | text: HTTP response body; it will be encoded to UTF-8. |
| 492 | |
| 493 | Returns: |
| 494 | HTTP response to send to the client. |
| 495 | |
| 496 | """ |
| 497 | # If status is an int instead of an HTTPStatus, fix it automatically. |
| 498 | status = http.HTTPStatus(status) |
| 499 | body = text.encode() |
| 500 | headers = Headers( |
| 501 | [ |
| 502 | ("Date", email.utils.formatdate(usegmt=True)), |
| 503 | ("Connection", "close"), |
| 504 | ("Content-Length", str(len(body))), |
| 505 | ("Content-Type", "text/plain; charset=utf-8"), |
| 506 | ] |
| 507 | ) |
| 508 | return Response(status.value, status.phrase, headers, body) |
| 509 | |
| 510 | def send_response(self, response: Response) -> None: |
| 511 | """ |