Skip to content

Commit 38db0f6

Browse files
committed
Address issue detected by CodeRabbit
1 parent 815f994 commit 38db0f6

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

server/streamable_http.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,23 @@ func (s *StreamableHTTPServer) handlePost(w http.ResponseWriter, r *http.Request
297297
}
298298
}()
299299

300-
// if there's notifications, upgradedHeader to SSE response
301-
if !upgradedHeader {
302-
w.Header().Set("Content-Type", "text/event-stream")
303-
w.Header().Set("Connection", "keep-alive")
304-
w.Header().Set("Cache-Control", "no-cache")
305-
w.WriteHeader(http.StatusAccepted)
306-
upgradedHeader = true
307-
}
308-
err := writeSSEEvent(w, nt)
309-
if err != nil {
310-
s.logger.Errorf("Failed to write SSE event: %v", err)
311-
return
300+
// if there's notifications and SSE upgrade is not disabled, upgrade to SSE response
301+
if !s.disableSSEUpgrade {
302+
if !upgradedHeader {
303+
w.Header().Set("Content-Type", "text/event-stream")
304+
w.Header().Set("Connection", "keep-alive")
305+
w.Header().Set("Cache-Control", "no-cache")
306+
w.WriteHeader(http.StatusAccepted)
307+
upgradedHeader = true
308+
}
309+
err := writeSSEEvent(w, nt)
310+
if err != nil {
311+
s.logger.Errorf("Failed to write SSE event: %v", err)
312+
return
313+
}
312314
}
315+
// If SSE upgrade is disabled, notifications are dropped in POST mode
316+
// (they can still be sent via separate GET connection if needed)
313317
}()
314318
case <-done:
315319
return
@@ -336,7 +340,8 @@ func (s *StreamableHTTPServer) handlePost(w http.ResponseWriter, r *http.Request
336340
return
337341
}
338342
// If client-server communication already upgraded to SSE stream
339-
if session.upgradeToSSE.Load() {
343+
// Double-check that SSE upgrade is not disabled before performing the upgrade
344+
if session.upgradeToSSE.Load() && !s.disableSSEUpgrade {
340345
if !upgradedHeader {
341346
w.Header().Set("Content-Type", "text/event-stream")
342347
w.Header().Set("Connection", "keep-alive")

0 commit comments

Comments
 (0)