Skip to content

Commit a2211de

Browse files
committed
Two fixes:
- Test bug: server-side handler assuming the PUT content would arrive in a single chunk and ignored the return result from the read() call. - Grizzly bug: TransferListeners were only notified of content when updated and not when completed. So the last delta was missing.
1 parent 7d52ef4 commit a2211de

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

api/src/test/java/org/asynchttpclient/async/TransferListenerTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,13 @@ public void handle(String s, org.eclipse.jetty.server.Request r, HttpServletRequ
7070
}
7171
byte[] bytes = new byte[size];
7272
if (bytes.length > 0) {
73-
httpRequest.getInputStream().read(bytes);
74-
httpResponse.getOutputStream().write(bytes);
73+
int read = 0;
74+
while (read != -1) {
75+
read = httpRequest.getInputStream().read(bytes);
76+
if (read > 0) {
77+
httpResponse.getOutputStream().write(bytes, 0, read);
78+
}
79+
}
7580
}
7681

7782
httpResponse.setStatus(200);
@@ -129,7 +134,7 @@ public void onThrowable(Throwable t) {
129134
assertEquals(response.getStatusCode(), 200);
130135
assertNotNull(hRead.get());
131136
assertNotNull(hSent.get());
132-
assertNotNull(bb.get());
137+
assertNull(bb.get());
133138
assertNull(throwable.get());
134139
} catch (IOException ex) {
135140
fail("Should have timed out");

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/bodyhandler/FileBodyHandler.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,12 @@ public boolean doHandle(final FilterChainContext ctx,
9292

9393
@Override
9494
public void updated(WriteResult result) {
95-
final AsyncHandler handler = context.getHandler();
96-
if (handler != null) {
97-
if (handler instanceof TransferCompletionHandler) {
98-
// WriteResult keeps a track of the total amount written,
99-
// so we need to calculate the delta ourselves.
100-
final long resultTotal = result.getWrittenSize();
101-
final long written = resultTotal - context.getTotalBodyWritten().get();
102-
final long total = context.getTotalBodyWritten().addAndGet(written);
103-
((TransferCompletionHandler) handler).onContentWriteProgress(
104-
written,
105-
total,
106-
requestPacket.getContentLength());
107-
}
108-
}
95+
notifyHandlerIfNeeded(context, requestPacket, result);
96+
}
97+
98+
@Override
99+
public void completed(WriteResult result) {
100+
notifyHandlerIfNeeded(context, requestPacket, result);
109101
}
110102
});
111103
}
@@ -117,6 +109,27 @@ public void updated(WriteResult result) {
117109
// --------------------------------------------------------- Private Methods
118110

119111

112+
private static void notifyHandlerIfNeeded(final HttpTransactionContext context,
113+
final HttpRequestPacket requestPacket,
114+
final WriteResult writeResult) {
115+
final AsyncHandler handler = context.getHandler();
116+
if (handler != null) {
117+
if (handler instanceof TransferCompletionHandler) {
118+
// WriteResult keeps a track of the total amount written,
119+
// so we need to calculate the delta ourselves.
120+
final long resultTotal = writeResult.getWrittenSize();
121+
final long written =
122+
(resultTotal - context.getTotalBodyWritten().get());
123+
final long total = context.getTotalBodyWritten().addAndGet(written);
124+
((TransferCompletionHandler) handler).onContentWriteProgress(
125+
written,
126+
total,
127+
requestPacket.getContentLength());
128+
}
129+
}
130+
}
131+
132+
120133
private static boolean configSendFileSupport() {
121134
return !((System.getProperty("os.name").equalsIgnoreCase("linux")
122135
&& !linuxSendFileSupported())

0 commit comments

Comments
 (0)