@@ -55,13 +55,14 @@ type SSEHandler struct {
5555// Sessions are created when the client issues a GET request to the server,
5656// which must accept text/event-stream responses (server-sent events).
5757// For each such request, a new [SSEServerTransport] is created with a distinct
58- // messages endpoint, and connected to the server returned by getServer. It is
59- // up to the user whether getServer returns a distinct [Server] for each new
60- // request, or reuses an existing server.
61- //
58+ // messages endpoint, and connected to the server returned by getServer.
6259// The SSEHandler also handles requests to the message endpoints, by
6360// delegating them to the relevant server transport.
6461//
62+ // The getServer function may return a distinct [Server] for each new
63+ // request, or reuse an existing server. If it returns nil, the handler
64+ // will return a 400 Bad Request.
65+ //
6566// TODO(rfindley): add options.
6667func NewSSEHandler (getServer func (request * http.Request ) * Server ) * SSEHandler {
6768 return & SSEHandler {
@@ -208,8 +209,12 @@ func (h *SSEHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
208209 h .mu .Unlock ()
209210 }()
210211
211- // TODO(hxjiang): getServer returns nil will panic.
212212 server := h .getServer (req )
213+ if server == nil {
214+ // The getServer argument to NewSSEHandler returned nil.
215+ http .Error (w , "no server available" , http .StatusBadRequest )
216+ return
217+ }
213218 ss , err := server .Connect (req .Context (), transport )
214219 if err != nil {
215220 http .Error (w , "connection failed" , http .StatusInternalServerError )
0 commit comments