Truly independent web browser
LibWeb: Strip a byte order mark only at the start of an event stream
Shannon Booth52892e3df5df
diff --git a/Libraries/LibWeb/HTML/EventSource.cpp b/Libraries/LibWeb/HTML/EventSource.cppindex d2073bfa20f..1ba9f18d5a2 100644--- a/Libraries/LibWeb/HTML/EventSource.cpp+++ b/Libraries/LibWeb/HTML/EventSource.cpp@@ -157,7 +157,7 @@ WebIDL::ExceptionOr<GC::Ref<EventSource>> EventSource::create(WindowOrWorkerGlobelse {event_source->announce_the_connection();- auto process_body_chunk = GC::create_function(GC::Heap::the(), [event_source, pending_data = ByteBuffer()](ByteBuffer body) mutable {+ auto process_body_chunk = GC::create_function(GC::Heap::the(), [event_source, pending_data = ByteBuffer(), is_start_of_stream = true](ByteBuffer body) mutable {if (pending_data.is_empty())pending_data = move(body);else@@ -168,7 +168,8 @@ WebIDL::ExceptionOr<GC::Ref<EventSource>> EventSource::create(WindowOrWorkerGlobreturn;auto end_index = *last_line_break + 1;- event_source->interpret_response(pending_data.bytes().slice(0, end_index));+ event_source->interpret_response(pending_data.bytes().slice(0, end_index), is_start_of_stream);+ is_start_of_stream = false;pending_data = MUST(pending_data.slice(end_index, pending_data.size() - end_index));});@@ -379,9 +380,10 @@ void EventSource::fail_the_connection()}// https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation-void EventSource::interpret_response(ReadonlyBytes response)+void EventSource::interpret_response(ReadonlyBytes response, bool is_start_of_stream){- auto response_string = Utf16String::from_utf8_with_replacement_character(response);+ // Streams must be decoded using the UTF-8 decode algorithm.+ auto response_string = Utf16String::from_utf8_with_replacement_character(response, is_start_of_stream ? Utf16String::WithBOMHandling::Yes : Utf16String::WithBOMHandling::No);// Lines must be processed, in the order they are received, as follows:for_each_line(response_string.utf16_view(), [&](auto line) {diff --git a/Libraries/LibWeb/HTML/EventSource.h b/Libraries/LibWeb/HTML/EventSource.hindex 0360720e3a7..871c9ce287d 100644--- a/Libraries/LibWeb/HTML/EventSource.h+++ b/Libraries/LibWeb/HTML/EventSource.h@@ -78,7 +78,7 @@ private:void reestablish_the_connection();void fail_the_connection();- void interpret_response(ReadonlyBytes);+ void interpret_response(ReadonlyBytes, bool is_start_of_stream);void process_field(Utf16View field, Utf16View value);void dispatch_the_event();JS::Object& relevant_global_object() const;diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 5edbddf3ce2..5b1a09d1cbe 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -258,6 +258,8 @@ Text/input/wpt-import/html/semantics/forms/the-input-element/show-picker-disableText/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/import-css-module-dynamic.htmlText/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic.htmlText/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html+Text/input/wpt-import/eventsource/format-bom-2.any.html+Text/input/wpt-import/eventsource/format-bom.any.htmlText/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/module.htmlText/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.htmlText/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.htmldiff --git a/Tests/LibWeb/Text/expected/HTML/EventSource-byte-order-mark-only-at-stream-start.txt b/Tests/LibWeb/Text/expected/HTML/EventSource-byte-order-mark-only-at-stream-start.txtnew file mode 100644index 00000000000..86548d92f2c--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/EventSource-byte-order-mark-only-at-stream-start.txt@@ -0,0 +1,2 @@+message: one+message: threediff --git a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-bom-2.any.txt b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-bom-2.any.txtnew file mode 100644index 00000000000..498034b4a2a--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-bom-2.any.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass EventSource: Double BOM\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-bom.any.txt b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-bom.any.txtnew file mode 100644index 00000000000..bfe390e5fa9--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-bom.any.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass EventSource: BOM\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/EventSource-byte-order-mark-only-at-stream-start.html b/Tests/LibWeb/Text/input/HTML/EventSource-byte-order-mark-only-at-stream-start.htmlnew file mode 100644index 00000000000..184fe8fd424--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/EventSource-byte-order-mark-only-at-stream-start.html@@ -0,0 +1,28 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const server = httpTestServer();+ const token = `event-source-byte-order-mark-${crypto.randomUUID()}`;++ // The second chunk starts with a byte order mark, which makes its first field "data".+ const firstChunk = "data: one\n\n";+ const url = await server.createEcho("GET", "/event-source-byte-order-mark", {+ status: 200,+ headers: { "Content-Type": "text/event-stream", "Access-Control-Allow-Origin": "*" },+ body: `${firstChunk}data: two\n\ndata: three\n\n`,+ });++ const source = new EventSource(`${url}?chunks=${firstChunk.length}&chunk_unblock_tokens=${token}`);+ await new Promise(resolve => {+ source.onmessage = event => {+ println(`message: ${event.data}`);+ if (event.data === "one")+ fetch(`${server.baseURL}/unblock/${token}`);+ if (event.data === "three")+ resolve();+ };+ });+ source.close();+ });+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom-2.any.html b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom-2.any.htmlnew file mode 100644index 00000000000..4a2bdf4f290--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom-2.any.html@@ -0,0 +1,15 @@+<!doctype html>+<meta charset=utf-8>+<title>EventSource: Double BOM</title>+<script>+self.GLOBAL = {+ isWindow: function() { return true; },+ isWorker: function() { return false; },+ isShadowRealm: function() { return false; },+};+</script>+<script src="../resources/testharness.js"></script>+<script src="../resources/testharnessreport.js"></script>++<div id=log></div>+<script src="../eventsource/format-bom-2.any.js"></script>diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom-2.any.js b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom-2.any.jsnew file mode 100644index 00000000000..8b7be8402c0--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom-2.any.js@@ -0,0 +1,24 @@+// META: title=EventSource: Double BOM++ var test = async_test(),+ hasbeenone = false,+ hasbeentwo = false+ test.step(function() {+ var source = new EventSource("resources/message.py?message=%EF%BB%BF%EF%BB%BFdata%3A1%0A%0Adata%3A2%0A%0Adata%3A3")+ source.addEventListener("message", listener, false)+ })+ function listener(e) {+ test.step(function() {+ if(e.data == "1")+ hasbeenone = true+ if(e.data == "2")+ hasbeentwo = true+ if(e.data == "3") {+ assert_false(hasbeenone)+ assert_true(hasbeentwo)+ this.close()+ test.done()+ }+ }, this)+ }+diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom.any.html b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom.any.htmlnew file mode 100644index 00000000000..2a96c16249a--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom.any.html@@ -0,0 +1,15 @@+<!doctype html>+<meta charset=utf-8>+<title>EventSource: BOM</title>+<script>+self.GLOBAL = {+ isWindow: function() { return true; },+ isWorker: function() { return false; },+ isShadowRealm: function() { return false; },+};+</script>+<script src="../resources/testharness.js"></script>+<script src="../resources/testharnessreport.js"></script>++<div id=log></div>+<script src="../eventsource/format-bom.any.js"></script>diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom.any.js b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom.any.jsnew file mode 100644index 00000000000..05d1abd18b1--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-bom.any.js@@ -0,0 +1,24 @@+// META: title=EventSource: BOM++ var test = async_test(),+ hasbeenone = false,+ hasbeentwo = false+ test.step(function() {+ var source = new EventSource("resources/message.py?message=%EF%BB%BFdata%3A1%0A%0A%EF%BB%BFdata%3A2%0A%0Adata%3A3")+ source.addEventListener("message", listener, false)+ })+ function listener(e) {+ test.step(function() {+ if(e.data == "1")+ hasbeenone = true+ if(e.data == "2")+ hasbeentwo = true+ if(e.data == "3") {+ assert_true(hasbeenone)+ assert_false(hasbeentwo)+ this.close()+ test.done()+ }+ }, this)+ }+diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/message.py b/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/message.pynew file mode 100644index 00000000000..468564f4df0--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/message.py@@ -0,0 +1,14 @@+import time++def main(request, response):+ mime = request.GET.first(b"mime", b"text/event-stream")+ message = request.GET.first(b"message", b"data: data");+ newline = b"" if request.GET.first(b"newline", None) == b"none" else b"\n\n";+ sleep = int(request.GET.first(b"sleep", b"0"))++ headers = [(b"Content-Type", mime)]+ body = message + newline + b"\n"+ if sleep != 0:+ time.sleep(sleep/1000)++ return headers, body