Browse Source

Fix line splitting in format_sse_event to comply with SSE spec

Resolves issue where string.splitlines() incorrectly splits on non-LF/CRLF characters, which broke the Server-Sent Events stream specification.
pull/15608/head
Sujit Patil 1 week ago
parent
commit
4690dfaed9
  1. 4
      fastapi/sse.py

4
fastapi/sse.py

@ -206,14 +206,14 @@ def format_sse_event(
lines: list[str] = []
if comment is not None:
for line in comment.splitlines():
for line in comment.replace("\r\n", "\n").replace("\r", "\n").split("\n"):
lines.append(f": {line}")
if event is not None:
lines.append(f"event: {event}")
if data_str is not None:
for line in data_str.splitlines():
for line in data_str.replace("\r\n", "\n").replace("\r", "\n").split("\n"):
lines.append(f"data: {line}")
if id is not None:

Loading…
Cancel
Save