diff --git a/api/http/handler/websocket/stream.go b/api/http/handler/websocket/stream.go index 16e68f8de..850d44a4c 100644 --- a/api/http/handler/websocket/stream.go +++ b/api/http/handler/websocket/stream.go @@ -4,6 +4,7 @@ import ( "bufio" "github.com/gorilla/websocket" "net" + "unicode/utf8" ) func streamFromWebsocketConnToTCPConn(websocketConn *websocket.Conn, tcpConn net.Conn, errorChan chan error) { @@ -26,15 +27,33 @@ func streamFromTCPConnToWebsocketConn(websocketConn *websocket.Conn, br *bufio.R for { out := make([]byte, 2048) _, err := br.Read(out) + processedOutput := validString(string(out[:])) if err != nil { errorChan <- err break } - err = websocketConn.WriteMessage(websocket.TextMessage, out) + err = websocketConn.WriteMessage(websocket.TextMessage, []byte(processedOutput)) if err != nil { errorChan <- err break } } } + +func validString(s string) string { + if !utf8.ValidString(s) { + v := make([]rune, 0, len(s)) + for i, r := range s { + if r == utf8.RuneError { + _, size := utf8.DecodeRuneInString(s[i:]) + if size == 1 { + continue + } + } + v = append(v, r) + } + s = string(v) + } + return s +}