Skip to content

Commit 68d8aa4

Browse files
committed
Fix InputStream chunking w/ Netty 3
1 parent ff4b1fa commit 68d8aa4

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

api/src/main/java/org/asynchttpclient/request/body/generator/InputStreamBodyGenerator.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,45 @@ public State read(ByteBuffer buffer) throws IOException {
7272
chunk = new byte[buffer.remaining() - 10];
7373

7474
int read = -1;
75+
boolean write = false;
7576
try {
7677
read = inputStream.read(chunk);
7778
} catch (IOException ex) {
7879
LOGGER.warn("Unable to read", ex);
7980
}
8081

8182
if (patchNetty3ChunkingIssue) {
82-
if (read == -1) {
83-
// Since we are chunked, we must output extra bytes before considering the input stream closed.
84-
// chunking requires to end the chunking:
85-
// - A Terminating chunk of "0\r\n".getBytes(),
86-
// - Then a separate packet of "\r\n".getBytes()
87-
if (!eof) {
88-
endDataCount++;
89-
90-
if (endDataCount == 1)
91-
buffer.put(ZERO);
92-
else if (endDataCount == 2)
93-
eof = true;
94-
95-
buffer.put(END_PADDING);
96-
}
97-
} else {
83+
if (read >= 0) {
9884
// Netty 3.2.3 doesn't support chunking encoding properly, so we chunk encoding ourself.
9985
buffer.put(Integer.toHexString(read).getBytes());
10086
// Chunking is separated by "<bytesreads>\r\n"
10187
buffer.put(END_PADDING);
10288
buffer.put(chunk, 0, read);
10389
// Was missing the final chunk \r\n.
10490
buffer.put(END_PADDING);
91+
write = true;
92+
93+
} else if (!eof) {
94+
// read == -1)
95+
// Since we are chunked, we must output extra bytes before considering the input stream closed.
96+
// chunking requires to end the chunking:
97+
// - A Terminating chunk of "0\r\n".getBytes(),
98+
// - Then a separate packet of "\r\n".getBytes()
99+
endDataCount++;
100+
101+
if (endDataCount == 1)
102+
buffer.put(ZERO);
103+
else if (endDataCount == 2)
104+
eof = true;
105+
106+
buffer.put(END_PADDING);
107+
write = true;
105108
}
106109
} else if (read > 0) {
107110
buffer.put(chunk, 0, read);
111+
write = true;
108112
}
109-
return read < 0 ? State.Stop : State.Continue;
113+
return write ? State.Continue : State.Stop;
110114
}
111115

112116
public void close() throws IOException {

0 commit comments

Comments
 (0)