Skip to content

Commit 257788b

Browse files
authored
Propagate explicit empty event ID to ReconnectingRequest. (#28)
Previously, the ReconnectingRequest object only cached event IDs that were Some, which was determined by fetching an event from the parser, and then checking the id (empty = None, not empty = Some). This logic is incorrect. ReconnectingRequest should always propagate the last known explicit event ID, which could in fact be an empty string.
1 parent 4ddba28 commit 257788b

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
TEMP_TEST_OUTPUT=/tmp/contract-test-service.log
2-
SKIPFLAGS = -skip 'reconnection' -skip 'HTTP behavior/client follows 301 redirect' -skip 'HTTP behavior/client follows 307 redirect'
2+
SKIPFLAGS = -skip 'HTTP behavior/client follows 301 redirect' -skip 'HTTP behavior/client follows 307 redirect'
33

44

55
build-contract-tests:

eventsource-client/src/client.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,13 @@ impl<C> ReconnectingRequest<C> {
306306
request_builder = request_builder.header(name, value);
307307
}
308308

309-
if self.last_event_id.is_some() {
310-
let id_as_header = HeaderValue::from_str(self.last_event_id.as_ref().unwrap())
311-
.map_err(|e| Error::InvalidParameter(Box::new(e)))?;
309+
if let Some(id) = self.last_event_id.as_ref() {
310+
if !id.is_empty() {
311+
let id_as_header =
312+
HeaderValue::from_str(id).map_err(|e| Error::InvalidParameter(Box::new(e)))?;
312313

313-
request_builder = request_builder.header("last-event-id", id_as_header);
314+
request_builder = request_builder.header("last-event-id", id_as_header);
315+
}
314316
}
315317

316318
let body = match &self.props.body {
@@ -357,9 +359,7 @@ where
357359
if let Some(event) = this.event_parser.get_event() {
358360
return match event {
359361
SSE::Event(ref evt) => {
360-
if evt.id.is_some() {
361-
*this.last_event_id = evt.id.clone();
362-
}
362+
*this.last_event_id = evt.id.clone();
363363

364364
if let Some(retry) = evt.retry {
365365
this.props.reconnect_opts.delay = Duration::from_millis(retry);

eventsource-client/src/event_parser.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct EventData {
1111
pub event_type: String,
1212
pub data: String,
1313
pub comment: Option<String>,
14-
pub id: String,
14+
pub id: Option<String>,
1515
pub retry: Option<u64>,
1616
}
1717

@@ -25,7 +25,7 @@ impl EventData {
2525
self.data.push('\n');
2626
}
2727

28-
pub fn with_id(mut self, value: String) -> Self {
28+
pub fn with_id(mut self, value: Option<String>) -> Self {
2929
self.id = value;
3030
self
3131
}
@@ -62,11 +62,7 @@ impl TryFrom<EventData> for Option<SSE> {
6262
let mut data = event_data.data.clone();
6363
data.truncate(data.len() - 1);
6464

65-
let id = if event_data.id.is_empty() {
66-
None
67-
} else {
68-
Some(event_data.id.clone())
69-
};
65+
let id = event_data.id.clone();
7066

7167
let retry = event_data.retry;
7268

@@ -151,7 +147,7 @@ pub struct EventParser {
151147
/// the event data currently being decoded
152148
event_data: Option<EventData>,
153149
/// the last-seen event ID; events without an ID will take on this value until it is updated.
154-
last_event_id: String,
150+
last_event_id: Option<String>,
155151
sse: VecDeque<SSE>,
156152
}
157153

@@ -162,7 +158,7 @@ impl EventParser {
162158
incomplete_line: None,
163159
last_char_was_cr: false,
164160
event_data: None,
165-
last_event_id: String::new(),
161+
last_event_id: None,
166162
sse: VecDeque::with_capacity(3),
167163
}
168164
}
@@ -234,8 +230,13 @@ impl EventParser {
234230
continue;
235231
}
236232

237-
self.last_event_id = value.to_string();
238-
event_data.id = self.last_event_id.clone();
233+
if value.is_empty() {
234+
self.last_event_id = Some("".to_string());
235+
} else {
236+
self.last_event_id = Some(value.to_string());
237+
}
238+
239+
event_data.id = self.last_event_id.clone()
239240
} else if key == "retry" {
240241
match value.parse::<u64>() {
241242
Ok(retry) => {

0 commit comments

Comments
 (0)