Skip to content

Commit d307861

Browse files
committed
fix(sse): handle empty lines and comments in SSE event stream #97
Previously, the SSE parser did not correctly handle empty lines and comments, which could lead to parsing errors. This commit addresses the issue by skipping empty lines and treating lines starting with `:` as comments, as per the SSE format specification. This change ensures that the SSE parser is more robust and adheres to the standard.
1 parent e448c28 commit d307861

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/main/kotlin/cc/unitmesh/devti/llms/azure/ResponseBodyCallback.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,28 @@ class ResponseBodyCallback(private val emitter: FlowableEmitter<SSE>, private va
9292
null
9393
}
9494

95-
// : ping
95+
// skip `: ping` comments for: https://github.com/sysid/sse-starlette/issues/16
9696
line!!.startsWith(": ping") -> {
9797
null
9898
}
9999

100100
else -> {
101-
throw SSEFormatException("Invalid sse format! '$line'")
101+
when {
102+
// sometimes the server maybe returns empty line
103+
line == "" -> {
104+
null
105+
}
106+
107+
// : is comment
108+
// https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
109+
line!!.startsWith(":") -> {
110+
null
111+
}
112+
113+
else -> {
114+
throw SSEFormatException("Invalid sse format! '$line'")
115+
}
116+
}
102117
}
103118
}
104119
}

0 commit comments

Comments
 (0)