feat(api): support utf8 output in websocket endpoints

* change TCPConnToWebsocketConn read function to ReadRune

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Update websocket_exec.go

* Delete Project_Default.xml

* Delete modules.xml

* Delete portainer.iml

* Delete vcs.xml

* Delete workspace.xml

* Delete exec.go

* change TCPConnToWebsocketConn read function to ReadRune

* Apply suggestions from code review

Co-Authored-By: Anthony Lapenna <lapenna.anthony@gmail.com>

* Update stream.go
pull/2331/merge
Kai 6 years ago committed by Anthony Lapenna
parent 1af9fb4490
commit 540d3c2c6b

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"net" "net"
"unicode/utf8"
) )
func streamFromWebsocketConnToTCPConn(websocketConn *websocket.Conn, tcpConn net.Conn, errorChan chan error) { func streamFromWebsocketConnToTCPConn(websocketConn *websocket.Conn, tcpConn net.Conn, errorChan chan error) {
@ -26,15 +27,33 @@ func streamFromTCPConnToWebsocketConn(websocketConn *websocket.Conn, br *bufio.R
for { for {
out := make([]byte, 2048) out := make([]byte, 2048)
_, err := br.Read(out) _, err := br.Read(out)
processedOutput := validString(string(out[:]))
if err != nil { if err != nil {
errorChan <- err errorChan <- err
break break
} }
err = websocketConn.WriteMessage(websocket.TextMessage, out) err = websocketConn.WriteMessage(websocket.TextMessage, []byte(processedOutput))
if err != nil { if err != nil {
errorChan <- err errorChan <- err
break 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
}

Loading…
Cancel
Save