Fix issue with new line handling in StompDecoder

Closes gh-23713
This commit is contained in:
Rossen Stoyanchev
2020-01-15 17:42:33 +00:00
parent 16e49bf0c9
commit 3c0c0c0597
2 changed files with 23 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -114,6 +114,10 @@ public class StompDecoder {
Message<byte[]> message = decodeMessage(byteBuffer, partialMessageHeaders);
if (message != null) {
messages.add(message);
skipEol(byteBuffer);
if (!byteBuffer.hasRemaining()) {
break;
}
}
else {
break;
@@ -128,7 +132,7 @@ public class StompDecoder {
@Nullable
private Message<byte[]> decodeMessage(ByteBuffer byteBuffer, @Nullable MultiValueMap<String, String> headers) {
Message<byte[]> decodedMessage = null;
skipLeadingEol(byteBuffer);
skipEol(byteBuffer);
// Explicit mark/reset access via Buffer base type for compatibility
// with covariant return type on JDK 9's ByteBuffer...
@@ -196,9 +200,10 @@ public class StompDecoder {
/**
* Skip one ore more EOL characters at the start of the given ByteBuffer.
* Those are STOMP heartbeat frames.
* STOMP, section 2.1 says: "The NULL octet can be optionally followed by
* multiple EOLs."
*/
protected void skipLeadingEol(ByteBuffer byteBuffer) {
protected void skipEol(ByteBuffer byteBuffer) {
while (true) {
if (!tryConsumeEndOfLine(byteBuffer)) {
break;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.messaging.simp.stomp;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;
@@ -78,7 +77,7 @@ public class StompDecoderTests {
}
@Test
public void decodeFrame() throws UnsupportedEncodingException {
public void decodeFrame() {
Message<byte[]> frame = decode("SEND\ndestination:test\n\nThe body of the message\0");
StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame);
@@ -166,6 +165,17 @@ public class StompDecoderTests {
decode("CONNECT\naccept-version:1.2\n\nThe body of the message\0"));
}
@Test // gh-23713
public void decodeFramesWithExtraNewLines() {
String frame1 = "SEND\ndestination:test\n\nbody\0\n\n\n";
ByteBuffer buffer = ByteBuffer.wrap((frame1).getBytes());
final List<Message<byte[]>> messages = decoder.decode(buffer);
assertThat(messages.size()).isEqualTo(1);
assertThat(StompHeaderAccessor.wrap(messages.get(0)).getCommand()).isEqualTo(StompCommand.SEND);
}
@Test
public void decodeMultipleFramesFromSameBuffer() {
String frame1 = "SEND\ndestination:test\n\nThe body of the message\0";
@@ -179,9 +189,7 @@ public class StompDecoderTests {
assertThat(StompHeaderAccessor.wrap(messages.get(1)).getCommand()).isEqualTo(StompCommand.DISCONNECT);
}
// SPR-13111
@Test
@Test // SPR-13111
public void decodeFrameWithHeaderWithEmptyValue() {
String accept = "accept-version:1.1\n";
String valuelessKey = "key:\n";