Truly independent web browser
LibWeb: Drop Last-Event-ID once an EventSource's ID is reset
Shannon Booth759198723e8d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/requests.rs b/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/requests.rsindex 8941b8428dd..a7ea8432a1f 100644--- a/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/requests.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/requests.rs@@ -10,6 +10,7 @@ use core::ptr::NonNull;unsafe extern "C" {fn web_fetch_request_done(request: *mut c_void) -> bool;fn web_fetch_request_set_header(request: *mut c_void, name: ak::Span<'_, u8>, value: ak::Span<'_, u8>);+ fn web_fetch_request_delete_header(request: *mut c_void, name: ak::Span<'_, u8>);fn web_fetch_request_unref(request: *mut c_void);}@@ -39,6 +40,12 @@ impl CppRequest {// SAFETY: This holds a reference to the request, and the name and the value are lent for the call.unsafe { web_fetch_request_set_header(self.0.as_ptr(), ak::Span::new(name), ak::Span::new(value)) };}++ /// https://fetch.spec.whatwg.org/#concept-header-list-delete+ pub(crate) fn delete_header(&self, name: &[u8]) {+ // SAFETY: This holds a reference to the request, and the name is lent for the call.+ unsafe { web_fetch_request_delete_header(self.0.as_ptr(), ak::Span::new(name)) };+ }}impl Drop for CppRequest {diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rs b/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rsindex 2b5f812fa7d..a8d1e5339a2 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/event_source.rs@@ -524,6 +524,10 @@ impl EventSource {// 2. Set (`Last-Event-ID`, lastEventIDValue) in request's header list.request.set_header(b"Last-Event-ID", last_event_id_value);}+ // AD-HOC: Delete the header an earlier reconnection set, as other engines do.+ else {+ request.delete_header(b"Last-Event-ID");+ }drop(last_event_id);// 4. Fetch request and process the response obtained in this fashion, if any, as described earlier in thisdiff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cppindex af34cc78286..2783505fcf7 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp@@ -23,6 +23,7 @@ extern "C" {// Called by Bindings/Rust/src/fetch/infrastructure/http/requests.rs.bool web_fetch_request_done(Request*);void web_fetch_request_set_header(Request*, ReadonlyBytes name, ReadonlyBytes value);+void web_fetch_request_delete_header(Request*, ReadonlyBytes name);void web_fetch_request_unref(Request*);}@@ -659,6 +660,11 @@ void web_fetch_request_set_header(Request* request, ReadonlyBytes name, Readonlyrequest->header_list()->set({ ByteString { name }, ByteString { value } });}+void web_fetch_request_delete_header(Request* request, ReadonlyBytes name)+{+ request->header_list()->delete_(StringView { name });+}+void web_fetch_request_unref(Request* request){request->unref();diff --git a/Tests/LibWeb/Text/expected/HTML/EventSource-last-event-id-header-dropped-after-reset.txt b/Tests/LibWeb/Text/expected/HTML/EventSource-last-event-id-header-dropped-after-reset.txtnew file mode 100644index 00000000000..8029fc645f8--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/EventSource-last-event-id-header-dropped-after-reset.txt@@ -0,0 +1,3 @@+set the ID+reset the ID, Last-Event-ID was 1+Last-Event-ID is nonediff --git a/Tests/LibWeb/Text/input/HTML/EventSource-last-event-id-header-dropped-after-reset.html b/Tests/LibWeb/Text/input/HTML/EventSource-last-event-id-header-dropped-after-reset.htmlnew file mode 100644index 00000000000..66f333969f8--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/EventSource-last-event-id-header-dropped-after-reset.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const server = httpTestServer();+ const url = server.getStaticURL(`Text/input/HTML/EventSource-last-event-id-reset.py?token=${crypto.randomUUID()}`);++ const source = new EventSource(url);+ await new Promise(resolve => {+ source.onmessage = event => {+ println(event.data);+ if (event.data.startsWith("Last-Event-ID is"))+ resolve();+ };+ });+ source.close();+ });+</script>diff --git a/Tests/LibWeb/Text/input/HTML/EventSource-last-event-id-reset.py b/Tests/LibWeb/Text/input/HTML/EventSource-last-event-id-reset.pynew file mode 100644index 00000000000..55b3ec17432--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/EventSource-last-event-id-reset.py@@ -0,0 +1,25 @@+# Copyright (c) 2026-present, the Ladybird developers.+#+# SPDX-License-Identifier: BSD-2-Clause+++def main(request, response):+ response.headers.set(b"Access-Control-Allow-Origin", b"*")+ if request.method == "OPTIONS":+ response.headers.set(b"Access-Control-Allow-Headers", b"last-event-id")+ return b""++ token = request.GET.first(b"token")+ connection = (request.server.stash.take(token) or 0) + 1+ request.server.stash.put(token, connection)++ last_event_id = request.headers.get(b"Last-Event-ID", b"none").decode()+ response.headers.set(b"Content-Type", b"text/event-stream")+ response.headers.set(b"Cache-Control", b"no-store")++ # The first stream sets the last event ID, and the second resets it with a bare "id" field.+ if connection == 1:+ return b"retry: 0\nid: 1\ndata: set the ID\n\n"+ if connection == 2:+ return f"retry: 0\nid\ndata: reset the ID, Last-Event-ID was {last_event_id}\n\n".encode()+ return f"retry: 100000\ndata: Last-Event-ID is {last_event_id}\n\n".encode()