Skip to content

Commit 9bc7107

Browse files
committed
Add logging around early chunked encoding close
1 parent 7913dbb commit 9bc7107

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

protocol/src/main/java/org/threadly/litesockets/protocols/http/response/HTTPResponseProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public void connectionClosed() {
134134
if (this.nextChunkSize == -1 || this.nextChunkSize == 0) {
135135
reset(null);
136136
} else {
137-
reset(new HTTPParsingException("Did not complete chunked encoding!"));
137+
reset(new HTTPParsingException("Did not complete chunked encoding! " +
138+
this.buffers.remaining() + " / " + nextChunkSize));
138139
}
139140
} else {
140141
long contentLength = response.getHeaders().getContentLength();

protocol/src/test/java/org/threadly/litesockets/protocols/http/ResponseTests.java

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import static org.junit.Assert.assertTrue;
66

77
import java.nio.ByteBuffer;
8-
import java.util.concurrent.ExecutionException;
98
import java.util.concurrent.TimeUnit;
10-
import java.util.concurrent.TimeoutException;
119

1210
import org.junit.Test;
1311
import org.threadly.concurrent.future.SettableListenableFuture;
@@ -18,6 +16,7 @@
1816
import org.threadly.litesockets.protocols.http.response.HTTPResponseProcessor.HTTPResponseCallback;
1917
import org.threadly.litesockets.protocols.http.shared.HTTPConstants;
2018
import org.threadly.litesockets.protocols.http.shared.HTTPResponseCode;
19+
import org.threadly.litesockets.protocols.http.shared.HTTPUtils;
2120
import org.threadly.litesockets.protocols.websocket.WSFrame;
2221

2322
public class ResponseTests {
@@ -50,21 +49,19 @@ public void responseCompareTest1() {
5049
}
5150

5251
@Test
53-
public void responseProcessorTest1() throws InterruptedException, ExecutionException, TimeoutException {
52+
public void responseProcessorBasicTest() throws Exception {
5453
HTTPResponseProcessor hrp = new HTTPResponseProcessor(false);
55-
final SettableListenableFuture<HTTPResponse> header = new SettableListenableFuture<>();
56-
final SettableListenableFuture<Boolean> finished = new SettableListenableFuture<>();
54+
final SettableListenableFuture<HTTPResponse> header = new SettableListenableFuture<>(false);
55+
final SettableListenableFuture<Boolean> finished = new SettableListenableFuture<>(false);
5756
hrp.addHTTPResponseCallback(new HTTPResponseCallback() {
58-
5957
@Override
6058
public void headersFinished(HTTPResponse hr) {
6159
header.setResult(hr);
6260
}
6361

6462
@Override
6563
public void bodyData(ByteBuffer bb) {
66-
// TODO Auto-generated method stub
67-
64+
// ignored
6865
}
6966

7067
@Override
@@ -74,18 +71,61 @@ public void finished() {
7471

7572
@Override
7673
public void hasError(Throwable t) {
77-
// TODO Auto-generated method stub
78-
74+
finished.setFailure(t);
75+
header.setFailure(t);
7976
}
8077

8178
@Override
8279
public void websocketData(WSFrame wsf, ByteBuffer bb) {
83-
// TODO Auto-generated method stub
84-
80+
hasError(new Exception("Unexpected websocket data"));
8581
}});
8682
HTTPResponse hr = new HTTPResponseBuilder().build();
8783
hrp.processData(hr.getMergedByteBuffers());
8884
assertEquals(hr, header.get(5,TimeUnit.SECONDS));
8985
assertTrue(finished.get(5,TimeUnit.SECONDS));
9086
}
87+
88+
@Test
89+
public void responseProcessorChunkedEncodingEarlyCloseTest() throws Exception {
90+
HTTPResponseProcessor hrp = new HTTPResponseProcessor(false);
91+
final SettableListenableFuture<HTTPResponse> header = new SettableListenableFuture<>(false);
92+
final SettableListenableFuture<Boolean> finished = new SettableListenableFuture<>(false);
93+
hrp.addHTTPResponseCallback(new HTTPResponseCallback() {
94+
@Override
95+
public void headersFinished(HTTPResponse hr) {
96+
header.setResult(hr);
97+
}
98+
99+
@Override
100+
public void bodyData(ByteBuffer bb) {
101+
// ignored
102+
}
103+
104+
@Override
105+
public void finished() {
106+
finished.setResult(true);
107+
}
108+
109+
@Override
110+
public void hasError(Throwable t) {
111+
finished.setFailure(t);
112+
header.setFailure(t);
113+
}
114+
115+
@Override
116+
public void websocketData(WSFrame wsf, ByteBuffer bb) {
117+
hasError(new Exception("Unexpected websocket data"));
118+
}});
119+
HTTPResponse hr =
120+
new HTTPResponseBuilder()
121+
.setHeader(HTTPConstants.HTTP_KEY_TRANSFER_ENCODING, "chunked")
122+
.build();
123+
124+
hrp.processData(hr.getMergedByteBuffers());
125+
hrp.processData(HTTPUtils.wrapInChunk(ByteBuffer.wrap(new byte[10])));
126+
hrp.connectionClosed();
127+
128+
assertEquals(hr, header.get(5,TimeUnit.SECONDS));
129+
assertTrue(finished.get(5,TimeUnit.SECONDS));
130+
}
91131
}

0 commit comments

Comments
 (0)