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