From 347928b9508df8f8effe92c372bd72873c4ba2aa Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Thu, 15 Aug 2024 19:05:53 -0600 Subject: [PATCH] LibWeb: Fix handling of SSEs split across chunks --- Userland/Libraries/LibWeb/HTML/EventSource.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/EventSource.cpp b/Userland/Libraries/LibWeb/HTML/EventSource.cpp index a0fa53edde7..7f23a267bf1 100644 --- a/Userland/Libraries/LibWeb/HTML/EventSource.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventSource.cpp @@ -130,9 +130,22 @@ WebIDL::ExceptionOr> EventSource::construct_impl(J else { event_source->announce_the_connection(); - auto process_body_chunk = JS::create_heap_function(realm.heap(), [event_source](ByteBuffer body) { - event_source->interpret_response(body); + auto process_body_chunk = JS::create_heap_function(realm.heap(), [event_source, pending_data = ByteBuffer()](ByteBuffer body) mutable { + if (pending_data.is_empty()) + pending_data = move(body); + else + pending_data.append(body); + + auto last_line_break = AK::StringUtils::find_any_of(pending_data, "\r\n"sv, AK::StringUtils::SearchDirection::Backward); + if (!last_line_break.has_value()) + return; + + auto end_index = *last_line_break + 1; + event_source->interpret_response({ pending_data.bytes().slice(0, end_index) }); + + pending_data = MUST(pending_data.slice(end_index, pending_data.size() - end_index)); }); + auto process_end_of_body = JS::create_heap_function(realm.heap(), []() { // This case is handled by `process_event_source_end_of_body` above. });