Preserve Hijacker/Flusher/Pusher in statusResponseWriter

Return full topic list without trimming namespace prefix in pubsub
handler
This commit is contained in:
anonpenguin 2025-08-23 11:31:30 +03:00
parent c9bb889f8b
commit 44e3f0a795
2 changed files with 29 additions and 10 deletions

View File

@ -1,7 +1,10 @@
package gateway package gateway
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt"
"net"
"net/http" "net/http"
) )
@ -22,6 +25,28 @@ func (w *statusResponseWriter) Write(b []byte) (int, error) {
return n, err return n, err
} }
// Ensure websocket upgrades work by preserving Hijacker/Flusher/Pusher
// interfaces when the underlying ResponseWriter supports them.
func (w *statusResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := w.ResponseWriter.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, fmt.Errorf("hijacker not supported")
}
func (w *statusResponseWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func (w *statusResponseWriter) Push(target string, opts *http.PushOptions) error {
if p, ok := w.ResponseWriter.(http.Pusher); ok {
return p.Push(target, opts)
}
return http.ErrNotSupported
}
// writeJSON writes JSON with status code // writeJSON writes JSON with status code
func writeJSON(w http.ResponseWriter, code int, v any) { func writeJSON(w http.ResponseWriter, code int, v any) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")

View File

@ -171,14 +171,8 @@ func (g *Gateway) pubsubTopicsHandler(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusInternalServerError, err.Error()) writeError(w, http.StatusInternalServerError, err.Error())
return return
} }
prefix := ns + "." // Client returns topics already trimmed to its namespace; return as-is
var filtered []string writeJSON(w, http.StatusOK, map[string]any{"topics": all})
for _, t := range all {
if len(t) >= len(prefix) && t[:len(prefix)] == prefix {
filtered = append(filtered, t[len(prefix):])
}
}
writeJSON(w, http.StatusOK, map[string]any{"topics": filtered})
} }
// resolveNamespaceFromRequest gets namespace from context set by auth middleware // resolveNamespaceFromRequest gets namespace from context set by auth middleware