Skip to content

Commit d965e9a

Browse files
authored
Handle no status in FormatCloseMessage
Return empty message for CloseNoStatusReceived. This status indicates that the message is empty, so make it so. Because it's illegal to send CloseNoStatusReceived, this change should not break a correct application.
1 parent cdedf21 commit d965e9a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

conn.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,10 +1061,7 @@ func (c *Conn) CloseHandler() func(code int, text string) error {
10611061
func (c *Conn) SetCloseHandler(h func(code int, text string) error) {
10621062
if h == nil {
10631063
h = func(code int, text string) error {
1064-
message := []byte{}
1065-
if code != CloseNoStatusReceived {
1066-
message = FormatCloseMessage(code, "")
1067-
}
1064+
message := FormatCloseMessage(code, "")
10681065
c.WriteControl(CloseMessage, message, time.Now().Add(writeWait))
10691066
return nil
10701067
}
@@ -1142,7 +1139,14 @@ func (c *Conn) SetCompressionLevel(level int) error {
11421139
}
11431140

11441141
// FormatCloseMessage formats closeCode and text as a WebSocket close message.
1142+
// An empty message is returned for code CloseNoStatusReceived.
11451143
func FormatCloseMessage(closeCode int, text string) []byte {
1144+
if closeCode == CloseNoStatusReceived {
1145+
// Return empty message because it's illegal to send
1146+
// CloseNoStatusReceived. Return non-nil value in case application
1147+
// checks for nil.
1148+
return []byte{}
1149+
}
11461150
buf := make([]byte, 2+len(text))
11471151
binary.BigEndian.PutUint16(buf, uint16(closeCode))
11481152
copy(buf[2:], text)

0 commit comments

Comments
 (0)