Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ extension ServerSentEventsSerializationSequence.Iterator {
buffer.append(contentsOf: value.utf8)
buffer.append(ASCII.lf)
}
if let id = value.id { encodeField(name: "id", value: id) }
if let event = value.event { encodeField(name: "event", value: event) }
// `id` and `event` values must not contain any newline characters, as they're used to
// delimit fields and events, so strip any that are present.
func removingNewlines(_ value: some StringProtocol) -> String {
value.replacingOccurrences(of: "\r\n", with: "").replacingOccurrences(of: "\r", with: "")
.replacingOccurrences(of: "\n", with: "")
}
if let id = value.id { encodeField(name: "id", value: removingNewlines(id)) }
if let event = value.event { encodeField(name: "event", value: removingNewlines(event)) }
if let retry = value.retry { encodeField(name: "retry", value: String(retry)) }
if let data = value.data {
// Normalize the data section by replacing CRLF and CR with just LF.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ final class Test_ServerSentEventsEncoding: Test_Runtime {
data: This is a message with an ID.


"""#
)
}
func testIdAndEventStripNewlines() async throws {
// The id and event fields must not contain newlines, as they would otherwise be
// misinterpreted as the start of another field.
try await _test(
input: [.init(id: "123\r\n456", event: "custom\nEvent\r", data: "hello")],
output: #"""
id: 123456
event: customEvent
data: hello


"""#
)
}
Expand Down
Loading