Bound the size of an SSE event

The SSE client buffered a line, or the data of an event, with no limit
of its own, and with payload_max_length(0) with none at all. Add
CPPHTTPLIB_SSE_MAX_EVENT_LENGTH (16MB) and end the connection with
ExceedMaxPayloadSize past it.
This commit is contained in:
yhirose committed 2026-10-16 16:39:00 -04:00
1 parent f5e11024da
commit cadcc425b8
3 files changed
+44

No files matched your search

+13
View File
@@ -138,6 +138,10 @@
#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (100 * 1024 * 1024) // 100MB
#endif
#ifndef CPPHTTPLIB_SSE_MAX_EVENT_LENGTH
#define CPPHTTPLIB_SSE_MAX_EVENT_LENGTH (16 * 1024 * 1024) // 16MB
#endif
#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH
#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192
#endif
@@ -5084,6 +5088,15 @@ inline void SSEClient::run_event_loop() {
// Keep unprocessed data in buffer
buffer.erase(0, line_start);
// Neither an event nor a line still waiting for its end may grow
// without bound. 0 means no limit.
if (CPPHTTPLIB_SSE_MAX_EVENT_LENGTH > 0 &&
buffer.size() + current_msg.data.size() >
CPPHTTPLIB_SSE_MAX_EVENT_LENGTH) {
if (on_error_) { on_error_(Error::ExceedMaxPayloadSize); }
break;
}
}
// Connection ended