1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use super::HttpStreamResponse;
use crate::api::error::{ErrorKind, ErrorStage, NetError};
use bytes::{Buf, Bytes};
use std::fmt;
use std::time::Duration;
const UTF8_BOM: &[u8] = b"\xef\xbb\xbf";
/// One dispatched server-sent event. Event data and identifiers may be sensitive.
#[derive(Clone, Eq, PartialEq)]
pub struct SseEvent {
/// The value of the `event` field, or `message` when absent or empty.
pub event: String,
/// Consecutive `data` fields joined by a single LF, including empty fields.
pub data: String,
/// The effective event ID, inherited across events until set or cleared.
pub last_event_id: String,
}
impl fmt::Debug for SseEvent {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("SseEvent")
.field("event_bytes", &self.event.len())
.field("data_bytes", &self.data.len())
.field("id_bytes", &self.last_event_id.len())
.finish()
}
}
/// An incremental SSE decoder for a single open HTTP response.
///
/// Dropping the decoder drops the underlying response. Callers may save the
/// response's request ID before conversion to cancel from another task.
pub struct SseEvents {
active: Option<ActiveStream>,
last_event_id: String,
retry_delay: Option<Duration>,
}
struct ActiveStream {
response: HttpStreamResponse,
chunk: Bytes,
steps_left: usize,
line: Vec<u8>,
bom_remaining: &'static [u8],
skip_lf: bool,
max_event_bytes: usize,
block_bytes: usize,
data: Option<String>,
event: String,
pending_id: Option<String>,
}
// Count line scans and source polls, including empty chunks. A ready backlog
// of SSE comments still gives other tasks a chance to cancel or time out.
const STEPS_PER_YIELD: usize = 128;
impl SseEvents {
pub(super) fn new(
response: HttpStreamResponse,
max_event_bytes: usize,
) -> Result<Self, NetError> {
if max_event_bytes == 0 {
return Err(NetError::input("max_event_bytes", "must be positive")
.with_http_request_id(response.request_id));
}
let media_type = response
.content_type()
.and_then(|value| value.split(';').next());
if !media_type.is_some_and(|value| value.trim().eq_ignore_ascii_case("text/event-stream")) {
return Err(
NetError::protocol("SSE requires Content-Type text/event-stream")
.with_stage(ErrorStage::Response)
.with_http_request_id(response.request_id)
.with_http_attempt(response.attempts),
);
}
Ok(Self {
active: Some(ActiveStream {
response,
chunk: Bytes::new(),
steps_left: STEPS_PER_YIELD,
line: Vec::new(),
bom_remaining: UTF8_BOM,
skip_lf: false,
max_event_bytes,
block_bytes: 0,
data: None,
event: String::new(),
pending_id: None,
}),
last_event_id: String::new(),
retry_delay: None,
})
}
/// The last ID committed by a blank line, including an ID-only block.
/// An empty ID clears the value. An unfinished block cannot change it.
pub fn last_event_id(&self) -> &str {
&self.last_event_id
}
/// The most recent server-suggested delay, in milliseconds on the wire.
/// Invalid values and values exceeding a `u64` in milliseconds are ignored.
pub fn retry_delay(&self) -> Option<Duration> {
self.retry_delay
}
/// Yield one event, one terminal error, or permanent EOF. A clean EOF
/// discards an incomplete event. A wire event named `error` is an ordinary
/// event. Dropping this wait preserves the partial event for the next call.
pub async fn next_event(&mut self) -> Option<Result<SseEvent, NetError>> {
loop {
let active = self.active.as_mut()?;
if active.steps_left == 0 {
active.steps_left = STEPS_PER_YIELD;
tokio::task::yield_now().await;
}
if !active.chunk.is_empty() {
match active.decode_chunk(&mut self.last_event_id, &mut self.retry_delay) {
Ok(Some(event)) => return Some(Ok(event)),
Err(error) => {
self.active = None;
return Some(Err(error));
}
Ok(None) => continue,
}
}
active.steps_left -= 1;
match active.response.next_chunk().await {
Some(Ok(chunk)) => active.chunk = chunk,
Some(Err(error)) => {
self.active = None;
return Some(Err(error));
}
None => {
let result = active.finish_bom_prefix();
self.active = None;
return result.err().map(Err);
}
}
}
}
}
impl ActiveStream {
// A partial signature at EOF, or a mismatch after one or two bytes, is
// ordinary input. Restore those bytes and apply the same line limit.
fn finish_bom_prefix(&mut self) -> Result<(), NetError> {
if !self.bom_remaining.is_empty() {
let matched = UTF8_BOM.len() - self.bom_remaining.len();
self.account(matched)?;
self.line.extend_from_slice(&UTF8_BOM[..matched]);
self.bom_remaining = &[];
}
Ok(())
}
fn account(&mut self, count: usize) -> Result<(), NetError> {
self.block_bytes = self
.block_bytes
.checked_add(count)
.filter(|total| *total <= self.max_event_bytes)
.ok_or_else(|| {
NetError::from(ErrorKind::ItemTooLarge)
.with_stage(ErrorStage::Receive)
.with_http_request_id(self.response.request_id)
.with_http_attempt(self.response.attempts)
})?;
Ok(())
}
fn decode_chunk(
&mut self,
last_event_id: &mut String,
retry_delay: &mut Option<Duration>,
) -> Result<Option<SseEvent>, NetError> {
// Only the start of the stream can contain a signature. Keep at most
// two undecided bytes across polls and exclude a complete BOM from the limit.
while let (Some((next, rest)), Some(byte)) =
(self.bom_remaining.split_first(), self.chunk.first())
{
if next != byte {
self.finish_bom_prefix()?;
break;
}
self.chunk.advance(1);
self.bom_remaining = rest;
}
while !self.chunk.is_empty() && self.steps_left > 0 {
self.steps_left -= 1;
// A CR terminates the line immediately, even if its optional LF
// hasn't arrived. Otherwise a lone CR could stall an open stream.
if std::mem::take(&mut self.skip_lf) && self.chunk.first() == Some(&b'\n') {
self.chunk.advance(1);
continue;
}
// One byte beyond the remaining limit is sufficient to reject an
// oversized line, even if a custom source supplied a much larger chunk.
let scan = self
.chunk
.len()
.min((self.max_event_bytes - self.block_bytes).saturating_add(1));
let length = self
.chunk
.iter()
.take(scan)
.position(|byte| matches!(byte, b'\r' | b'\n'))
.map_or(scan, |offset| offset);
self.account(length)?;
self.line.extend_from_slice(&self.chunk.split_to(length));
let Some(ending) = self.chunk.first().copied() else {
break;
};
self.chunk.advance(1);
self.skip_lf = ending == b'\r';
if !self.line.is_empty() {
self.account(1)?;
}
if self.line.is_empty() {
self.block_bytes = 0;
if let Some(id) = self.pending_id.take() {
*last_event_id = id;
}
let event = std::mem::take(&mut self.event);
if let Some(data) = self.data.take() {
return Ok(Some(SseEvent {
event: if event.is_empty() {
"message".into()
} else {
event
},
data,
last_event_id: last_event_id.clone(),
}));
}
continue;
}
// Line endings are ASCII. A complete line contains all bytes of
// each UTF-8 sequence, regardless of the transport chunk boundary.
let line = String::from_utf8_lossy(&self.line);
let (field, value) = match line.split_once(':') {
Some((field, value)) => (field, value.strip_prefix(' ').map_or(value, |v| v)),
None => (line.as_ref(), ""),
};
match field {
"data" => match &mut self.data {
Some(data) => {
data.push('\n');
data.push_str(value);
}
None => self.data = Some(value.to_owned()),
},
"event" => value.clone_into(&mut self.event),
"id" if !value.contains('\0') => self.pending_id = Some(value.to_owned()),
"retry" if value.bytes().all(|byte| byte.is_ascii_digit()) => {
if let Ok(millis) = value.parse::<u64>() {
*retry_delay = Some(Duration::from_millis(millis));
}
}
_ => {}
}
self.line.clear();
}
Ok(None)
}
}
#[cfg(test)]
#[path = "sse_tests.rs"]
mod tests;