Remove support for the base64 WebSocket subprotocol.

This was only needed for very very old Firefox before WebSockets were
properly standardized.
This commit is contained in:
David Fifield 2017-01-18 20:00:59 -08:00
parent 3e78251715
commit 15963688c2

View file

@ -10,7 +10,6 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"encoding/base64"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
@ -31,8 +30,7 @@ import (
const ptMethodName = "snowflake" const ptMethodName = "snowflake"
const requestTimeout = 10 * time.Second const requestTimeout = 10 * time.Second
// "4/3+1" accounts for possible base64 encoding. const maxMessageSize = 64*1024
const maxMessageSize = 64*1024*4/3 + 1
var ptInfo pt.ServerInfo var ptInfo pt.ServerInfo
@ -50,11 +48,9 @@ func usage() {
} }
// An abstraction that makes an underlying WebSocket connection look like an // An abstraction that makes an underlying WebSocket connection look like an
// io.ReadWriteCloser. It internally takes care of things like base64 encoding // io.ReadWriteCloser.
// and decoding.
type webSocketConn struct { type webSocketConn struct {
Ws *websocket.WebSocket Ws *websocket.WebSocket
Base64 bool
messageBuf []byte messageBuf []byte
} }
@ -70,26 +66,12 @@ func (conn *webSocketConn) Read(b []byte) (n int, err error) {
err = io.EOF err = io.EOF
return return
} }
if conn.Base64 {
if m.Opcode != 1 {
err = errors.New(fmt.Sprintf("got non-text opcode %d with the base64 subprotocol", m.Opcode))
return
}
conn.messageBuf = make([]byte, base64.StdEncoding.DecodedLen(len(m.Payload)))
var num int
num, err = base64.StdEncoding.Decode(conn.messageBuf, m.Payload)
if err != nil {
return
}
conn.messageBuf = conn.messageBuf[:num]
} else {
if m.Opcode != 2 { if m.Opcode != 2 {
err = errors.New(fmt.Sprintf("got non-binary opcode %d with no subprotocol", m.Opcode)) err = errors.New(fmt.Sprintf("got non-binary opcode %d", m.Opcode))
return return
} }
conn.messageBuf = m.Payload conn.messageBuf = m.Payload
} }
}
n = copy(b, conn.messageBuf) n = copy(b, conn.messageBuf)
conn.messageBuf = conn.messageBuf[n:] conn.messageBuf = conn.messageBuf[n:]
@ -98,20 +80,9 @@ func (conn *webSocketConn) Read(b []byte) (n int, err error) {
} }
// Implements io.Writer. // Implements io.Writer.
func (conn *webSocketConn) Write(b []byte) (n int, err error) { func (conn *webSocketConn) Write(b []byte) (int, error) {
if conn.Base64 { err := conn.Ws.WriteMessage(2, b)
buf := make([]byte, base64.StdEncoding.EncodedLen(len(b))) return len(b), err
base64.StdEncoding.Encode(buf, b)
err = conn.Ws.WriteMessage(1, buf)
if err != nil {
return
}
n = len(b)
} else {
err = conn.Ws.WriteMessage(2, b)
n = len(b)
}
return
} }
// Implements io.Closer. // Implements io.Closer.
@ -125,7 +96,6 @@ func (conn *webSocketConn) Close() error {
func newWebSocketConn(ws *websocket.WebSocket) webSocketConn { func newWebSocketConn(ws *websocket.WebSocket) webSocketConn {
var conn webSocketConn var conn webSocketConn
conn.Ws = ws conn.Ws = ws
conn.Base64 = (ws.Subprotocol == "base64")
return conn return conn
} }
@ -233,7 +203,6 @@ func startServer(ln net.Listener) (net.Listener, error) {
go func() { go func() {
defer ln.Close() defer ln.Close()
var config websocket.Config var config websocket.Config
config.Subprotocols = []string{"base64"}
config.MaxMessageSize = maxMessageSize config.MaxMessageSize = maxMessageSize
s := &http.Server{ s := &http.Server{
Handler: config.Handler(webSocketHandler), Handler: config.Handler(webSocketHandler),