Commit e4b44e77 authored by Keita Suzuki's avatar Keita Suzuki Committed by Commit Bot

Update net/data websocket handlers to python 3 compatible

This commit updates websocket handlers in net/data/websocket to support
python 3 compatibility.

Change-Id: I2f2bae4938c8abdfe2964e4973aecef6776d58d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2051903Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarAdam Rice <ricea@chromium.org>
Commit-Queue: Keita Suzuki <suzukikeita@google.com>
Cr-Commit-Position: refs/heads/master@{#740970}
parent a6e81f6d
...@@ -16,9 +16,9 @@ def web_socket_transfer_data(request): ...@@ -16,9 +16,9 @@ def web_socket_transfer_data(request):
if line is None: if line is None:
return return
if line == '-': if line == '-':
data = '' data = b''
elif line == '--': elif line == '--':
data = 'X' data = b'X'
else: else:
code, reason = line.split(' ', 1) code, reason = line.split(' ', 1)
data = struct.pack('!H', int(code)) + reason.encode('utf-8') data = struct.pack('!H', int(code)) + reason.encode('utf-8')
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# 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 cgi from six.moves.urllib import parse
import threading import threading
...@@ -16,7 +16,7 @@ def get_role(request): ...@@ -16,7 +16,7 @@ def get_role(request):
query = request.ws_resource.split('?', 1) query = request.ws_resource.split('?', 1)
if len(query) == 1: if len(query) == 1:
raise LookupError('No query string found in URL') raise LookupError('No query string found in URL')
param = cgi.parse_qs(query[1]) param = parse.parse_qs(query[1])
if 'role' not in param: if 'role' not in param:
raise LookupError('No role parameter found in the query string') raise LookupError('No role parameter found in the query string')
return param['role'][0] return param['role'][0]
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# 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 six
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
...@@ -14,7 +16,7 @@ def web_socket_transfer_data(request): ...@@ -14,7 +16,7 @@ def web_socket_transfer_data(request):
line = request.ws_stream.receive_message() line = request.ws_stream.receive_message()
if line is None: if line is None:
return return
if isinstance(line, unicode): if isinstance(line, six.text_type):
request.ws_stream.send_message(line, binary=False) request.ws_stream.send_message(line, binary=False)
if line == _GOODBYE_MESSAGE: if line == _GOODBYE_MESSAGE:
return return
......
...@@ -6,15 +6,20 @@ ...@@ -6,15 +6,20 @@
# back to the client. In |headers_in|, the keys are converted to lower-case, # back to the client. In |headers_in|, the keys are converted to lower-case,
# while the original case is retained for the values. # while the original case is retained for the values.
import json import json
from mod_pywebsocket import msgutil
def web_socket_do_extra_handshake(request): def web_socket_do_extra_handshake(request):
pass pass
def web_socket_transfer_data(request): def web_socket_transfer_data(request):
request.ws_stream.send_message(json.dumps(dict(request.headers_in.items()))) # Since python 3 does not lowercase the dictionary key, manually lower all
# Wait for closing handshake # keys to maintain python 2/3 compatibility
request.ws_stream.receive_message() lowered_dict = {
header.lower(): value for header, value in request.headers_in.items()
}
msgutil.send_message(request, json.dumps(lowered_dict))
# Wait for closing handshake
request.ws_stream.receive_message()
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# 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 six
_GOODBYE_MESSAGE = u'Goodbye' _GOODBYE_MESSAGE = u'Goodbye'
...@@ -14,7 +16,7 @@ def web_socket_transfer_data(request): ...@@ -14,7 +16,7 @@ def web_socket_transfer_data(request):
line = request.ws_stream.receive_message() line = request.ws_stream.receive_message()
if line is None: if line is None:
return return
if isinstance(line, unicode): if isinstance(line, six.text_type):
request.ws_stream.send_message(line, binary=False) request.ws_stream.send_message(line, binary=False)
if line == _GOODBYE_MESSAGE: if line == _GOODBYE_MESSAGE:
return return
......
...@@ -11,14 +11,15 @@ from mod_pywebsocket.handshake.hybi import compute_accept ...@@ -11,14 +11,15 @@ from mod_pywebsocket.handshake.hybi import compute_accept
def web_socket_do_extra_handshake(request): def web_socket_do_extra_handshake(request):
accept = compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] accept = compute_accept(
message = ('HTTP/1.1 101 Switching Protocols\r\n' request.headers_in['Sec-WebSocket-Key'].encode('UTF-8'))[0]
'Upgrade: websocket\r\n' message = (b'HTTP/1.1 101 Switching Protocols\r\n'
'Connection: Upgrade\r\n' b'Upgrade: websocket\r\n'
'Sec-WebSocket-Accept: %s\r\n' b'Connection: Upgrade\r\n'
'Sec-WebSocket-Extensions: permessage-deflate;\r\n' b'Sec-WebSocket-Accept: %s\r\n'
' server_max_window_bits=10\r\n' b'Sec-WebSocket-Extensions: permessage-deflate;\r\n'
'\r\n' % accept) b' server_max_window_bits=10\r\n'
b'\r\n' % accept)
request.connection.write(message) request.connection.write(message)
# Prevent pywebsocket from sending its own handshake message. # Prevent pywebsocket from sending its own handshake message.
raise handshake.AbortedByUserException('Close the connection') raise handshake.AbortedByUserException('Close the connection')
......
...@@ -11,13 +11,14 @@ from mod_pywebsocket.handshake.hybi import compute_accept ...@@ -11,13 +11,14 @@ from mod_pywebsocket.handshake.hybi import compute_accept
def web_socket_do_extra_handshake(request): def web_socket_do_extra_handshake(request):
accept = compute_accept(request.headers_in['Sec-WebSocket-Key'])[0] accept = compute_accept(
message = ('HTTP/1.1 101 Switching Protocols\r\n' request.headers_in['Sec-WebSocket-Key'].encode('UTF-8'))[0]
'Upgrade: websocket\r\n' message = (b'HTTP/1.1 101 Switching Protocols\r\n'
'Connection: Upgrade\r\n' b'Upgrade: websocket\r\n'
'Sec-WebSocket-Accept: %s\r\n' b'Connection: Upgrade\r\n'
'Sec-WebSocket-Protocol: sip \r\n' b'Sec-WebSocket-Accept: %s\r\n'
'\r\n' % accept) b'Sec-WebSocket-Protocol: sip \r\n'
b'\r\n' % accept)
request.connection.write(message) request.connection.write(message)
# Prevent pywebsocket from sending its own handshake message. # Prevent pywebsocket from sending its own handshake message.
raise handshake.AbortedByUserException('Close the connection') raise handshake.AbortedByUserException('Close the connection')
......
...@@ -6,10 +6,10 @@ from mod_pywebsocket import handshake ...@@ -6,10 +6,10 @@ from mod_pywebsocket import handshake
def web_socket_do_extra_handshake(request): def web_socket_do_extra_handshake(request):
msg = ('HTTP/1.1 101 Switching Protocols\r\n' msg = (b'HTTP/1.1 101 Switching Protocols\r\n'
'Upgrade: websocket\r\n' b'Upgrade: websocket\r\n'
'Connection: Upgrade\r\n' b'Connection: Upgrade\r\n'
'Sec-WebSocket-Accept: 3rfd') b'Sec-WebSocket-Accept: 3rfd')
request.connection.write(msg) request.connection.write(msg)
# Prevent pywebsocket from sending its own handshake message. # Prevent pywebsocket from sending its own handshake message.
raise handshake.AbortedByUserException('Close the connection') raise handshake.AbortedByUserException('Close the connection')
......
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