mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-09 16:52:52 +07:00
Fix SSE client not clearing Last-Event-ID on an empty id field (Fix #2611)
An event with an empty id field must reset the last event ID, so that no Last-Event-ID header is sent on reconnect. run_event_loop only updated last_event_id_ when the id was non-empty, so it could not tell an empty id field from an event with no id field, and kept sending the stale ID. Track whether an id field was seen with a has_id flag, as has_data does for the data field, and add a regression test.
This commit is contained in:
@@ -22470,6 +22470,30 @@ TEST_F(SSEParsingTest, EventWithoutDataUpdatesLastEventId) {
|
||||
EXPECT_EQ(msgs[0].data, "42");
|
||||
}
|
||||
|
||||
TEST_F(SSEParsingTest, EmptyEventIdClearsLastEventId) {
|
||||
// The first connection sets an id and then clears it with an empty one; the
|
||||
// second reports whether it was reconnected with a Last-Event-ID
|
||||
std::atomic<int> connection_count{0};
|
||||
server_->Get("/id-clear", [&](const Request &req, Response &res) {
|
||||
if (connection_count++ == 0) {
|
||||
res.set_content("id: 1\ndata: first\n\nid:\ndata: second\n\n",
|
||||
"text/event-stream");
|
||||
} else {
|
||||
res.set_content(
|
||||
std::string("data: ") +
|
||||
(req.has_header("Last-Event-ID") ? "sent" : "not sent") +
|
||||
"\n\nevent: end\ndata: end\n\n",
|
||||
"text/event-stream");
|
||||
}
|
||||
});
|
||||
|
||||
auto msgs = collect("/id-clear");
|
||||
ASSERT_EQ(msgs.size(), 3u);
|
||||
EXPECT_EQ(msgs[0].id, "1");
|
||||
EXPECT_EQ(msgs[1].id, "");
|
||||
EXPECT_EQ(msgs[2].data, "not sent");
|
||||
}
|
||||
|
||||
TEST_F(SSEParsingTest, CompleteEventParsing) {
|
||||
auto msgs = parse("event: notification\nid: evt-42\n"
|
||||
"data: {\"type\":\"alert\"}\nretry: 1000\n\n");
|
||||
|
||||
Reference in New Issue
Block a user