Bring close frame handling into line between parser implementations.

This commit is contained in:
James Coglan
2017-03-06 21:12:27 +00:00
parent 2a0ebd9844
commit 4564736fde
3 changed files with 14 additions and 13 deletions
@@ -240,6 +240,7 @@ public class Parser {
case OPCODE_CLOSE:
if (frame.length == 0) {
code = DEFAULT_ERROR_CODE;
reason = new byte[0];
} else if (frame.length >= 2) {
code = bitshift(frame.payload[0], 8) | bitshift(frame.payload[1], 0);
reason = Arrays.copyOfRange(frame.payload, 2, (int)frame.length);
@@ -247,7 +248,6 @@ public class Parser {
if (!validCloseCode(code)) {
code = PROTOCOL_ERROR;
// TODO emit error on invalid code
}
observer.onClose(code, reason);
break;
-1
View File
@@ -291,7 +291,6 @@ void wsd_Parser_emit_frame(wsd_Parser *parser)
if (!wsd_Parser_valid_close_code(code)) {
code = WSD_PROTOCOL_ERROR;
// TODO emit error on invalid code
}
wsd_Observer_on_close(parser->observer, code, length, reason);
break;
+13 -11
View File
@@ -135,6 +135,9 @@ module WebSocket
@frame = nil
code = 0
reason = nil
case opcode
when OPCODES[:continuation] then
return parser_error(:protocol_error, 'Received unexpected continuation frame') unless @message
@@ -145,21 +148,20 @@ module WebSocket
@message << frame
when OPCODES[:close] then
code = (bytesize >= 2) ? payload.unpack(PACK_FORMATS[2]).first : nil
reason = (bytesize > 2) ? Driver.encode(bytes[2..-1] || [], UNICODE) : nil
if frame.length == 0
code = DEFAULT_ERROR_CODE
reason = ''
elsif frame.length >= 2
code = payload.unpack(PACK_FORMATS[2]).first
reason = bytes[2..-1]
end
unless (bytesize == 0) or
(code && code >= MIN_RESERVED_ERROR && code <= MAX_RESERVED_ERROR) or
ERROR_CODES.include?(code)
unless ERROR_CODES.include?(code) or
(code >= MIN_RESERVED_ERROR and code <= MAX_RESERVED_ERROR)
code = ERRORS[:protocol_error]
end
if bytesize > 125 or (bytesize > 2 and reason.nil?)
code = ERRORS[:protocol_error]
# TODO emit error on invalid code
end
@driver.__send__(:handle_close, code || DEFAULT_ERROR_CODE, reason || '')
@driver.__send__(:handle_close, code, reason || '')
when OPCODES[:ping] then
@driver.__send__(:handle_ping, payload)