From cc9d67404efa1d65d7b2b0ba5cc5cf5f7ad45f74 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 18 Oct 2026 12:41:23 -0400 Subject: [PATCH] Dispatch an SSE event with an empty type as "message" An `event:` line without a value left the type empty, so the event never reached on_event("message"), and on_message saw an empty type. The HTML standard dispatches an event with an empty type as "message". --- httplib.h | 3 ++- test/test.cc | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index e08e882e..7bcad4a4 100644 --- a/httplib.h +++ b/httplib.h @@ -5045,7 +5045,8 @@ inline bool SSEClient::parse_sse_line(const std::string &line, SSEMessage &msg, // Handle known fields if (field == "event") { - msg.event = value; + // An empty event type is dispatched as "message" per the SSE spec + msg.event = value.empty() ? std::string("message") : std::move(value); } else if (field == "data") { // Multiple data lines are concatenated with newlines if (has_data) { msg.data += "\n"; } diff --git a/test/test.cc b/test/test.cc index 857f8c2a..c75aa984 100644 --- a/test/test.cc +++ b/test/test.cc @@ -25263,6 +25263,14 @@ TEST_F(SSEParsingTest, EventWithoutDataResetsEventType) { EXPECT_EQ(msgs[0].data, "hello"); } +TEST_F(SSEParsingTest, EmptyEventTypeIsMessage) { + // Also when the empty field replaces an earlier type + auto msgs = parse("event:\ndata: a\n\nevent: update\nevent:\ndata: b\n\n"); + ASSERT_EQ(msgs.size(), 2u); + EXPECT_EQ(msgs[0].event, "message"); + EXPECT_EQ(msgs[1].event, "message"); +} + TEST_F(SSEParsingTest, EventWithoutDataUpdatesLastEventId) { // The first connection sends only an id; the second echoes back the // Last-Event-ID it was reconnected with