File: remote-close_wsh.py

package info (click to toggle)
thunderbird 1%3A140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,609,432 kB
  • sloc: cpp: 7,672,442; javascript: 5,901,613; ansic: 3,898,954; python: 1,413,343; xml: 653,997; asm: 462,286; java: 180,927; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (44 lines) | stat: -rw-r--r-- 1,355 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright (c) 2024 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Perform a server-initiated close according to the parameters passed in the
query string. Supported parameters:

 * code=INT: The close code to send in the close frame. If omitted the Close
   frame will have an empty body.

 * reason=TEXT: The reason to be sent in the close frame. Only sent if `code` is
   set.

 * abrupt=1: Close the connection without sending a Close frame.

Example: /remote-close?code=1000&reason=Done

"""

import urllib

from pywebsocket3.handshake import AbortedByUserException


def web_socket_do_extra_handshake(request):
    pass


def web_socket_transfer_data(request):
    parts = urllib.parse.urlsplit(request.uri)
    parameters = urllib.parse.parse_qs(parts.query)
    if 'abrupt' in parameters:
        # Send a ping frame to make sure this isn't misinterpreted as a
        # handshake failure.
        request.ws_stream.send_ping('ping')
        # Rudely close the connection.
        raise AbortedByUserException('Abort the connection')
    code = None
    reason = None
    if 'code' in parameters:
        code = int(parameters['code'][0])
        reason = parameters.get('reason', [''])[0]
    request.ws_stream.close_connection(code, reason)