Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / websocket / invalid-encode-length_wsh.py
1 import re
2 import struct
3 from mod_pywebsocket import common
4
5
6 def web_socket_do_extra_handshake(request):
7     pass
8
9
10 def web_socket_transfer_data(request):
11     match = re.search(r'\?case=(\d+_\d+)$', request.ws_resource)
12     if match is None:
13         msgutil.send_message(request, 'FAIL: Query value is incorrect or missing')
14         return
15
16     payload_length, extended_length = (match.group(1)).split('_', 1)
17     payload_length = int(payload_length)
18     extended_length = int(extended_length)
19
20     # pywebsocket refuses to create a frame with error encode length.
21     # Thus, we need to build a frame manually.
22     header = chr(0x80 | common.OPCODE_TEXT)  # 0x80 is for "fin" bit.
23     # No Mask and two bytes extended payload length.
24     header += chr(payload_length)
25     if payload_length == 126:
26         header += struct.pack('!H', extended_length)
27     elif payload_length == 127:
28         header += struct.pack('!Q', extended_length)
29     else:
30         msgutil.send_message(request, 'FAIL: Query value is incorrect or missing')
31         return
32     request.connection.write(header)
33     request.connection.write('X' * extended_length)