From 949abffd71d12ae021dfc2dbd0fddd3d8acb9924 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Mon, 13 Sep 2021 12:59:53 -0600 Subject: [PATCH] Updates based on code review Signed-off-by: Ivan Kozlovic --- ws.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ws.go b/ws.go index 7a15822fe..a35ce0847 100644 --- a/ws.go +++ b/ws.go @@ -53,6 +53,7 @@ const ( wsContinuationFrame = 0 wsMaxFrameHeaderSize = 14 wsMaxControlPayloadSize = 125 + wsCloseSatusSize = 2 // From https://tools.ietf.org/html/rfc6455#section-11.7 wsCloseStatusNormalClosure = 1000 @@ -381,18 +382,18 @@ func (r *websocketReader) handleControlFrame(frameType wsOpCode, buf []byte, pos switch frameType { case wsCloseMessage: status := wsCloseStatusNoStatusReceived - body := _EMPTY_ + var body string lp := len(payload) // If there is a payload, the status is represented as a 2-byte // unsigned integer (in network byte order). Then, there may be an // optional body. - hasStatus, hasBody := lp >= 2, lp > 2 + hasStatus, hasBody := lp >= wsCloseSatusSize, lp > wsCloseSatusSize if hasStatus { // Decode the status - status = int(binary.BigEndian.Uint16(payload[:2])) + status = int(binary.BigEndian.Uint16(payload[:wsCloseSatusSize])) // Now if there is a body, capture it and make sure this is a valid UTF-8. if hasBody { - body = string(payload[2:]) + body = string(payload[wsCloseSatusSize:]) if !utf8.ValidString(body) { // https://tools.ietf.org/html/rfc6455#section-5.5.1 // If body is present, it must be a valid utf8