Commit 648712d3 authored by Adam Rice's avatar Adam Rice Committed by Commit Bot

Improve conformance of WebSocket layout tests to pep8

This CL was generated by running pyformat and then applying manual fixes
to the result. The command-line used was:

pyformat --indent_size=4 -r -i --force_quote_type single .

Long comments, strings, if conditions and assignments that had been left
behind by pyformat were manually broken to make the pep8 tool happy.

There are still some things the pep8 tool complains about:

* pyformat wraps to 80 columns, but pep8 wants lines to be at most 79
  characters. 80-column lines have been left as-is as it doesn't seem
  worth fixing.
* handshake-fail-by-maxlength_wsh.py and
  handshake-fail-by-prepended-null_wsh.py have copyright messages with
  lines longer than 80 columns. Not changed.

Bug: 237056
Change-Id: I8ff73bc1e677975e1a99d86815971750b275a0a3
Reviewed-on: https://chromium-review.googlesource.com/570080Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Commit-Queue: Adam Rice <ricea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486753}
parent 831fd2f2
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
msg = 'HTTP/1.1 101 Switching Protocols\r\n' msg = 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += '\xa5:\r\n' msg += '\xa5:\r\n'
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
......
...@@ -9,8 +9,10 @@ def web_socket_do_extra_handshake(request): ...@@ -9,8 +9,10 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
messages_to_send = ['Hello, world!', '', all_distinct_bytes()] messages_to_send = ['Hello, world!', '', all_distinct_bytes()]
for message in messages_to_send: for message in messages_to_send:
# FIXME: Should use better API to send binary messages when pywebsocket supports it. # FIXME: Should use better API to send binary messages when pywebsocket
header = stream.create_header(common.OPCODE_BINARY, len(message), 1, 0, 0, 0, 0) # supports it.
header = stream.create_header(common.OPCODE_BINARY,
len(message), 1, 0, 0, 0, 0)
request.connection.write(header + message) request.connection.write(header + message)
......
...@@ -7,5 +7,8 @@ def web_socket_do_extra_handshake(request): ...@@ -7,5 +7,8 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
payload = 'This text should be ignored. \xff' # '\xff' will never appear in UTF-8 encoded data. # '\xff' will never appear in UTF-8 encoded data.
request.connection.write(stream.create_header(common.OPCODE_TEXT, len(payload), 1, 0, 0, 0, 0) + payload) payload = 'This text should be ignored. \xff'
request.connection.write(
stream.create_header(common.OPCODE_TEXT, len(payload), 1, 0, 0, 0, 0) +
payload)
...@@ -14,7 +14,9 @@ def web_socket_transfer_data(request): ...@@ -14,7 +14,9 @@ def web_socket_transfer_data(request):
if type(message) == str and message == expected_message: if type(message) == str and message == expected_message:
msgutil.send_message(request, 'PASS: Message #%d.' % test_number) msgutil.send_message(request, 'PASS: Message #%d.' % test_number)
else: else:
msgutil.send_message(request, 'FAIL: Message #%d: Received unexpected message: %r' % (test_number, message)) msgutil.send_message(
request, 'FAIL: Message #%d: Received unexpected message: %r' %
(test_number, message))
def all_distinct_bytes(): def all_distinct_bytes():
......
...@@ -14,8 +14,8 @@ def web_socket_transfer_data(request): ...@@ -14,8 +14,8 @@ def web_socket_transfer_data(request):
msgutil.send_message(request, 'Client should ignore this message') msgutil.send_message(request, 'Client should ignore this message')
# Send only first two bytes of the received frame. The remaining four bytes are # Send only first two bytes of the received frame. The remaining four bytes
# "masking key", which changes every time the test runs. # are "masking key", which changes every time the test runs.
data = struct.pack('!H', 1000) + 'close_frame[:2]=%r' % close_frame[:2] data = struct.pack('!H', 1000) + 'close_frame[:2]=%r' % close_frame[:2]
request.connection.write(stream.create_close_frame(data)) request.connection.write(stream.create_close_frame(data))
......
...@@ -31,7 +31,6 @@ import cgi ...@@ -31,7 +31,6 @@ import cgi
import json import json
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
connections = {} connections = {}
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import logging import logging
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import common from mod_pywebsocket import common
from mod_pywebsocket import stream from mod_pywebsocket import stream
import zlib import zlib
...@@ -37,12 +36,17 @@ def web_socket_do_extra_handshake(request): ...@@ -37,12 +36,17 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
compress = zlib.compressobj( compress = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED,
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS) -zlib.MAX_WBITS)
compressed_message = compress.compress('close message') compressed_message = compress.compress('close message')
compressed_message += compress.flush(zlib.Z_SYNC_FLUSH) compressed_message += compress.flush(zlib.Z_SYNC_FLUSH)
compressed_message = compressed_message[:-4] compressed_message = compressed_message[:-4]
header = stream.create_header( header = stream.create_header(
opcode=common.OPCODE_CLOSE, payload_length=len(compressed_message), opcode=common.OPCODE_CLOSE,
fin=1, rsv1=1, rsv2=0, rsv3=0, mask=False) payload_length=len(compressed_message),
fin=1,
rsv1=1,
rsv2=0,
rsv3=0,
mask=False)
request.connection.write(header + compressed_message) request.connection.write(header + compressed_message)
...@@ -2,7 +2,6 @@ import cgi ...@@ -2,7 +2,6 @@ import cgi
import time import time
import threading import threading
lock = threading.Lock() lock = threading.Lock()
connections = set() connections = set()
next_test_id = 0 next_test_id = 0
...@@ -13,10 +12,10 @@ def web_socket_do_extra_handshake(request): ...@@ -13,10 +12,10 @@ def web_socket_do_extra_handshake(request):
if len(query_string) == 1: if len(query_string) == 1:
return return
params = cgi.parse_qs(query_string[1]) params = cgi.parse_qs(query_string[1])
mode = params["mode"][0] mode = params['mode'][0]
if mode == "new_test": if mode == 'new_test':
new_test(request) new_test(request)
elif mode == "do_test": elif mode == 'do_test':
do_test(request, params) do_test(request, params)
...@@ -31,16 +30,16 @@ def new_test(request): ...@@ -31,16 +30,16 @@ def new_test(request):
def do_test(request, params): def do_test(request, params):
"""Check that no other connection is happening at the same time.""" """Check that no other connection is happening at the same time."""
global lock, connections global lock, connections
id = params["id"][0] id = params['id'][0]
with lock: with lock:
if id in connections: if id in connections:
request.response = "FAIL" request.response = 'FAIL'
return return
connections.add(id) connections.add(id)
time.sleep(0.05) time.sleep(0.05)
with lock: with lock:
connections.remove(id) connections.remove(id)
request.response = "PASS" request.response = 'PASS'
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import codecs import codecs
......
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
message = 'HTTP/1.1 101 Switching Protocols\r\n' message = 'HTTP/1.1 101 Switching Protocols\r\n'
message += 'Upgrade: websocket\r\n' message += 'Upgrade: websocket\r\n'
message += 'Connection: Upgrade\r\n' message += 'Connection: Upgrade\r\n'
message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
message += 'foo: bar, baz\r\n' message += 'foo: bar, baz\r\n'
message += 'foo: hoge\r\n' message += 'foo: hoge\r\n'
message += 'FOO: FUGA\r\n' message += 'FOO: FUGA\r\n'
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import common, msgutil from mod_pywebsocket import common, msgutil
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
......
...@@ -7,13 +7,16 @@ def web_socket_do_extra_handshake(request): ...@@ -7,13 +7,16 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# pyformat: disable
messages_to_send = [['Hello, ', 'world!'], messages_to_send = [['Hello, ', 'world!'],
['', 'Hello, ', '', 'world!', ''], ['', 'Hello, ', '', 'world!', ''],
['', '', ''], ['', '', ''],
[chr(i) for i in xrange(256)]] [chr(i) for i in xrange(256)]]
# pyformat: enable
for message_list in messages_to_send: for message_list in messages_to_send:
for index, message in enumerate(message_list): for index, message in enumerate(message_list):
# FIXME: Should use better API to send binary messages when pywebsocket supports it. # FIXME: Should use better API to send binary messages when
# pywebsocket supports it.
if index == 0: if index == 0:
opcode = common.OPCODE_BINARY opcode = common.OPCODE_BINARY
else: else:
...@@ -22,5 +25,6 @@ def web_socket_transfer_data(request): ...@@ -22,5 +25,6 @@ def web_socket_transfer_data(request):
final = 0 final = 0
else: else:
final = 1 final = 1
header = stream.create_header(opcode, len(message), final, 0, 0, 0, 0) header = stream.create_header(opcode,
len(message), final, 0, 0, 0, 0)
request.connection.write(header + message) request.connection.write(header + message)
...@@ -8,5 +8,9 @@ def web_socket_do_extra_handshake(request): ...@@ -8,5 +8,9 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# Fragmented control frame is prohibited. The client must abort the connection. # Fragmented control frame is prohibited. The client must abort the
request.connection.write(stream.create_text_frame('This message should be ignored.', opcode=common.OPCODE_PING, fin=0)) # connection.
request.connection.write(
stream.create_text_frame(
'This message should be ignored.', opcode=common.OPCODE_PING,
fin=0))
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
message = 'HTTP/1.1 101 Switching Protocols\r\n' message = 'HTTP/1.1 101 Switching Protocols\r\n'
message += 'Upgrade: websocket\r\n' message += 'Upgrade: websocket\r\n'
message += 'Connection: Upgrade\r\n' message += 'Connection: Upgrade\r\n'
message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
message += 'Sec-WebSocket-Extensions: x-foo\r\n' message += 'Sec-WebSocket-Extensions: x-foo\r\n'
message += '\r\n' message += '\r\n'
request.connection.write(message) request.connection.write(message)
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import handshake from mod_pywebsocket import handshake
from mod_pywebsocket.handshake.hybi import compute_accept from mod_pywebsocket.handshake.hybi import compute_accept
...@@ -31,7 +30,8 @@ def web_socket_do_extra_handshake(request): ...@@ -31,7 +30,8 @@ def web_socket_do_extra_handshake(request):
msg += 'HTTP/1.1 101 Switching Protocols\r\n' msg += 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
# Prevents pywebsocket from sending its own handshake message. # Prevents pywebsocket from sending its own handshake message.
......
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
msg = 'HTTP/1.1 101 Switching Protocols\r\n' msg = 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += 'Sec-WebSocket-Protocol: MismatchProtocol\r\n' msg += 'Sec-WebSocket-Protocol: MismatchProtocol\r\n'
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
......
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
msg = 'HTTP/1.1 101 Switching Protocols\r\n' msg = 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += 'Sec-WebSocket-Accept: XXXXthisiswrongXXXX\r\n' msg += 'Sec-WebSocket-Accept: XXXXthisiswrongXXXX\r\n'
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
......
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
msg = 'HTTP/1.1 101 Switching Protocols\r\n' msg = 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += 'Sec-WebSocket-Protocol: MatchProtocol\r\n' msg += 'Sec-WebSocket-Protocol: MatchProtocol\r\n'
msg += 'Sec-WebSocket-Protocol: MismatchProtocol\r\n' msg += 'Sec-WebSocket-Protocol: MismatchProtocol\r\n'
msg += '\r\n' msg += '\r\n'
......
...@@ -5,8 +5,9 @@ from mod_pywebsocket.handshake.hybi import compute_accept ...@@ -5,8 +5,9 @@ from mod_pywebsocket.handshake.hybi import compute_accept
def web_socket_do_extra_handshake(request): def web_socket_do_extra_handshake(request):
msg = 'HTTP/1.1 101 Switching Protocols\r\n' msg = 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
# Missing 'Connection: Upgrade\r\n' # Missing 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
print msg print msg
......
...@@ -4,9 +4,10 @@ from mod_pywebsocket.handshake.hybi import compute_accept ...@@ -4,9 +4,10 @@ from mod_pywebsocket.handshake.hybi import compute_accept
def web_socket_do_extra_handshake(request): def web_socket_do_extra_handshake(request):
msg = 'HTTP/1.1 101 Switching Protocols\r\n' msg = 'HTTP/1.1 101 Switching Protocols\r\n'
# Missing 'Upgrade: websocket\r\n' # Missing 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
print msg print msg
......
...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request): ...@@ -6,7 +6,8 @@ def web_socket_do_extra_handshake(request):
message = 'HTTP/1.1 101 Switching Protocols\r\n' message = 'HTTP/1.1 101 Switching Protocols\r\n'
message += 'Upgrade: websocket\r\n' message += 'Upgrade: websocket\r\n'
message += 'Connection: Upgrade\r\n' message += 'Connection: Upgrade\r\n'
message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
message += '\r\n' message += '\r\n'
request.connection.write(message) request.connection.write(message)
# Prevents pywebsocket from sending its own handshake message. # Prevents pywebsocket from sending its own handshake message.
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import time import time
from mod_pywebsocket import stream from mod_pywebsocket import stream
from mod_pywebsocket.handshake.hybi import compute_accept from mod_pywebsocket.handshake.hybi import compute_accept
...@@ -37,7 +36,8 @@ def web_socket_do_extra_handshake(request): ...@@ -37,7 +36,8 @@ def web_socket_do_extra_handshake(request):
msg += 'HTTP/1.1 101 Switching Protocols\r\n' msg += 'HTTP/1.1 101 Switching Protocols\r\n'
msg += 'Upgrade: websocket\r\n' msg += 'Upgrade: websocket\r\n'
msg += 'Connection: Upgrade\r\n' msg += 'Connection: Upgrade\r\n'
msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] msg += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
msg += '\r\n' msg += '\r\n'
request.connection.write(msg) request.connection.write(msg)
# continue writing data until the client disconnects # continue writing data until the client disconnects
......
...@@ -8,5 +8,10 @@ def web_socket_do_extra_handshake(request): ...@@ -8,5 +8,10 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# A new frame is arrived before the previous fragmented frame has finished. # A new frame is arrived before the previous fragmented frame has finished.
request.connection.write(stream.create_text_frame('This message ', opcode=common.OPCODE_TEXT, fin=0)) request.connection.write(
request.connection.write(stream.create_text_frame('should be ignored.', opcode=common.OPCODE_TEXT, fin=1)) # Not OPCODE_CONTINUATION. stream.create_text_frame(
'This message ', opcode=common.OPCODE_TEXT, fin=0))
request.connection.write(
stream.create_text_frame(
'should be ignored.', opcode=common.OPCODE_TEXT,
fin=1)) # Not OPCODE_CONTINUATION.
...@@ -9,9 +9,9 @@ def web_socket_do_extra_handshake(request): ...@@ -9,9 +9,9 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
payload1 = 'Invalid continuation frame to be ignored.' payload1 = 'Invalid continuation frame to be ignored.'
payload2 = 'Valid frame after closing should be disposed.' payload2 = 'Valid frame after closing should be disposed.'
request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, request.connection.write(
len(payload1), stream.create_header(common.OPCODE_CONTINUATION,
1, 0, 0, 0, 0) + payload1) len(payload1), 1, 0, 0, 0, 0) + payload1)
request.connection.write(stream.create_header(common.OPCODE_TEXT, request.connection.write(
len(payload2), stream.create_header(common.OPCODE_TEXT, len(payload2), 1, 0, 0, 0, 0) +
1, 0, 0, 0, 0) + payload2) payload2)
...@@ -10,7 +10,8 @@ def web_socket_do_extra_handshake(request): ...@@ -10,7 +10,8 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
match = re.search(r'\?case=(\d+_\d+)$', request.ws_resource) match = re.search(r'\?case=(\d+_\d+)$', request.ws_resource)
if match is None: if match is None:
msgutil.send_message(request, 'FAIL: Query value is incorrect or missing') msgutil.send_message(request,
'FAIL: Query value is incorrect or missing')
return return
payload_length, extended_length = (match.group(1)).split('_', 1) payload_length, extended_length = (match.group(1)).split('_', 1)
...@@ -27,7 +28,8 @@ def web_socket_transfer_data(request): ...@@ -27,7 +28,8 @@ def web_socket_transfer_data(request):
elif payload_length == 127: elif payload_length == 127:
header += struct.pack('!Q', extended_length) header += struct.pack('!Q', extended_length)
else: else:
msgutil.send_message(request, 'FAIL: Query value is incorrect or missing') msgutil.send_message(request,
'FAIL: Query value is incorrect or missing')
return return
request.connection.write(header) request.connection.write(header)
request.connection.write('X' * extended_length) request.connection.write('X' * extended_length)
...@@ -6,5 +6,7 @@ def web_socket_do_extra_handshake(request): ...@@ -6,5 +6,7 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# pywebsocket does not mask message by default. We need to build a frame manually to mask it. # pywebsocket does not mask message by default. We need to build a frame
request.connection.write(stream.create_text_frame('The Masked Message', mask=True)) # manually to mask it.
request.connection.write(
stream.create_text_frame('The Masked Message', mask=True))
...@@ -9,4 +9,5 @@ def web_socket_do_extra_handshake(request): ...@@ -9,4 +9,5 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# All control frames must have a payload length of 125 bytes or less. # All control frames must have a payload length of 125 bytes or less.
message = 'X' * 126 message = 'X' * 126
request.connection.write(stream.create_text_frame(message, opcode=common.OPCODE_PING, fin=1)) request.connection.write(
stream.create_text_frame(message, opcode=common.OPCODE_PING, fin=1))
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import urllib import urllib
from mod_pywebsocket import handshake from mod_pywebsocket import handshake
from mod_pywebsocket.handshake.hybi import compute_accept from mod_pywebsocket.handshake.hybi import compute_accept
...@@ -41,7 +40,8 @@ def web_socket_do_extra_handshake(request): ...@@ -41,7 +40,8 @@ def web_socket_do_extra_handshake(request):
message = 'HTTP/1.1 101 Switching Protocols\r\n' message = 'HTTP/1.1 101 Switching Protocols\r\n'
message += 'Upgrade: websocket\r\n' message += 'Upgrade: websocket\r\n'
message += 'Connection: Upgrade\r\n' message += 'Connection: Upgrade\r\n'
message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] message += 'Sec-WebSocket-Accept: %s\r\n' % compute_accept(
request.headers_in['Sec-WebSocket-Key'])[0]
message += 'Sec-WebSocket-Extensions: permessage-deflate' message += 'Sec-WebSocket-Extensions: permessage-deflate'
if parameters: if parameters:
message += '; %s\r\n' % parameters message += '; %s\r\n' % parameters
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import urlparse import urlparse
import zlib import zlib
from mod_pywebsocket import common, util from mod_pywebsocket import common, util
...@@ -34,7 +33,6 @@ from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor ...@@ -34,7 +33,6 @@ from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor
from mod_pywebsocket.extensions import ExtensionProcessorInterface from mod_pywebsocket.extensions import ExtensionProcessorInterface
from mod_pywebsocket.common import ExtensionParameter from mod_pywebsocket.common import ExtensionParameter
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
_ENABLE_MESSAGE = u'EnableCompression' _ENABLE_MESSAGE = u'EnableCompression'
_DISABLE_MESSAGE = u'DisableCompression' _DISABLE_MESSAGE = u'DisableCompression'
...@@ -44,8 +42,7 @@ _client_max_window_bits = 15 ...@@ -44,8 +42,7 @@ _client_max_window_bits = 15
def _get_permessage_deflate_extension_processor(request): def _get_permessage_deflate_extension_processor(request):
for extension_processor in request.ws_extension_processors: for extension_processor in request.ws_extension_processors:
if isinstance(extension_processor, if isinstance(extension_processor, PerMessageDeflateExtensionProcessor):
PerMessageDeflateExtensionProcessor):
return extension_processor return extension_processor
return None return None
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from mod_pywebsocket import common from mod_pywebsocket import common
from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor
from mod_pywebsocket.stream import create_header from mod_pywebsocket.stream import create_header
...@@ -10,8 +9,7 @@ from mod_pywebsocket.stream import create_header ...@@ -10,8 +9,7 @@ from mod_pywebsocket.stream import create_header
def _get_permessage_deflate_extension_processor(request): def _get_permessage_deflate_extension_processor(request):
for extension_processor in request.ws_extension_processors: for extension_processor in request.ws_extension_processors:
if isinstance(extension_processor, if isinstance(extension_processor, PerMessageDeflateExtensionProcessor):
PerMessageDeflateExtensionProcessor):
return extension_processor return extension_processor
return None return None
...@@ -31,12 +29,24 @@ def web_socket_transfer_data(request): ...@@ -31,12 +29,24 @@ def web_socket_transfer_data(request):
# Strip \x00\x00\xff\xff # Strip \x00\x00\xff\xff
stripped = payload[:-4] stripped = payload[:-4]
header = create_header(common.OPCODE_TEXT, len(payload), header = create_header(
fin=0, rsv1=1, rsv2=0, rsv3=0, mask=False) common.OPCODE_TEXT,
len(payload),
fin=0,
rsv1=1,
rsv2=0,
rsv3=0,
mask=False)
request.ws_stream._write(header + payload) request.ws_stream._write(header + payload)
header = create_header(common.OPCODE_CONTINUATION, len(stripped), header = create_header(
fin=1, rsv1=0, rsv2=0, rsv3=0, mask=False) common.OPCODE_CONTINUATION,
len(stripped),
fin=1,
rsv1=0,
rsv2=0,
rsv3=0,
mask=False)
request.ws_stream._write(header + stripped) request.ws_stream._write(header + stripped)
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import urlparse import urlparse
from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor
......
...@@ -26,13 +26,11 @@ ...@@ -26,13 +26,11 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import urlparse import urlparse
from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor
from mod_pywebsocket.extensions import ExtensionProcessorInterface from mod_pywebsocket.extensions import ExtensionProcessorInterface
from mod_pywebsocket.common import ExtensionParameter from mod_pywebsocket.common import ExtensionParameter
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
_ENABLE_MESSAGE = u'EnableCompression' _ENABLE_MESSAGE = u'EnableCompression'
_DISABLE_MESSAGE = u'DisableCompression' _DISABLE_MESSAGE = u'DisableCompression'
......
...@@ -17,11 +17,16 @@ def web_socket_transfer_data(request): ...@@ -17,11 +17,16 @@ def web_socket_transfer_data(request):
msgutil.send_ping(request, send_payload) msgutil.send_ping(request, send_payload)
# We need to use an internal function to detect a pong frame from the client. # We need to use an internal function to detect a pong frame from the
opcode, recv_payload, final, reserved1, reserved2, reserved3 = request.ws_stream._receive_frame() # client.
if opcode == common.OPCODE_PONG and recv_payload == send_payload and final and not reserved1 and not reserved2 and not reserved3: opcode, recv_payload, final, reserved1, reserved2, reserved3 = \
request.ws_stream._receive_frame()
if (opcode == common.OPCODE_PONG and recv_payload == send_payload and
final and not reserved1 and not reserved2 and not reserved3):
msgutil.send_message(request, 'PASS') msgutil.send_message(request, 'PASS')
else: else:
msgutil.send_message(request, msgutil.send_message(
'FAIL: Received unexpected frame: opcode = %r, payload = %r, final = %r, reserved1 = %r, reserved2 = %r, reserved3 = %r' % request,
(opcode, recv_payload, final, reserved1, reserved2, reserved3)) 'FAIL: Received unexpected frame: opcode = %r, payload = %r, '
'final = %r, reserved1 = %r, reserved2 = %r, reserved3 = %r'
% (opcode, recv_payload, final, reserved1, reserved2, reserved3))
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import cgi import cgi
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -4,5 +4,5 @@ def web_socket_do_extra_handshake(request): ...@@ -4,5 +4,5 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
request.ws_stream._send_pong("{payload: 'yes'}") request.ws_stream._send_pong("{payload: 'yes'}")
request.ws_stream.send_message("sent pong") request.ws_stream.send_message('sent pong')
line = request.ws_stream.receive_message() line = request.ws_stream.receive_message()
...@@ -11,9 +11,11 @@ def web_socket_do_extra_handshake(request): ...@@ -11,9 +11,11 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
match = re.search(r'\?opcode=(\d+)$', request.ws_resource) match = re.search(r'\?opcode=(\d+)$', request.ws_resource)
if match is None: if match is None:
msgutil.send_message(request, 'FAIL: Query value is incorrect or missing') msgutil.send_message(request,
'FAIL: Query value is incorrect or missing')
return return
opcode = int(match.group(1)) opcode = int(match.group(1))
payload = 'This text should be ignored. (opcode = %d)' % opcode payload = 'This text should be ignored. (opcode = %d)' % opcode
request.connection.write(stream.create_header(opcode, len(payload), 1, 0, 0, 0, 0) + payload) request.connection.write(
stream.create_header(opcode, len(payload), 1, 0, 0, 0, 0) + payload)
...@@ -21,10 +21,15 @@ def web_socket_transfer_data(request): ...@@ -21,10 +21,15 @@ def web_socket_transfer_data(request):
for test_number, expected_message in enumerate(expected_messages): for test_number, expected_message in enumerate(expected_messages):
frame = _retrieve_frame(request.ws_stream) frame = _retrieve_frame(request.ws_stream)
if frame.opcode == common.OPCODE_BINARY and frame.payload == expected_message and frame.fin: if (frame.opcode == common.OPCODE_BINARY and
frame.payload == expected_message and frame.fin):
msgutil.send_message(request, 'PASS: Message #%d.' % test_number) msgutil.send_message(request, 'PASS: Message #%d.' % test_number)
else: else:
msgutil.send_message(request, 'FAIL: Message #%d: Received unexpected frame: opcode = %r, payload = %r, final = %r' % (test_number, frame.opcode, frame.payload, frame.fin)) msgutil.send_message(
request,
'FAIL: Message #%d: Received unexpected frame: opcode = %r, '
'payload = %r, final = %r'
% (test_number, frame.opcode, frame.payload, frame.fin))
def all_distinct_bytes(): def all_distinct_bytes():
......
...@@ -7,4 +7,6 @@ def web_socket_do_extra_handshake(request): ...@@ -7,4 +7,6 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# send 2 messages in one packet. # send 2 messages in one packet.
request.connection.write(stream.create_text_frame('first message') + stream.create_text_frame('second message')) request.connection.write(
stream.create_text_frame('first message') +
stream.create_text_frame('second message'))
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import urlparse import urlparse
...@@ -39,8 +38,7 @@ def web_socket_do_extra_handshake(request): ...@@ -39,8 +38,7 @@ def web_socket_do_extra_handshake(request):
'ws-domain-local-ip=1; Domain=127.0.0.1' + max_age, 'ws-domain-local-ip=1; Domain=127.0.0.1' + max_age,
'ws-domain-example-com=1; Domain=example.com' + max_age, 'ws-domain-example-com=1; Domain=example.com' + max_age,
'ws-path-root=1; Path=/' + max_age, 'ws-path-root=1; Path=/' + max_age,
'ws-path-foobar=1; Path=/foo/bar' + max_age, 'ws-path-foobar=1; Path=/foo/bar' + max_age, 'ws=1' + max_age,
'ws=1' + max_age,
'same-site-strict=1; SameSite=Strict' + max_age, 'same-site-strict=1; SameSite=Strict' + max_age,
'same-site-lax=1; SameSite=Lax' + max_age 'same-site-lax=1; SameSite=Lax' + max_age
] ]
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -17,8 +17,8 @@ def web_socket_transfer_data(request): ...@@ -17,8 +17,8 @@ def web_socket_transfer_data(request):
header += struct.pack('!Q', length) header += struct.pack('!Q', length)
request.connection.write(header) request.connection.write(header)
# Send data indefinitely to simulate a real (broken) server sending a big frame. # Send data indefinitely to simulate a real (broken) server sending a big
# A client should ignore these bytes and abort the connection. # frame. A client should ignore these bytes and abort the connection.
while True: while True:
request.connection.write('X' * 4096) request.connection.write('X' * 4096)
time.sleep(1) time.sleep(1)
...@@ -26,10 +26,8 @@ ...@@ -26,10 +26,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
# Hello in Japanese # Hello in Japanese
_UNICODE_HELLO = u'\u3053\u3093\u306b\u3061\u306f' _UNICODE_HELLO = u'\u3053\u3093\u306b\u3061\u306f'
......
...@@ -9,27 +9,34 @@ def web_socket_do_extra_handshake(request): ...@@ -9,27 +9,34 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
# pywebsocket does not mask message by default. We need to build a frame manually to mask it. # pywebsocket does not mask message by default. We need to build a frame
request.connection.write(stream.create_text_frame('First message', mask=False)) # manually to mask it.
request.connection.write(
request.connection.write(stream.create_text_frame('Fragmented ', opcode=common.OPCODE_TEXT, fin=0, mask=False)) stream.create_text_frame('First message', mask=False))
request.connection.write(stream.create_text_frame('message', opcode=common.OPCODE_CONTINUATION, fin=1, mask=False))
request.connection.write(
stream.create_text_frame(
'Fragmented ', opcode=common.OPCODE_TEXT, fin=0, mask=False))
request.connection.write(
stream.create_text_frame(
'message', opcode=common.OPCODE_CONTINUATION, fin=1, mask=False))
request.connection.write(stream.create_text_frame('', mask=False)) request.connection.write(stream.create_text_frame('', mask=False))
msgutil.send_message(request, 'END') msgutil.send_message(request, 'END')
# Wait for the client to start closing handshake. # Wait for the client to start closing handshake. To receive a close frame,
# To receive a close frame, we must use an internal method of request.ws_stream. # we must use an internal method of request.ws_stream.
opcode, payload, final, reserved1, reserved2, reserved3 = request.ws_stream._receive_frame() opcode, payload, final, reserved1, reserved2, reserved3 = \
request.ws_stream._receive_frame()
assert opcode == common.OPCODE_CLOSE assert opcode == common.OPCODE_CLOSE
assert final assert final
assert not reserved1 assert not reserved1
assert not reserved2 assert not reserved2
assert not reserved3 assert not reserved3
# Send a masked close frame. Clients should be able to handle this frame and # Send a masked close frame. Clients should be able to handle this frame
# the WebSocket object should be closed cleanly. # and the WebSocket object should be closed cleanly.
request.connection.write(stream.create_close_frame('', mask=False)) request.connection.write(stream.create_close_frame('', mask=False))
# Prevents pywebsocket from starting its own closing handshake. # Prevents pywebsocket from starting its own closing handshake.
......
...@@ -9,8 +9,10 @@ def web_socket_do_extra_handshake(request): ...@@ -9,8 +9,10 @@ def web_socket_do_extra_handshake(request):
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
messages_to_send = ['Hello, world!', '', all_distinct_bytes()] messages_to_send = ['Hello, world!', '', all_distinct_bytes()]
for message in messages_to_send: for message in messages_to_send:
# FIXME: Should use better API to send binary messages when pywebsocket supports it. # FIXME: Should use better API to send binary messages when pywebsocket
header = stream.create_header(common.OPCODE_BINARY, len(message), 1, 0, 0, 0, 0) # supports it.
header = stream.create_header(common.OPCODE_BINARY,
len(message), 1, 0, 0, 0, 0)
request.connection.write(header + message) request.connection.write(header + message)
......
...@@ -14,7 +14,9 @@ def web_socket_transfer_data(request): ...@@ -14,7 +14,9 @@ def web_socket_transfer_data(request):
if type(message) == str and message == expected_message: if type(message) == str and message == expected_message:
msgutil.send_message(request, 'PASS: Message #%d.' % test_number) msgutil.send_message(request, 'PASS: Message #%d.' % test_number)
else: else:
msgutil.send_message(request, 'FAIL: Message #%d: Received unexpected message: %r' % (test_number, message)) msgutil.send_message(
request, 'FAIL: Message #%d: Received unexpected message: %r' %
(test_number, message))
def all_distinct_bytes(): def all_distinct_bytes():
......
...@@ -27,10 +27,8 @@ ...@@ -27,10 +27,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
_GOODBYE_MESSAGE = 'Goodbye' _GOODBYE_MESSAGE = 'Goodbye'
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import cgi import cgi
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from mod_pywebsocket import msgutil from mod_pywebsocket import msgutil
......
...@@ -12,30 +12,26 @@ def web_socket_transfer_data(request): ...@@ -12,30 +12,26 @@ def web_socket_transfer_data(request):
payload3 = 'should be received, too.' payload3 = 'should be received, too.'
# send '' # send ''
request.connection.write(stream.create_header(common.OPCODE_TEXT, request.connection.write(
0, stream.create_header(common.OPCODE_TEXT, 0, 1, 0, 0, 0, 0))
1, 0, 0, 0, 0))
# send payload1 # send payload1
request.connection.write(stream.create_header(common.OPCODE_TEXT, request.connection.write(
len(payload1), stream.create_header(common.OPCODE_TEXT, len(payload1), 1, 0, 0, 0, 0) +
1, 0, 0, 0, 0) + payload1) payload1)
# send '' + '' # send '' + ''
request.connection.write(stream.create_header(common.OPCODE_TEXT, request.connection.write(
0, stream.create_header(common.OPCODE_TEXT, 0, 0, 0, 0, 0, 0))
0, 0, 0, 0, 0)) request.connection.write(
request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, stream.create_header(common.OPCODE_CONTINUATION, 0, 1, 0, 0, 0, 0))
0,
1, 0, 0, 0, 0))
# send payload2 + '' + payload3 # send payload2 + '' + payload3
request.connection.write(stream.create_header(common.OPCODE_TEXT, request.connection.write(
len(payload2), stream.create_header(common.OPCODE_TEXT, len(payload2), 0, 0, 0, 0, 0) +
0, 0, 0, 0, 0) + payload2) payload2)
request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, request.connection.write(
0, stream.create_header(common.OPCODE_CONTINUATION, 0, 0, 0, 0, 0, 0))
0, 0, 0, 0, 0)) request.connection.write(
request.connection.write(stream.create_header(common.OPCODE_CONTINUATION, stream.create_header(common.OPCODE_CONTINUATION,
len(payload3), len(payload3), 1, 0, 0, 0, 0) + payload3)
1, 0, 0, 0, 0) + payload3)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment