Avoid unescape for CONNECT and CONNECTED frames

See gh-27722
This commit is contained in:
조현수(Hyunsoo Cho)/Platform Engineering팀/11ST 2021-11-24 15:20:51 +09:00 committed by Rossen Stoyanchev
parent 7854dbb025
commit 5d91560f92
2 changed files with 23 additions and 4 deletions

View File

@ -143,7 +143,7 @@ public class StompDecoder {
StompCommand stompCommand = StompCommand.valueOf(command); StompCommand stompCommand = StompCommand.valueOf(command);
headerAccessor = StompHeaderAccessor.create(stompCommand); headerAccessor = StompHeaderAccessor.create(stompCommand);
initHeaders(headerAccessor); initHeaders(headerAccessor);
readHeaders(byteBuffer, headerAccessor); readHeaders(stompCommand, byteBuffer, headerAccessor);
payload = readPayload(byteBuffer, headerAccessor); payload = readPayload(byteBuffer, headerAccessor);
} }
if (payload != null) { if (payload != null) {
@ -215,7 +215,9 @@ public class StompDecoder {
return StreamUtils.copyToString(command, StandardCharsets.UTF_8); return StreamUtils.copyToString(command, StandardCharsets.UTF_8);
} }
private void readHeaders(ByteBuffer byteBuffer, StompHeaderAccessor headerAccessor) { private void readHeaders(StompCommand stompCommand, ByteBuffer byteBuffer, StompHeaderAccessor headerAccessor) {
boolean shouldUnescape = (stompCommand != StompCommand.CONNECT && stompCommand != StompCommand.STOMP
&& stompCommand != StompCommand.CONNECTED);
while (true) { while (true) {
ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256); ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256);
boolean headerComplete = false; boolean headerComplete = false;
@ -236,8 +238,8 @@ public class StompDecoder {
} }
} }
else { else {
String headerName = unescape(header.substring(0, colonIndex)); String headerName = shouldUnescape ? unescape(header.substring(0, colonIndex)) : header.substring(0, colonIndex);
String headerValue = unescape(header.substring(colonIndex + 1)); String headerValue = shouldUnescape ? unescape(header.substring(colonIndex + 1)) : header.substring(colonIndex + 1);
try { try {
headerAccessor.addNativeHeader(headerName, headerValue); headerAccessor.addNativeHeader(headerName, headerValue);
} }

View File

@ -159,6 +159,23 @@ public class StompDecoderTests {
assertThat(headers.getFirstNativeHeader("a:\r\n\\b")).isEqualTo("alpha:bravo\r\n\\"); assertThat(headers.getFirstNativeHeader("a:\r\n\\b")).isEqualTo("alpha:bravo\r\n\\");
} }
@Test
public void decodeFrameWithHeaderWithBackslashValue() {
String accept = "accept-version:1.1\n";
String keyAndValueWithBackslash = "key:\\value\n";
Message<byte[]> frame = decode("CONNECT\n" + accept + keyAndValueWithBackslash + "\n\0");
StompHeaderAccessor headers = StompHeaderAccessor.wrap(frame);
assertThat(headers.getCommand()).isEqualTo(StompCommand.CONNECT);
assertThat(headers.toNativeHeaderMap().size()).isEqualTo(2);
assertThat(headers.getFirstNativeHeader("accept-version")).isEqualTo("1.1");
assertThat(headers.getFirstNativeHeader("key")).isEqualTo("\\value");
assertThat(frame.getPayload().length).isEqualTo(0);
}
@Test @Test
public void decodeFrameBodyNotAllowed() { public void decodeFrameBodyNotAllowed() {
assertThatExceptionOfType(StompConversionException.class).isThrownBy(() -> assertThatExceptionOfType(StompConversionException.class).isThrownBy(() ->