Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit efcb33e

Browse files
pavelbucekGerrit Code Review
authored andcommitted
Merge "JERSEY-2869: InboundEventReader in SSE support LF, CR, CRLF delimiters"
2 parents c316a8a + 9894d92 commit efcb33e

File tree

2 files changed

+161
-11
lines changed

2 files changed

+161
-11
lines changed

media/sse/src/main/java/org/glassfish/jersey/media/sse/InboundEventReader.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
*/
6969
@ConstrainedTo(RuntimeType.CLIENT)
7070
class InboundEventReader implements MessageBodyReader<InboundEvent> {
71+
7172
private static final Logger LOGGER = Logger.getLogger(InboundEventReader.class.getName());
72-
private static final byte[] EOL_DATA = new byte[]{'\n'};
73+
private static final byte[] EOL_DATA = new byte[] {'\n'};
7374

7475
@Inject
7576
private Provider<MessageBodyWorkers> messageBodyWorkers;
@@ -110,8 +111,15 @@ public InboundEvent readFrom(final Class<InboundEvent> type,
110111
do {
111112
switch (currentState) {
112113
case NEW_LINE:
113-
b = entityStream.read();
114-
if (b == '\n' || b == -1) {
114+
if (b == '\r') {
115+
// read next byte in case of CRLF delimiter
116+
b = entityStream.read();
117+
b = b == '\n' ? entityStream.read() : b;
118+
} else {
119+
b = entityStream.read();
120+
}
121+
122+
if (b == '\n' || b == '\r' || b == -1) {
115123
break loop;
116124
}
117125

@@ -137,14 +145,11 @@ public InboundEvent readFrom(final Class<InboundEvent> type,
137145
tokenData.reset();
138146

139147
if (b == ':') {
140-
// read field value
141-
b = entityStream.read();
142-
if (b == ' ') {
143-
// first space in value has to be skipped
148+
do {
144149
b = entityStream.read();
145-
}
150+
} while (b == ' ');
146151

147-
if (b != '\n' && b != -1) {
152+
if (b != '\n' && b != '\r' && b != -1) {
148153
tokenData.write(b);
149154
b = readLineUntil(entityStream, '\n', tokenData);
150155
}
@@ -167,7 +172,7 @@ public InboundEvent readFrom(final Class<InboundEvent> type,
167172
* the data if the output stream is {@code null}.
168173
*
169174
* @param in input stream to be read.
170-
* @param delimiter delimiter to break the read (apart from {@code EOL ('\n')} or {@code EOF}).
175+
* @param delimiter delimiter to break the read (apart from {@code EOL ('\n', '\r')} or {@code EOF}).
171176
* @param out output stream to write the read data to. May be {@code null}, in which case the
172177
* read data are silently discarded.
173178
* @return value of the last byte read.
@@ -176,7 +181,7 @@ public InboundEvent readFrom(final Class<InboundEvent> type,
176181
private int readLineUntil(final InputStream in, final int delimiter, final OutputStream out) throws IOException {
177182
int b;
178183
while ((b = in.read()) != -1) {
179-
if (b == delimiter || b == '\n') {
184+
if (b == delimiter || b == '\n' || b == '\r') {
180185
break;
181186
} else if (out != null) {
182187
out.write(b);
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.media.sse;
41+
42+
import java.io.ByteArrayInputStream;
43+
import java.io.IOException;
44+
import java.io.InputStream;
45+
import java.lang.annotation.Annotation;
46+
import java.nio.charset.Charset;
47+
import java.util.Collections;
48+
49+
import javax.ws.rs.core.MediaType;
50+
51+
import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
52+
53+
import org.glassfish.hk2.api.ServiceLocator;
54+
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
55+
import org.glassfish.hk2.utilities.binding.AbstractBinder;
56+
57+
import org.junit.Test;
58+
import static org.junit.Assert.assertEquals;
59+
import static org.junit.Assert.assertNull;
60+
61+
/**
62+
* @author Petr Bouda (petr.bouda at oracle.com)
63+
*/
64+
public class InboundEventReaderTest {
65+
66+
private static final MultivaluedStringMap headers;
67+
68+
private static final ServiceLocator locator;
69+
70+
static {
71+
headers = new MultivaluedStringMap();
72+
headers.put("Transfer-Encoding", Collections.singletonList("chunked"));
73+
headers.put("Content-Type", Collections.singletonList("text/event-stream"));
74+
75+
locator = ServiceLocatorUtilities.bind(new TestBinder());
76+
}
77+
78+
@Test
79+
public void testReadWithStartsWithLF() throws Exception {
80+
InboundEvent event = parse(new ByteArrayInputStream("\nevent: custom-message".getBytes()));
81+
assertNull(event.getName());
82+
assertEquals(0, event.getRawData().length);
83+
}
84+
85+
@Test
86+
public void testReadWithStartsWithCR() throws Exception {
87+
InboundEvent event = parse(new ByteArrayInputStream("\revent: custom-message".getBytes()));
88+
assertNull(event.getName());
89+
assertEquals(0, event.getRawData().length);
90+
}
91+
92+
@Test
93+
public void testReadWithLF() throws Exception {
94+
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\ndata: message 1".getBytes());
95+
InboundEvent event = parse(inputStream);
96+
assertEquals("custom-message", event.getName());
97+
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
98+
}
99+
100+
@Test
101+
public void testReadWithCRLF() throws Exception {
102+
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\r\ndata: message 1".getBytes());
103+
InboundEvent event = parse(inputStream);
104+
assertEquals("custom-message", event.getName());
105+
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
106+
}
107+
108+
@Test
109+
public void testReadWithCR() throws Exception {
110+
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\rdata: message 1".getBytes());
111+
InboundEvent event = parse(inputStream);
112+
assertEquals("custom-message", event.getName());
113+
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
114+
}
115+
116+
@Test
117+
public void testReadWithMultipleSpaces() throws Exception {
118+
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\rdata: message 1".getBytes());
119+
InboundEvent event = parse(inputStream);
120+
assertEquals("custom-message", event.getName());
121+
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
122+
}
123+
124+
@Test
125+
public void testReadWithMultipleEndingDelimiter() throws Exception {
126+
ByteArrayInputStream inputStream = new ByteArrayInputStream("event: custom-message\rdata: message 1\r".getBytes());
127+
InboundEvent event = parse(inputStream);
128+
assertEquals("custom-message", event.getName());
129+
assertEquals("message 1\n", new String(event.getRawData(), Charset.defaultCharset()));
130+
}
131+
132+
private static InboundEvent parse(InputStream stream) throws IOException {
133+
return locator.getService(InboundEventReader.class).readFrom(InboundEvent.class, InboundEvent.class, new Annotation[0],
134+
MediaType.valueOf(SseFeature.SERVER_SENT_EVENTS), headers, stream);
135+
}
136+
137+
private static class TestBinder extends AbstractBinder {
138+
139+
@Override
140+
protected void configure() {
141+
bind(InboundEventReader.class)
142+
.to(InboundEventReader.class);
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)