Skip to content

Commit

Permalink
Skip "data:" lines without content
Browse files Browse the repository at this point in the history
Closes gh-27923
  • Loading branch information
jhoeller committed Jan 12, 2022
1 parent cf5b863 commit 6b30522
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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 @@ -42,6 +42,7 @@
*
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 5.0
*/
public class ServerSentEventHttpMessageReader implements HttpMessageReader<Object> {
Expand Down Expand Up @@ -140,22 +141,23 @@ public Flux<Object> read(
private Object buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap,
Map<String, Object> hints) {

ServerSentEvent.Builder<Object> sseBuilder = shouldWrap ? ServerSentEvent.builder() : null;
ServerSentEvent.Builder<Object> sseBuilder = (shouldWrap ? ServerSentEvent.builder() : null);
StringBuilder data = null;
StringBuilder comment = null;

for (String line : lines) {
if (line.startsWith("data:")) {
data = (data != null ? data : new StringBuilder());
if (line.charAt(5) != ' ') {
data.append(line, 5, line.length());
int length = line.length();
if (length > 5) {
int index = (line.charAt(5) != ' ' ? 5 : 6);
if (length > index) {
data = (data != null ? data : new StringBuilder());
data.append(line, index, line.length());
data.append('\n');
}
}
else {
data.append(line, 6, line.length());
}
data.append('\n');
}
if (shouldWrap) {
else if (shouldWrap) {
if (line.startsWith("id:")) {
sseBuilder.id(line.substring(3).trim());
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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 @@ -40,6 +40,7 @@
* Unit tests for {@link ServerSentEventHttpMessageReader}.
*
* @author Sebastien Deleuze
* @author Juergen Hoeller
*/
public class ServerSentEventHttpMessageReaderTests extends AbstractLeakCheckingTests {

Expand All @@ -66,7 +67,7 @@ public void readServerSentEvents() {
MockServerHttpRequest request = MockServerHttpRequest.post("/")
.body(Mono.just(stringBuffer(
"id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n\n" +
"id:c43\nevent:bar\nretry:456\ndata:baz\n\n")));
"id:c43\nevent:bar\nretry:456\ndata:baz\n\ndata:\n\ndata: \n\n")));

Flux<ServerSentEvent> events = this.reader
.read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class),
Expand All @@ -87,6 +88,8 @@ public void readServerSentEvents() {
assertThat(event.comment()).isNull();
assertThat(event.data()).isEqualTo("baz");
})
.consumeNextWith(event -> assertThat(event.data()).isNull())
.consumeNextWith(event -> assertThat(event.data()).isNull())
.expectComplete()
.verify();
}
Expand Down Expand Up @@ -175,7 +178,7 @@ public void readPojo() {
.verify();
}

@Test // gh-24389
@Test // gh-24389
void readPojoWithCommentOnly() {
MockServerHttpRequest request = MockServerHttpRequest.post("/")
.body(Flux.just(stringBuffer(":ping\n"), stringBuffer("\n")));
Expand Down Expand Up @@ -221,7 +224,6 @@ public void readError() {

@Test
public void maxInMemoryLimit() {

this.reader.setMaxInMemorySize(17);

MockServerHttpRequest request = MockServerHttpRequest.post("/")
Expand All @@ -235,9 +237,8 @@ public void maxInMemoryLimit() {
.verify();
}

@Test // gh-24312
@Test // gh-24312
public void maxInMemoryLimitAllowsReadingPojoLargerThanDefaultSize() {

int limit = this.jsonDecoder.getMaxInMemorySize();

String fooValue = getStringOfSize(limit) + "and then some more";
Expand All @@ -259,13 +260,6 @@ public void maxInMemoryLimitAllowsReadingPojoLargerThanDefaultSize() {
.verify();
}

private static String getStringOfSize(long size) {
StringBuilder content = new StringBuilder("Aa");
while (content.length() < size) {
content.append(content);
}
return content.toString();
}

private DataBuffer stringBuffer(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
Expand All @@ -274,4 +268,12 @@ private DataBuffer stringBuffer(String value) {
return buffer;
}

private static String getStringOfSize(long size) {
StringBuilder content = new StringBuilder("Aa");
while (content.length() < size) {
content.append(content);
}
return content.toString();
}

}

0 comments on commit 6b30522

Please sign in to comment.