Truly independent web browser
LibWeb: Discard an EventSource's incomplete event at the end of a stream
Shannon Boothf0b0bc179836
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rs b/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rsindex dd15cdc1d41..4d4a8260e1a 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rs@@ -88,6 +88,8 @@ pub(crate) struct EventSource {event_type: RefCell<String>,#[gc(untraced)]data: RefCell<String>,+ #[gc(untraced)]+ last_event_id_buffer: RefCell<String>,with_credentials: Cell<bool>,@@ -205,6 +207,7 @@ impl EventSource {last_event_id: RefCell::new(String::new()),event_type: RefCell::new(String::new()),data: RefCell::new(String::new()),+ last_event_id_buffer: RefCell::new(String::new()),with_credentials: Cell::new(false),ready_state: Cell::new(ReadyState::Connecting),pending_data: RefCell::new(Vec::new()),@@ -332,6 +335,14 @@ impl EventSource {this.pending_data.borrow_mut().clear();this.is_start_of_stream.set(true);this.last_was_cr.set(false);++ // When a stream is parsed, a data buffer, an event type buffer, and a last event ID buffer must be associated with+ // it. They must be initialized to the empty string.+ // AD-HOC: The last event ID buffer starts as the last event ID string, which other engines keep across streams.+ this.data.borrow_mut().clear();+ this.event_type.borrow_mut().clear();+ *this.last_event_id_buffer.borrow_mut() = this.last_event_id.borrow().clone();+// SAFETY: The realm, the response and this are live.unsafe {web_html_event_source_incrementally_read_body(@@ -597,7 +608,7 @@ impl EventSource {// If the field value does not contain U+0000 NULL, then set the last event ID buffer to the field value.// Otherwise, ignore the field.if !value.contains('\0') {- *self.last_event_id.borrow_mut() = value.to_owned();+ *self.last_event_id_buffer.borrow_mut() = value.to_owned();}}// -> If the field name is "retry"@@ -622,6 +633,7 @@ impl EventSource {// 1. Set the last event ID string of the event source to the value of the last event ID buffer. The buffer does not// get reset, so the last event ID string of the event source remains set to this value until the next time it is// set by the server.+ *this.last_event_id.borrow_mut() = this.last_event_id_buffer.borrow().clone();let last_event_id = this.last_event_id.borrow();// 2. If the data buffer is an empty string, set the data buffer and the event type buffer to the empty string and return.diff --git a/Tests/LibWeb/Text/expected/HTML/EventSource-incomplete-event-discarded-at-end-of-stream.txt b/Tests/LibWeb/Text/expected/HTML/EventSource-incomplete-event-discarded-at-end-of-stream.txtnew file mode 100644index 00000000000..b758e96245c--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/EventSource-incomplete-event-discarded-at-end-of-stream.txt@@ -0,0 +1,2 @@+message: "x"+message: "x"diff --git a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.txt b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.txtindex 621d4a64d3c..306173c1375 100644--- a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail EventSource: a data before final empty line\ No newline at end of file+1 Pass+Pass EventSource: a data before final empty line\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.worker.txtindex 621d4a64d3c..306173c1375 100644--- a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-data-before-final-empty-line.any.worker.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail EventSource: a data before final empty line\ No newline at end of file+1 Pass+Pass EventSource: a data before final empty line\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/EventSource-incomplete-event-discarded-at-end-of-stream.html b/Tests/LibWeb/Text/input/HTML/EventSource-incomplete-event-discarded-at-end-of-stream.htmlnew file mode 100644index 00000000000..db66318c14c--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/EventSource-incomplete-event-discarded-at-end-of-stream.html@@ -0,0 +1,27 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const server = httpTestServer();++ // Each stream ends in the middle of an event, which the next stream must not continue.+ const url = await server.createEcho("GET", "/event-source-incomplete-event", {+ status: 200,+ headers: { "Content-Type": "text/event-stream", "Access-Control-Allow-Origin": "*" },+ body: "retry: 10\ndata: x\n\nevent: custom\ndata: partial\n",+ });++ const source = new EventSource(url);+ await new Promise(resolve => {+ let count = 0;+ const record = event => {+ println(`${event.type}: ${JSON.stringify(event.data)}`);+ if (++count === 2)+ resolve();+ };+ source.addEventListener("message", record);+ source.addEventListener("custom", record);+ });+ source.close();+ });+</script>