Debian Codegraph @

testing/web-platform/tests/tools/third_party/pywebsocket3/example/benchmark_helper_wsh.py

raw
1 # Copyright 2013, Google Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 """Handler for benchmark.html."""
30
31 from __future__ import absolute_import
32
33 import six
34
35
36 def web_socket_do_extra_handshake(request):
37 # Turn off compression.
38 request.ws_extension_processors = []
39
40
41 def web_socket_transfer_data(request):
42 data = b''
43
44 while True:
45 command = request.ws_stream.receive_message()
46 if command is None:
47 return
48
49 if not isinstance(command, six.text_type):
50 raise ValueError('Invalid command data:' + command)
51 commands = command.split(' ')
52 if len(commands) == 0:
53 raise ValueError('Invalid command data: ' + command)
54
55 if commands[0] == 'receive':
56 if len(commands) != 2:
57 raise ValueError(
58 'Illegal number of arguments for send command' + command)
59 size = int(commands[1])
60
61 # Reuse data if possible.
62 if len(data) != size:
63 data = b'a' * size
64 request.ws_stream.send_message(data, binary=True)
65 elif commands[0] == 'send':
66 if len(commands) != 2:
67 raise ValueError(
68 'Illegal number of arguments for receive command' +
69 command)
70 verify_data = commands[1] == '1'
71
72 data = request.ws_stream.receive_message()
73 if data is None:
74 raise ValueError('Payload not received')
75 size = len(data)
76
77 if verify_data:
78 if data != b'a' * size:
79 raise ValueError('Payload verification failed')
80
81 request.ws_stream.send_message(str(size))
82 else:
83 raise ValueError('Invalid command: ' + commands[0])
84
85
86 # vi:sts=4 sw=4 et