Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add space between SSE field and value for compatibility #2149

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **change:** axum's MSRV is now 1.63 ([#2021])
- **added:** Implement `Handler` for `T: IntoResponse` ([#2140])
- **added:** Implement `IntoResponse` for `(R,) where R: IntoResponse` ([#2143])
- **changed:** For SSE, add space between field and value for compatibility ([#2149])

[#2021]: https://github.com/tokio-rs/axum/pull/2021
[#2014]: https://github.com/tokio-rs/axum/pull/2014
Expand All @@ -82,6 +83,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#2096]: https://github.com/tokio-rs/axum/pull/2096
[#2140]: https://github.com/tokio-rs/axum/pull/2140
[#2143]: https://github.com/tokio-rs/axum/pull/2143
[#2149]: https://github.com/tokio-rs/axum/pull/2149

# 0.6.17 (25. April, 2023)

Expand Down
15 changes: 6 additions & 9 deletions axum/src/response/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ pub struct Event {
}

impl Event {
/// Set the event's data data field(s) (`data:<content>`)
/// Set the event's data data field(s) (`data: <content>`)
///
/// 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].
///
Expand Down Expand Up @@ -202,7 +202,7 @@ impl Event {
self
}

/// Set the event's data field to a value serialized as unformatted JSON (`data:<content>`).
/// Set the event's data field to a value serialized as unformatted JSON (`data: <content>`).
///
/// This corresponds to [`MessageEvent`'s data field].
///
Expand All @@ -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');

Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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");
Expand Down