Skip to content

Commit dd3603c

Browse files
committed
Make client request base64 encoding
This makes gotty-client still work.
1 parent 1eed97f commit dd3603c

File tree

8 files changed

+42
-37
lines changed

8 files changed

+42
-37
lines changed

bindata/static/js/gotty.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindata/static/js/gotty.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/package-lock.json

Lines changed: 0 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/webtty.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class WebTTY {
149149
let maxChunkSize = Math.floor(effectiveBufferSize / 4)*3;
150150

151151
for (let i = 0; i < Math.ceil(dataString.length / maxChunkSize); i++) {
152-
let inputChunk = dataString.substring(i * effectiveBufferSize, Math.min((i + 1) * effectiveBufferSize, dataString.length))
152+
let inputChunk = dataString.substring(i * maxChunkSize, Math.min((i + 1) * maxChunkSize, dataString.length))
153153
this.connection.send(msgInput + btoa(inputChunk));
154154
}
155155
}
@@ -210,6 +210,9 @@ export class WebTTY {
210210
this.term.onResize(resizeHandler);
211211
resizeHandler(termInfo.columns, termInfo.rows);
212212

213+
// Set encoding to base64 (TODO: Fix this up)
214+
connection.send("4base64");
215+
213216
this.term.onInput(
214217
(input: string) => {
215218
this.sendInput(input);

webtty/codecs.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package webtty
2+
3+
type Decoder interface {
4+
Decode(dst, src []byte) (int, error)
5+
}
6+
7+
type Encoder interface {
8+
Encode(dst, src []byte) (int, error)
9+
}
10+
11+
type NullCodec struct{}
12+
13+
func (NullCodec) Encode(dst, src []byte) (int, error) {
14+
return copy(dst, src), nil
15+
}
16+
17+
func (NullCodec) Decode(dst, src []byte) (int, error) {
18+
return copy(dst, src), nil
19+
}

webtty/message_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
Ping = '2'
1414
// Notify that the browser size has been changed
1515
ResizeTerminal = '3'
16+
// Change encoding
17+
SetEncoding = '4'
1618
)
1719

1820
const (

webtty/webtty.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type WebTTY struct {
2424
rows int
2525
reconnect int // in seconds
2626
masterPrefs []byte
27+
decoder Decoder
2728

2829
bufferSize int
2930
writeMutex sync.Mutex
@@ -43,6 +44,7 @@ func New(masterConn Master, slave Slave, options ...Option) (*WebTTY, error) {
4344
rows: 0,
4445

4546
bufferSize: 1024,
47+
decoder: &NullCodec{},
4648
}
4749

4850
for _, option := range options {
@@ -177,9 +179,9 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
177179
}
178180

179181
var decodedBuffer = make([]byte, len(data))
180-
n, err := base64.StdEncoding.Decode(decodedBuffer, data[1:])
182+
n, err := wt.decoder.Decode(decodedBuffer, data[1:])
181183
if err != nil {
182-
return errors.Wrapf(err, "failed to write received data to slave")
184+
return errors.Wrapf(err, "failed to decode received data")
183185
}
184186

185187
_, err = wt.slave.Write(decodedBuffer[:n])
@@ -193,6 +195,14 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
193195
return errors.Wrapf(err, "failed to return Pong message to master")
194196
}
195197

198+
case SetEncoding:
199+
switch string(data[1:]) {
200+
case "base64":
201+
wt.decoder = base64.StdEncoding
202+
case "null":
203+
wt.decoder = NullCodec{}
204+
}
205+
196206
case ResizeTerminal:
197207
if wt.columns != 0 && wt.rows != 0 {
198208
break

webtty/webtty_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func TestWriteFromFrontend(t *testing.T) {
9898
checkNextMsgType(t, mMaster.gottyToMasterReader, SetWindowTitle)
9999
checkNextMsgType(t, mMaster.gottyToMasterReader, SetBufferSize)
100100

101-
// simulate input from frontend... ("hello" in base64)
102-
message := []byte("1aGVsbG8=\n") // line buffered canonical mode
101+
// simulate input from frontend...
102+
message := []byte("1hello\n") // line buffered canonical mode
103103
mMaster.masterToGottyWriter.Write(message)
104104

105105
// ...and make sure it makes it through to the slave intact
@@ -108,7 +108,7 @@ func TestWriteFromFrontend(t *testing.T) {
108108
if err != nil {
109109
t.Fatalf("Unexpected error from Write(): %s", err)
110110
}
111-
if !bytes.Equal(readBuf[:n], []byte("hello")) {
111+
if !bytes.Equal(readBuf[:n], message[1:]) {
112112
t.Fatalf("Unexpected message received: `%s`", readBuf[:n])
113113
}
114114
}

0 commit comments

Comments
 (0)