|
@@ -5,7 +5,6 @@ package irc
|
5
|
5
|
|
6
|
6
|
import (
|
7
|
7
|
"bytes"
|
8
|
|
- "errors"
|
9
|
8
|
"net"
|
10
|
9
|
"unicode/utf8"
|
11
|
10
|
|
|
@@ -22,8 +21,7 @@ const (
|
22
|
21
|
)
|
23
|
22
|
|
24
|
23
|
var (
|
25
|
|
- crlf = []byte{'\r', '\n'}
|
26
|
|
- errWSBinaryMessage = errors.New("WebSocket binary messages are unsupported")
|
|
24
|
+ crlf = []byte{'\r', '\n'}
|
27
|
25
|
)
|
28
|
26
|
|
29
|
27
|
// IRCConn abstracts away the distinction between a regular
|
|
@@ -90,11 +88,13 @@ func (cc *IRCStreamConn) Close() (err error) {
|
90
|
88
|
|
91
|
89
|
// IRCWSConn is an IRCConn over a websocket.
|
92
|
90
|
type IRCWSConn struct {
|
93
|
|
- conn *websocket.Conn
|
|
91
|
+ conn *websocket.Conn
|
|
92
|
+ binary bool
|
94
|
93
|
}
|
95
|
94
|
|
96
|
95
|
func NewIRCWSConn(conn *websocket.Conn) IRCWSConn {
|
97
|
|
- return IRCWSConn{conn: conn}
|
|
96
|
+ binary := conn.Subprotocol() == "binary.ircv3.net"
|
|
97
|
+ return IRCWSConn{conn: conn, binary: binary}
|
98
|
98
|
}
|
99
|
99
|
|
100
|
100
|
func (wc IRCWSConn) UnderlyingConn() *utils.WrappedConn {
|
|
@@ -106,7 +106,11 @@ func (wc IRCWSConn) UnderlyingConn() *utils.WrappedConn {
|
106
|
106
|
func (wc IRCWSConn) WriteLine(buf []byte) (err error) {
|
107
|
107
|
buf = bytes.TrimSuffix(buf, crlf)
|
108
|
108
|
// #1483: if we have websockets at all, then we're enforcing utf8
|
109
|
|
- return wc.conn.WriteMessage(websocket.TextMessage, buf)
|
|
109
|
+ messageType := websocket.TextMessage
|
|
110
|
+ if wc.binary {
|
|
111
|
+ messageType = websocket.BinaryMessage
|
|
112
|
+ }
|
|
113
|
+ return wc.conn.WriteMessage(messageType, buf)
|
110
|
114
|
}
|
111
|
115
|
|
112
|
116
|
func (wc IRCWSConn) WriteLines(buffers [][]byte) (err error) {
|
|
@@ -122,11 +126,12 @@ func (wc IRCWSConn) WriteLines(buffers [][]byte) (err error) {
|
122
|
126
|
func (wc IRCWSConn) ReadLine() (line []byte, err error) {
|
123
|
127
|
messageType, line, err := wc.conn.ReadMessage()
|
124
|
128
|
if err == nil {
|
125
|
|
- if messageType == websocket.TextMessage {
|
126
|
|
- return line, nil
|
127
|
|
- } else {
|
128
|
|
- return nil, errWSBinaryMessage
|
|
129
|
+ if messageType == websocket.BinaryMessage && globalUtf8EnforcementSetting {
|
|
130
|
+ if !utf8.Valid(line) {
|
|
131
|
+ return line, errInvalidUtf8
|
|
132
|
+ }
|
129
|
133
|
}
|
|
134
|
+ return line, nil
|
130
|
135
|
} else if err == websocket.ErrReadLimit {
|
131
|
136
|
return line, ircreader.ErrReadQ
|
132
|
137
|
} else {
|