Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[WebSocket] CloseEvent's code and reason properties support.
https://bugs.webkit.org/show_bug.cgi?id=66362

Patch by Takashi Toyoshima <toyoshim@chromium.org> on 2011-08-19
Reviewed by Kent Tamura.

Current WebSocket implementation miss code and reason properties
in CloseEvent. This change expose incoming closing frame's code
and reason to JavaScript API.

Source/WebCore:

Tests: http/tests/websocket/tests/hybi/close-code-and-reason.html
       http/tests/websocket/tests/hybi/workers/close-code-and-reason.html

* websockets/CloseEvent.h:
(WebCore::CloseEvent::initCloseEvent):
(WebCore::CloseEvent::code):
(WebCore::CloseEvent::reason):
(WebCore::CloseEvent::CloseEvent):
* websockets/CloseEvent.idl:
Add code and reason properies.
* websockets/ThreadableWebSocketChannelClientWrapper.cpp:
(WebCore::ThreadableWebSocketChannelClientWrapper::didClose):
(WebCore::ThreadableWebSocketChannelClientWrapper::didCloseCallback):
* websockets/ThreadableWebSocketChannelClientWrapper.h:
* websockets/WebSocket.cpp:
(WebCore::WebSocket::didConnect):
(WebCore::WebSocket::didClose):
* websockets/WebSocket.h:
Add implement to handle code and reason.
* websockets/WebSocketChannel.cpp:
(WebCore::WebSocketChannel::WebSocketChannel):
(WebCore::WebSocketChannel::didCloseSocketStream):
(WebCore::WebSocketChannel::processFrame):
* websockets/WebSocketChannel.h:
Add closing frame payload parser.
* websockets/WebSocketChannelClient.h:
(WebCore::WebSocketChannelClient::didClose):
* websockets/WorkerThreadableWebSocketChannel.cpp:
(WebCore::workerContextDidClose):
(WebCore::WorkerThreadableWebSocketChannel::Peer::didClose):
* websockets/WorkerThreadableWebSocketChannel.h:
Add implement to handle code and reason.

LayoutTests:

* http/tests/websocket/tests/hybi/close-code-and-reason-expected.txt: Added.
* http/tests/websocket/tests/hybi/close-code-and-reason.html: Added.
* http/tests/websocket/tests/hybi/close-code-and-reason_wsh.py: Added.
* http/tests/websocket/tests/hybi/workers/close-code-and-reason-expected.txt: Added.
* http/tests/websocket/tests/hybi/workers/close-code-and-reason.html: Added.
* http/tests/websocket/tests/hybi/workers/resources/close-code-and-reason.js: Added.
(postResult):
(ws.onopen):
(ws.onmessage):
(ws.onclose):

Canonical link: https://commits.webkit.org/82383@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@93393 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
toyoshim authored and webkit-commit-queue committed Aug 19, 2011
1 parent 99fa89c commit c742cfe
Show file tree
Hide file tree
Showing 19 changed files with 436 additions and 23 deletions.
22 changes: 22 additions & 0 deletions LayoutTests/ChangeLog
@@ -1,3 +1,25 @@
2011-08-19 Takashi Toyoshima <toyoshim@chromium.org>

[WebSocket] CloseEvent's code and reason properties support.
https://bugs.webkit.org/show_bug.cgi?id=66362

Reviewed by Kent Tamura.

Current WebSocket implementation miss code and reason properties
in CloseEvent. This change expose incoming closing frame's code
and reason to JavaScript API.

* http/tests/websocket/tests/hybi/close-code-and-reason-expected.txt: Added.
* http/tests/websocket/tests/hybi/close-code-and-reason.html: Added.
* http/tests/websocket/tests/hybi/close-code-and-reason_wsh.py: Added.
* http/tests/websocket/tests/hybi/workers/close-code-and-reason-expected.txt: Added.
* http/tests/websocket/tests/hybi/workers/close-code-and-reason.html: Added.
* http/tests/websocket/tests/hybi/workers/resources/close-code-and-reason.js: Added.
(postResult):
(ws.onopen):
(ws.onmessage):
(ws.onclose):

2011-08-19 MORITA Hajime <morrita@google.com>

Spell-checking doesn't recognize word boundaries on contests inserted by execCommand('insertHTML')
Expand Down
@@ -0,0 +1,34 @@
CONSOLE MESSAGE: line 0: WebSocket is closed before the connection is established.
Test CloseEvent code and reason property.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".

ws.onclose() was called.
PASS closeEvent.wasClean is false
PASS closeEvent.code is codeAbnormalClosure
PASS closeEvent.reason is emptyString
WebSocketTest.onopen() was called with testId = 0.
WebSocketTest.onclose() was called with testId = 0.
PASS closeEvent.wasClean is true
PASS closeEvent.code is expectedCode[testId]
PASS closeEvent.reason is expectedReason[testId]
PASS closeEvent.code is expectedCode[testId]
PASS closeEvent.reason is expectedReason[testId]
WebSocketTest.onopen() was called with testId = 1.
WebSocketTest.onclose() was called with testId = 1.
PASS closeEvent.wasClean is true
PASS closeEvent.code is expectedCode[testId]
PASS closeEvent.reason is expectedReason[testId]
PASS closeEvent.code is expectedCode[testId]
PASS closeEvent.reason is expectedReason[testId]
WebSocketTest.onopen() was called with testId = 2.
WebSocketTest.onclose() was called with testId = 2.
PASS closeEvent.wasClean is true
PASS closeEvent.code is expectedCode[testId]
PASS closeEvent.reason is expectedReason[testId]
PASS closeEvent.code is expectedCode[testId]
PASS closeEvent.reason is expectedReason[testId]
PASS successfullyParsed is true

TEST COMPLETE

100 changes: 100 additions & 0 deletions LayoutTests/http/tests/websocket/tests/hybi/close-code-and-reason.html
@@ -0,0 +1,100 @@
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="../../../../js-test-resources/js-test-style.css">
<script src="../../../../js-test-resources/js-test-pre.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script type="text/javascript">
description("Test CloseEvent code and reason property.");

window.jsTestIsAsync = true;
if (window.layoutTestController)
layoutTestController.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);

var closeEvent;
var codeNoStatusRcvd = 1005;
var codeAbnormalClosure = 1006;
var emptyString = "";

var ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hybi/hanging-handshake");

ws.onopen = function(event)
{
testFailed("ws.onopen() was called. (message = \"" + event.data + "\")");
};

ws.onclose = function(event)
{
debug("ws.onclose() was called.");
closeEvent = event;
shouldBeFalse("closeEvent.wasClean");
shouldBe("closeEvent.code", "codeAbnormalClosure");
shouldBe("closeEvent.reason", "emptyString");
};

ws.close();

var testId = 0;
var testNum = 3;
var sendData = [
"-", // request close frame without code and reason
"--", // request close frame with invalid body which size is 1
"65535 good bye", // request close frame with specified code and reason
];
var expectedCode = [
codeNoStatusRcvd,
codeNoStatusRcvd,
65535,
];
var expectedReason = [
"",
"",
"good bye",
];

WebSocketTest = function() {
this.ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hybi/close-code-and-reason");
this.ws.onopen = this.onopen;
this.ws.onmessage = this.onmessage;
this.ws.onclose = this.onclose;
};

WebSocketTest.prototype.onopen = function()
{
debug("WebSocketTest.onopen() was called with testId = " + testId + ".");
this.send(sendData[testId]);
};

WebSocketTest.prototype.onmessage = function(event)
{
testFailed("WebSocketTest.onmessage() was called. (message = \"" + event.data + "\")");
};

WebSocketTest.prototype.onclose = function(event)
{
closeEvent = event;
debug("WebSocketTest.onclose() was called with testId = " + testId + ".");
shouldBeTrue("closeEvent.wasClean");
shouldBe("closeEvent.code", "expectedCode[testId]");
shouldBe("closeEvent.reason", "expectedReason[testId]");
closeEvent.code = 0;
closeEvent.reason = "readonly";
shouldBe("closeEvent.code", "expectedCode[testId]");
shouldBe("closeEvent.reason", "expectedReason[testId]");
testId++;
if (testId < testNum)
test = new WebSocketTest();
else
finishJSTest();
};

var test = new WebSocketTest();

var successfullyParsed = true;
</script>
<script src="../../../../js-test-resources/js-test-post.js"></script>
</body>
</html>
@@ -0,0 +1,19 @@
import struct

from mod_pywebsocket import stream

def web_socket_do_extra_handshake(request):
pass

def web_socket_transfer_data(request):
line = request.ws_stream.receive_message()
if line is None:
return
if line == '-':
data = ''
elif line == '--':
data = 'X'
else:
code, reason = line.split(' ', 1)
data = struct.pack('!H', int(code)) + reason.encode('utf-8')
request.connection.write(stream.create_close_frame(data))
@@ -0,0 +1,35 @@
CONSOLE MESSAGE: line 0: WebSocket is closed before the connection is established.
Test CloseEvent code and reason property in Worker.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".

ws.onclose() was called.
PASS PASS: worker: event.wasClean is false
PASS PASS: worker: event.code is codeAbnormalClosure
PASS PASS: worker: event.reason is emptyString
WebSocketTest.onopen() was called with testId = 0.
WebSocketTest.onclose() was called with testId = 0.
PASS PASS: worker: event.wasClean is true
PASS PASS: worker: event.code is 1005
PASS PASS: worker: event.reason is
PASS PASS: worker: event.code is 1005
PASS PASS: worker: event.reason is
WebSocketTest.onopen() was called with testId = 1.
WebSocketTest.onclose() was called with testId = 1.
PASS PASS: worker: event.wasClean is true
PASS PASS: worker: event.code is 1005
PASS PASS: worker: event.reason is
PASS PASS: worker: event.code is 1005
PASS PASS: worker: event.reason is
WebSocketTest.onopen() was called with testId = 2.
WebSocketTest.onclose() was called with testId = 2.
PASS PASS: worker: event.wasClean is true
PASS PASS: worker: event.code is 65535
PASS PASS: worker: event.reason is good bye
PASS PASS: worker: event.code is 65535
PASS PASS: worker: event.reason is good bye
DONE
PASS successfullyParsed is true

TEST COMPLETE

@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../../../../../js-test-resources/js-test-style.css">
<script src="../../../../../js-test-resources/js-test-pre.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script type="text/javascript">
description("Test CloseEvent code and reason property in Worker.");

window.jsTestIsAsync = true;
if (window.layoutTestController)
layoutTestController.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);

function startsWith(str, prefix)
{
return str.indexOf(prefix) == 0;
}

var worker = new Worker("resources/close-code-and-reason.js");
worker.onmessage = function (event)
{
var message = event.data;
if (startsWith(message, "PASS"))
testPassed(message);
else if (startsWith(message, "FAIL"))
testFailed(message)
else
debug(message);
if (message === "DONE")
finishJSTest();
};

var successfullyParsed = true;
</script>
<script src="../../../../../js-test-resources/js-test-post.js"></script>
</body>
</html>
@@ -0,0 +1,85 @@
var codeNoStatusRcvd = 1005;
var codeAbnormalClosure = 1006;
var emptyString = "";

function postResult(result, actual, expected)
{
var message = result ? "PASS" : "FAIL";
message += ": worker: " + actual + " is ";
if (!result)
message += "not ";
message += expected;
postMessage(message);
}

var ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hybi/hanging-handshake");

ws.onopen = function(event)
{
testFailed("FAIL: ws.onopen() was called. (message = \"" + event.data + "\")");
};

ws.onclose = function(event)
{
postMessage("ws.onclose() was called.");
postResult(!event.wasClean, "event.wasClean", "false");
postResult(event.code == codeAbnormalClosure, "event.code", "codeAbnormalClosure");
postResult(event.reason == emptyString, "event.reason", "emptyString");
};

ws.close();

var testId = 0;
var testNum = 3;
var sendData = [
"-", // request close frame without code and reason
"--", // request close frame with invalid body which size is 1
"65535 good bye", // request close frame with specified code and reason
];
var expectedCode = [
codeNoStatusRcvd,
codeNoStatusRcvd,
65535,
];
var expectedReason = [
"",
"",
"good bye",
];

WebSocketTest = function() {
this.ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hybi/close-code-and-reason");
this.ws.onopen = this.onopen;
this.ws.onmessage = this.onmessage;
this.ws.onclose = this.onclose;
};

WebSocketTest.prototype.onopen = function()
{
postMessage("WebSocketTest.onopen() was called with testId = " + testId + ".");
this.send(sendData[testId]);
};

WebSocketTest.prototype.onmessage = function(event)
{
postMessage("FAIL: WebSocketTest.onmessage() was called. (message = \"" + event.data + "\")");
};

WebSocketTest.prototype.onclose = function(event)
{
postMessage("WebSocketTest.onclose() was called with testId = " + testId + ".");
postResult(event.wasClean, "event.wasClean", "true");
postResult(event.code == expectedCode[testId], "event.code", expectedCode[testId]);
postResult(event.reason == expectedReason[testId], "event.reason", expectedReason[testId]);
event.code = 0;
event.reason = "readonly";
postResult(event.code == expectedCode[testId], "event.code", expectedCode[testId]);
postResult(event.reason == expectedReason[testId], "event.reason", expectedReason[testId]);
testId++;
if (testId < testNum)
test = new WebSocketTest();
else
postMessage("DONE")
};

var test = new WebSocketTest();
44 changes: 44 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,47 @@
2011-08-19 Takashi Toyoshima <toyoshim@chromium.org>

[WebSocket] CloseEvent's code and reason properties support.
https://bugs.webkit.org/show_bug.cgi?id=66362

Reviewed by Kent Tamura.

Current WebSocket implementation miss code and reason properties
in CloseEvent. This change expose incoming closing frame's code
and reason to JavaScript API.

Tests: http/tests/websocket/tests/hybi/close-code-and-reason.html
http/tests/websocket/tests/hybi/workers/close-code-and-reason.html

* websockets/CloseEvent.h:
(WebCore::CloseEvent::initCloseEvent):
(WebCore::CloseEvent::code):
(WebCore::CloseEvent::reason):
(WebCore::CloseEvent::CloseEvent):
* websockets/CloseEvent.idl:
Add code and reason properies.
* websockets/ThreadableWebSocketChannelClientWrapper.cpp:
(WebCore::ThreadableWebSocketChannelClientWrapper::didClose):
(WebCore::ThreadableWebSocketChannelClientWrapper::didCloseCallback):
* websockets/ThreadableWebSocketChannelClientWrapper.h:
* websockets/WebSocket.cpp:
(WebCore::WebSocket::didConnect):
(WebCore::WebSocket::didClose):
* websockets/WebSocket.h:
Add implement to handle code and reason.
* websockets/WebSocketChannel.cpp:
(WebCore::WebSocketChannel::WebSocketChannel):
(WebCore::WebSocketChannel::didCloseSocketStream):
(WebCore::WebSocketChannel::processFrame):
* websockets/WebSocketChannel.h:
Add closing frame payload parser.
* websockets/WebSocketChannelClient.h:
(WebCore::WebSocketChannelClient::didClose):
* websockets/WorkerThreadableWebSocketChannel.cpp:
(WebCore::workerContextDidClose):
(WebCore::WorkerThreadableWebSocketChannel::Peer::didClose):
* websockets/WorkerThreadableWebSocketChannel.h:
Add implement to handle code and reason.

2011-08-19 MORITA Hajime <morrita@google.com>

Spell-checking doesn't recognize word boundaries on contests inserted by execCommand('insertHTML')
Expand Down

0 comments on commit c742cfe

Please sign in to comment.