summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/websockets/handlers/remote-close_wsh.py
blob: aadd99ea950d0d8414c432356443d5f3ce3e444e (plain)
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 mod_pywebsocket.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)