Skip to content

Commit 91cea5c

Browse files
committed
feat(sse): add middleware to prevent proxy buffering of SSE connections
1 parent 26ad537 commit 91cea5c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/httpapi/server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
188188
})
189189
router.Use(corsMiddleware.Handler)
190190

191+
// Add SSE middleware to prevent proxy buffering
192+
sseMiddleware := func(next http.Handler) http.Handler {
193+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
194+
if strings.HasSuffix(r.URL.Path, "/events") || strings.HasSuffix(r.URL.Path, "/screen") {
195+
// Disable proxy buffering for SSE endpoints
196+
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
197+
w.Header().Set("Pragma", "no-cache")
198+
w.Header().Set("Expires", "0")
199+
w.Header().Set("X-Accel-Buffering", "no") // nginx
200+
w.Header().Set("X-Proxy-Buffering", "no") // generic proxy
201+
w.Header().Set("Connection", "keep-alive")
202+
}
203+
next.ServeHTTP(w, r)
204+
})
205+
}
206+
router.Use(sseMiddleware)
207+
191208
humaConfig := huma.DefaultConfig("AgentAPI", "0.6.1")
192209
humaConfig.Info.Description = "HTTP API for Claude Code, Goose, and Aider.\n\nhttps://github.com/coder/agentapi"
193210
api := humachi.New(router, humaConfig)
@@ -388,6 +405,7 @@ func (s *Server) subscribeEvents(ctx context.Context, input *struct{}, send sse.
388405
return
389406
}
390407
}
408+
391409
for {
392410
select {
393411
case event, ok := <-ch:

0 commit comments

Comments
 (0)