fix(websocket): use the read part of the buffer instead of everything EE-5235 (#8685)

pull/8692/head
andres-portainer 2 years ago committed by GitHub
parent e142be399d
commit 76bdf6f220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,14 +31,14 @@ func streamFromReaderToWebsocket(websocketConn *websocket.Conn, reader io.Reader
out := make([]byte, readerBufferSize) out := make([]byte, readerBufferSize)
for { for {
_, err := reader.Read(out) n, err := reader.Read(out)
if err != nil { if err != nil {
errorChan <- err errorChan <- err
break break
} }
processedOutput := validString(string(out)) processedOutput := validString(string(out[:n]))
err = websocketConn.WriteMessage(websocket.TextMessage, []byte(processedOutput)) err = websocketConn.WriteMessage(websocket.TextMessage, []byte(processedOutput))
if err != nil { if err != nil {
errorChan <- err errorChan <- err
@ -49,22 +49,22 @@ func streamFromReaderToWebsocket(websocketConn *websocket.Conn, reader io.Reader
} }
func validString(s string) string { func validString(s string) string {
if !utf8.ValidString(s) { if utf8.ValidString(s) {
v := make([]rune, 0, len(s)) return s
}
for i, r := range s {
if r == utf8.RuneError {
_, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
continue
}
}
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)
} }

Loading…
Cancel
Save