diff --git a/axum/src/response/sse.rs b/axum/src/response/sse.rs index 55947f1b5c2..3187a38f294 100644 --- a/axum/src/response/sse.rs +++ b/axum/src/response/sse.rs @@ -171,9 +171,9 @@ pub struct Event { } impl Event { - /// Set the event's data data field(s) (`data:`) + /// Set the event's data data field(s) (`data: `) /// - /// Newlines in `data` will automatically be broken across `data:` fields. + /// Newlines in `data` will automatically be broken across `data: ` fields. /// /// This corresponds to [`MessageEvent`'s data field]. /// @@ -202,7 +202,7 @@ impl Event { self } - /// Set the event's data field to a value serialized as unformatted JSON (`data:`). + /// Set the event's data field to a value serialized as unformatted JSON (`data: `). /// /// This corresponds to [`MessageEvent`'s data field]. /// @@ -220,7 +220,7 @@ impl Event { panic!("Called `EventBuilder::json_data` multiple times"); } - self.buffer.extend_from_slice(b"data:"); + self.buffer.extend_from_slice(b"data: "); serde_json::to_writer((&mut self.buffer).writer(), &data).map_err(axum_core::Error::new)?; self.buffer.put_u8(b'\n'); @@ -358,10 +358,7 @@ impl Event { ); self.buffer.extend_from_slice(name.as_bytes()); self.buffer.put_u8(b':'); - // Prevent values that start with spaces having that space stripped - if value.starts_with(b" ") { - self.buffer.put_u8(b' '); - } + self.buffer.put_u8(b' '); self.buffer.extend_from_slice(value); self.buffer.put_u8(b'\n'); } @@ -538,7 +535,7 @@ mod tests { #[test] fn leading_space_is_not_stripped() { let no_leading_space = Event::default().data("\tfoobar"); - assert_eq!(&*no_leading_space.finalize(), b"data:\tfoobar\n\n"); + assert_eq!(&*no_leading_space.finalize(), b"data: \tfoobar\n\n"); let leading_space = Event::default().data(" foobar"); assert_eq!(&*leading_space.finalize(), b"data: foobar\n\n");