Title: [135587] trunk
Revision
135587
Author
[email protected]
Date
2012-11-23 03:52:11 -0800 (Fri, 23 Nov 2012)

Log Message

WebSocket's MessageEvent.origin attribute is an empty string
https://bugs.webkit.org/show_bug.cgi?id=102889

Patch by Christophe Dumez <[email protected]> on 2012-11-23
Reviewed by Kentaro Hara.

Source/WebCore:

Fix WebSocket implementation so that the message event's origin
attribute is properly set to the Unicode serialization of the
origin of the URL that was passed to the WebSocket object's
constructor, instead of an empty string.

The new behavior is according to the specification:
http://www.whatwg.org/specs/web-apps/current-work/multipage/network.html#feedback-from-the-protocol

It also matches the behavior of Firefox.

Tests: http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html
       http/tests/websocket/tests/hybi/send-onmessage-origin.html

* Modules/websockets/WebSocket.cpp:
(WebCore::WebSocket::didReceiveMessage):
(WebCore::WebSocket::didReceiveBinaryData):
* dom/MessageEvent.cpp:
(WebCore::MessageEvent::MessageEvent):
* dom/MessageEvent.h:
(WebCore::MessageEvent::create):
(MessageEvent):

LayoutTests:

Add layout tests to check that the message event's origin attribute
is set to the Unicode serialization of the origin of the URL that
was passed to the WebSocket object's constructor, as per the
WebSocket specification.

* http/tests/websocket/tests/hybi/send-blob-onmessage-origin-expected.txt: Added.
* http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html: Added.
* http/tests/websocket/tests/hybi/send-onmessage-origin-expected.txt: Added.
* http/tests/websocket/tests/hybi/send-onmessage-origin.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (135586 => 135587)


--- trunk/LayoutTests/ChangeLog	2012-11-23 11:31:07 UTC (rev 135586)
+++ trunk/LayoutTests/ChangeLog	2012-11-23 11:52:11 UTC (rev 135587)
@@ -1,3 +1,20 @@
+2012-11-23  Christophe Dumez  <[email protected]>
+
+        WebSocket's MessageEvent.origin attribute is an empty string
+        https://bugs.webkit.org/show_bug.cgi?id=102889
+
+        Reviewed by Kentaro Hara.
+
+        Add layout tests to check that the message event's origin attribute
+        is set to the Unicode serialization of the origin of the URL that
+        was passed to the WebSocket object's constructor, as per the
+        WebSocket specification.
+
+        * http/tests/websocket/tests/hybi/send-blob-onmessage-origin-expected.txt: Added.
+        * http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html: Added.
+        * http/tests/websocket/tests/hybi/send-onmessage-origin-expected.txt: Added.
+        * http/tests/websocket/tests/hybi/send-onmessage-origin.html: Added.
+
 2012-11-22  Eugene Klyuchnikov  <[email protected]>
 
         Web Inspector: No "Save Heap Snapshot" option after canceling save to file dialog.

Added: trunk/LayoutTests/http/tests/websocket/tests/hybi/send-blob-onmessage-origin-expected.txt (0 => 135587)


--- trunk/LayoutTests/http/tests/websocket/tests/hybi/send-blob-onmessage-origin-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/websocket/tests/hybi/send-blob-onmessage-origin-expected.txt	2012-11-23 11:52:11 UTC (rev 135587)
@@ -0,0 +1,16 @@
+Web Socket message event origin attribute test (Blob message)
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS PASS: Message #0.
+PASS origin is "ws://127.0.0.1:8880"
+PASS PASS: Message #1.
+PASS origin is "ws://127.0.0.1:8880"
+PASS PASS: Message #2.
+PASS origin is "ws://127.0.0.1:8880"
+PASS closeEvent.wasClean is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html (0 => 135587)


--- trunk/LayoutTests/http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html	2012-11-23 11:52:11 UTC (rev 135587)
@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script>
+description("Web Socket message event origin attribute test (Blob message)");
+
+window.jsTestIsAsync = true;
+var data = ""
+var origin = "";
+
+function startsWith(target, prefix)
+{
+    return target.indexOf(prefix) === 0;
+}
+
+function createBlobContainingHelloWorld()
+{
+    return new Blob(["Hello, world!"]);
+}
+
+function createEmptyBlob()
+{
+    return new Blob([]);
+}
+
+function createBlobContainingAllDistinctBytes()
+{
+    var array = new Uint8Array(256);
+    for (var i = 0; i < 256; ++i)
+        array[i] = i;
+    return new Blob([array]);
+}
+
+var url = ""
+var ws = new WebSocket(url);
+var closeEvent;
+
+ws._onopen_ = function()
+{
+    ws.send(createBlobContainingHelloWorld());
+    ws.send(createEmptyBlob());
+    ws.send(createBlobContainingAllDistinctBytes());
+};
+
+ws._onmessage_ = function(event)
+{
+    var message = event.data;
+    if (startsWith(message, "PASS"))
+        testPassed(message);
+    else
+        testFailed(message);
+    origin = event.origin;
+    shouldBeEqualToString("origin", "ws://127.0.0.1:8880");
+};
+
+ws._onclose_ = function(event)
+{
+    closeEvent = event;
+    shouldBeTrue("closeEvent.wasClean");
+    finishJSTest();
+};
+
+</script>
+<script src=""
+</body>
+</html>

Added: trunk/LayoutTests/http/tests/websocket/tests/hybi/send-onmessage-origin-expected.txt (0 => 135587)


--- trunk/LayoutTests/http/tests/websocket/tests/hybi/send-onmessage-origin-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/websocket/tests/hybi/send-onmessage-origin-expected.txt	2012-11-23 11:52:11 UTC (rev 135587)
@@ -0,0 +1,15 @@
+Web Socket message event origin attribute test (String message)
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Connected.
+PASS data is firstMessageToSend
+PASS origin is "ws://localhost:8880"
+PASS data is secondMessageToSend
+PASS origin is "ws://localhost:8880"
+Closed.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/http/tests/websocket/tests/hybi/send-onmessage-origin.html (0 => 135587)


--- trunk/LayoutTests/http/tests/websocket/tests/hybi/send-onmessage-origin.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/websocket/tests/hybi/send-onmessage-origin.html	2012-11-23 11:52:11 UTC (rev 135587)
@@ -0,0 +1,66 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script>
+description("Web Socket message event origin attribute test (String message)");
+
+window.jsTestIsAsync = true;
+
+function endTest()
+{
+    clearTimeout(timeoutID);
+    finishJSTest();
+}
+
+var ws = new WebSocket("ws://localhost:8880/websocket/tests/hybi/send");
+
+var firstMessageToSend = "This is the first message to send to the server.";
+var secondMessageToSend = "This is the second.";
+// needs to be global to be accessbile from shouldBe().
+var data = ""
+var origin = "";
+
+ws._onopen_ = function()
+{
+    debug("Connected.");
+    ws.send(firstMessageToSend);
+};
+
+ws._onmessage_ = function(messageEvent)
+{
+    // The server should echo back the first message.
+    data = ""
+    shouldBe("data", "firstMessageToSend");
+    origin = messageEvent.origin;
+    shouldBeEqualToString("origin", "ws://localhost:8880");
+    ws._onmessage_ = function(messageEvent) {
+        // The server should echo back the second message.
+        data = ""
+        shouldBe("data", "secondMessageToSend");
+        origin = messageEvent.origin;
+        shouldBeEqualToString("origin", "ws://localhost:8880");
+    };
+    ws.send(secondMessageToSend);
+};
+
+ws._onclose_ = function()
+{
+    debug("Closed.");
+    endTest();
+};
+
+function timeOutCallback()
+{
+    testFailed("Timed out in state: " + ws.readyState);
+    endTest();
+}
+
+var timeoutID = setTimeout(timeOutCallback, 3000);
+
+</script>
+<script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (135586 => 135587)


--- trunk/Source/WebCore/ChangeLog	2012-11-23 11:31:07 UTC (rev 135586)
+++ trunk/Source/WebCore/ChangeLog	2012-11-23 11:52:11 UTC (rev 135587)
@@ -1,3 +1,32 @@
+2012-11-23  Christophe Dumez  <[email protected]>
+
+        WebSocket's MessageEvent.origin attribute is an empty string
+        https://bugs.webkit.org/show_bug.cgi?id=102889
+
+        Reviewed by Kentaro Hara.
+
+        Fix WebSocket implementation so that the message event's origin
+        attribute is properly set to the Unicode serialization of the
+        origin of the URL that was passed to the WebSocket object's
+        constructor, instead of an empty string.
+
+        The new behavior is according to the specification:
+        http://www.whatwg.org/specs/web-apps/current-work/multipage/network.html#feedback-from-the-protocol
+
+        It also matches the behavior of Firefox.
+
+        Tests: http/tests/websocket/tests/hybi/send-blob-onmessage-origin.html
+               http/tests/websocket/tests/hybi/send-onmessage-origin.html
+
+        * Modules/websockets/WebSocket.cpp:
+        (WebCore::WebSocket::didReceiveMessage):
+        (WebCore::WebSocket::didReceiveBinaryData):
+        * dom/MessageEvent.cpp:
+        (WebCore::MessageEvent::MessageEvent):
+        * dom/MessageEvent.h:
+        (WebCore::MessageEvent::create):
+        (MessageEvent):
+
 2012-11-22  Eugene Klyuchnikov  <[email protected]>
 
         Web Inspector: No "Save Heap Snapshot" option after canceling save to file dialog.

Modified: trunk/Source/WebCore/Modules/websockets/WebSocket.cpp (135586 => 135587)


--- trunk/Source/WebCore/Modules/websockets/WebSocket.cpp	2012-11-23 11:31:07 UTC (rev 135586)
+++ trunk/Source/WebCore/Modules/websockets/WebSocket.cpp	2012-11-23 11:52:11 UTC (rev 135587)
@@ -508,7 +508,7 @@
     if (m_state != OPEN && m_state != CLOSING)
         return;
     ASSERT(scriptExecutionContext());
-    dispatchEvent(MessageEvent::create(msg));
+    dispatchEvent(MessageEvent::create(msg, SecurityOrigin::create(m_url)->toString()));
 }
 
 void WebSocket::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData)
@@ -521,12 +521,12 @@
         OwnPtr<BlobData> blobData = BlobData::create();
         blobData->appendData(rawData.release(), 0, BlobDataItem::toEndOfFile);
         RefPtr<Blob> blob = Blob::create(blobData.release(), size);
-        dispatchEvent(MessageEvent::create(blob.release()));
+        dispatchEvent(MessageEvent::create(blob.release(), SecurityOrigin::create(m_url)->toString()));
         break;
     }
 
     case BinaryTypeArrayBuffer:
-        dispatchEvent(MessageEvent::create(ArrayBuffer::create(binaryData->data(), binaryData->size())));
+        dispatchEvent(MessageEvent::create(ArrayBuffer::create(binaryData->data(), binaryData->size()), SecurityOrigin::create(m_url)->toString()));
         break;
     }
 }

Modified: trunk/Source/WebCore/dom/MessageEvent.cpp (135586 => 135587)


--- trunk/Source/WebCore/dom/MessageEvent.cpp	2012-11-23 11:31:07 UTC (rev 135586)
+++ trunk/Source/WebCore/dom/MessageEvent.cpp	2012-11-23 11:52:11 UTC (rev 135587)
@@ -79,29 +79,29 @@
 #endif
 }
 
-MessageEvent::MessageEvent(const String& data)
+MessageEvent::MessageEvent(const String& data, const String& origin)
     : Event(eventNames().messageEvent, false, false)
     , m_dataType(DataTypeString)
     , m_dataAsString(data)
-    , m_origin("")
+    , m_origin(origin)
     , m_lastEventId("")
 {
 }
 
-MessageEvent::MessageEvent(PassRefPtr<Blob> data)
+MessageEvent::MessageEvent(PassRefPtr<Blob> data, const String& origin)
     : Event(eventNames().messageEvent, false, false)
     , m_dataType(DataTypeBlob)
     , m_dataAsBlob(data)
-    , m_origin("")
+    , m_origin(origin)
     , m_lastEventId("")
 {
 }
 
-MessageEvent::MessageEvent(PassRefPtr<ArrayBuffer> data)
+MessageEvent::MessageEvent(PassRefPtr<ArrayBuffer> data, const String& origin)
     : Event(eventNames().messageEvent, false, false)
     , m_dataType(DataTypeArrayBuffer)
     , m_dataAsArrayBuffer(data)
-    , m_origin("")
+    , m_origin(origin)
     , m_lastEventId("")
 {
 }

Modified: trunk/Source/WebCore/dom/MessageEvent.h (135586 => 135587)


--- trunk/Source/WebCore/dom/MessageEvent.h	2012-11-23 11:31:07 UTC (rev 135586)
+++ trunk/Source/WebCore/dom/MessageEvent.h	2012-11-23 11:52:11 UTC (rev 135587)
@@ -64,17 +64,17 @@
     {
         return adoptRef(new MessageEvent(data, origin, lastEventId, source, ports));
     }
-    static PassRefPtr<MessageEvent> create(const String& data)
+    static PassRefPtr<MessageEvent> create(const String& data, const String& origin = "")
     {
-        return adoptRef(new MessageEvent(data));
+        return adoptRef(new MessageEvent(data, origin));
     }
-    static PassRefPtr<MessageEvent> create(PassRefPtr<Blob> data)
+    static PassRefPtr<MessageEvent> create(PassRefPtr<Blob> data, const String& origin = "")
     {
-        return adoptRef(new MessageEvent(data));
+        return adoptRef(new MessageEvent(data, origin));
     }
-    static PassRefPtr<MessageEvent> create(PassRefPtr<ArrayBuffer> data)
+    static PassRefPtr<MessageEvent> create(PassRefPtr<ArrayBuffer> data, const String& origin = "")
     {
-        return adoptRef(new MessageEvent(data));
+        return adoptRef(new MessageEvent(data, origin));
     }
     static PassRefPtr<MessageEvent> create(const AtomicString& type, const MessageEventInit& initializer)
     {
@@ -119,9 +119,9 @@
     MessageEvent(const ScriptValue& data, const String& origin, const String& lastEventId, PassRefPtr<DOMWindow> source, PassOwnPtr<MessagePortArray>);
     MessageEvent(PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, PassRefPtr<DOMWindow> source, PassOwnPtr<MessagePortArray>);
 
-    explicit MessageEvent(const String& data);
-    explicit MessageEvent(PassRefPtr<Blob> data);
-    explicit MessageEvent(PassRefPtr<ArrayBuffer> data);
+    explicit MessageEvent(const String& data, const String& origin);
+    explicit MessageEvent(PassRefPtr<Blob> data, const String& origin);
+    explicit MessageEvent(PassRefPtr<ArrayBuffer> data, const String& origin);
 
     DataType m_dataType;
     ScriptValue m_dataAsScriptValue;
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to