Skip to content

Commit

Permalink
Fix issue with new line handling in StompDecoder
Browse files Browse the repository at this point in the history
Closes gh-23713
  • Loading branch information
rstoyanchev committed Jan 16, 2020
1 parent 16e49bf commit 3c0c0c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -114,6 +114,10 @@ public List<Message<byte[]>> decode(ByteBuffer byteBuffer,
Message<byte[]> message = decodeMessage(byteBuffer, partialMessageHeaders);
if (message != null) {
messages.add(message);
skipEol(byteBuffer);
if (!byteBuffer.hasRemaining()) {
break;
}
}
else {
break;
Expand All @@ -128,7 +132,7 @@ public List<Message<byte[]>> decode(ByteBuffer byteBuffer,
@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...
Expand Down Expand Up @@ -196,9 +200,10 @@ private void initHeaders(StompHeaderAccessor headerAccessor) {

/**
* 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,7 +16,6 @@

package org.springframework.messaging.simp.stomp;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;

Expand Down Expand Up @@ -78,7 +77,7 @@ public void decodeFrameWithNoBody() {
}

@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);

Expand Down Expand Up @@ -166,6 +165,17 @@ public void decodeFrameBodyNotAllowed() {
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";
Expand All @@ -179,9 +189,7 @@ public void decodeMultipleFramesFromSameBuffer() {
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";
Expand Down

0 comments on commit 3c0c0c0

Please sign in to comment.