fix: #419 TLS https URL for SSE endpoint (#420)

Fixes that in SSL deployments, e.g. on k8s or cloud run the SSE endpoint
was statically returning `http:`

not a scheme based on request TLS attribute.

Fixes https://github.com/googleapis/genai-toolbox/issues/419

---------

Co-authored-by: Yuan <45984206+Yuan325@users.noreply.github.com>
This commit is contained in:
Michael Hunger
2025-04-09 20:43:23 +02:00
committed by GitHub
parent cf967452e1
commit 0a7d3ff06b

View File

@@ -126,8 +126,17 @@ func sseHandler(s *Server, w http.ResponseWriter, r *http.Request) {
s.sseManager.add(sessionId, session)
defer s.sseManager.remove(sessionId)
// https scheme formatting if (forwarded) request is a TLS request
proto := r.Header.Get("X-Forwarded-Proto")
if (proto == "") {
if r.TLS == nil {
proto = "http"
} else {
proto = "https"
}
}
// send initial endpoint event
messageEndpoint := fmt.Sprintf("http://%s/mcp?sessionId=%s", r.Host, sessionId)
messageEndpoint := fmt.Sprintf("%s://%s/mcp?sessionId=%s", proto, r.Host, sessionId)
s.logger.DebugContext(ctx, fmt.Sprintf("sending endpoint event: %s", messageEndpoint))
fmt.Fprintf(w, "event: endpoint\ndata: %s\n\n", messageEndpoint)
flusher.Flush()