Don't drop leading spaces in SSE responses (#600)

* Don't drop leading spaces in SSE responses

* Mention leading space fix in changelog

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
This commit is contained in:
Kai Jewson 2021-12-12 16:11:15 +00:00 committed by GitHub
parent 980a0a466e
commit d9bf100216
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View file

@ -7,11 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- **fixed:** `sse::Event` will no longer drop the leading space of data, event ID and name values
that have it ([#600])
- **fixed:** `sse::Event` is more strict about what field values it supports, disallowing any SSE
events that break the specification (such as field values containing carriage returns) ([#599])
- **added:** `axum::AddExtension::layer` ([#607])
[#599]: https://github.com/tokio-rs/axum/pull/599
[#600]: https://github.com/tokio-rs/axum/pull/600
[#607]: https://github.com/tokio-rs/axum/pull/607
# 0.4.2 (06. December, 2021)

View file

@ -315,7 +315,7 @@ impl fmt::Display for Event {
}
if let Some(event) = &self.event {
"event:".fmt(f)?;
"event: ".fmt(f)?;
event.fmt(f)?;
f.write_char('\n')?;
}
@ -323,7 +323,7 @@ impl fmt::Display for Event {
match &self.data {
Some(DataType::Text(data)) => {
for line in data.split('\n') {
"data:".fmt(f)?;
"data: ".fmt(f)?;
line.fmt(f)?;
f.write_char('\n')?;
}
@ -338,7 +338,7 @@ impl fmt::Display for Event {
}
if let Some(id) = &self.id {
"id:".fmt(f)?;
"id: ".fmt(f)?;
id.fmt(f)?;
f.write_char('\n')?;
}
@ -452,3 +452,12 @@ impl KeepAliveStream {
Poll::Ready(event)
}
}
#[test]
fn leading_space_is_not_stripped() {
let no_leading_space = Event::default().data("\tfoobar");
assert_eq!(no_leading_space.to_string(), "data: \tfoobar\n\n");
let leading_space = Event::default().data(" foobar");
assert_eq!(leading_space.to_string(), "data: foobar\n\n");
}