From 76bdf6f2202f079f9f2c23518a604bd5b01cfcd4 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Fri, 17 Mar 2023 17:23:24 -0300 Subject: [PATCH] fix(websocket): use the read part of the buffer instead of everything EE-5235 (#8685) --- api/http/handler/websocket/stream.go | 30 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/api/http/handler/websocket/stream.go b/api/http/handler/websocket/stream.go index c3e8a5835..77ce78566 100644 --- a/api/http/handler/websocket/stream.go +++ b/api/http/handler/websocket/stream.go @@ -31,14 +31,14 @@ func streamFromReaderToWebsocket(websocketConn *websocket.Conn, reader io.Reader out := make([]byte, readerBufferSize) for { - _, err := reader.Read(out) + n, err := reader.Read(out) if err != nil { errorChan <- err break } - processedOutput := validString(string(out)) + processedOutput := validString(string(out[:n])) err = websocketConn.WriteMessage(websocket.TextMessage, []byte(processedOutput)) if err != nil { errorChan <- err @@ -49,22 +49,22 @@ func streamFromReaderToWebsocket(websocketConn *websocket.Conn, reader io.Reader } 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 - } - } + if utf8.ValidString(s) { + return s + } - v = append(v, r) + v := make([]rune, 0, len(s)) + + for i, r := range s { + if r == utf8.RuneError { + _, size := utf8.DecodeRuneInString(s[i:]) + if size == 1 { + continue + } } - s = string(v) + v = append(v, r) } - return s + return string(v) }