Skip to content

Commit

Permalink
NullPointerException in UndertowChunkedStream fix #3409
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed May 12, 2024
1 parent b7f7761 commit ea3298c
Showing 1 changed file with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package io.jooby.internal.undertow;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ReadableByteChannel;

import org.xnio.IoUtils;
Expand Down Expand Up @@ -53,26 +53,30 @@ public void send(

@Override
public void run() {
ByteBuffer buffer = pooled.getBuffer();
try {
buffer.clear();
int count = source.read(buffer);
if (count == -1 || (len != -1 && total >= len)) {
done();
callback.onComplete(exchange, sender);
} else {
total += count;
buffer.flip();
if (len > 0) {
if (total > len) {
long limit = count - (total - len);
buffer.limit((int) limit);
if (pooled != null && pooled.isOpen()) {
var buffer = pooled.getBuffer();
try {
buffer.clear();
int count = source.read(buffer);
if (count == -1 || (len != -1 && total >= len)) {
done();
callback.onComplete(exchange, sender);
} else {
total += count;
buffer.flip();
if (len > 0) {
if (total > len) {
long limit = count - (total - len);
buffer.limit((int) limit);
}
}
sender.send(buffer, this);
}
sender.send(buffer, this);
} catch (IOException ex) {
onException(exchange, sender, ex);
}
} catch (IOException ex) {
onException(exchange, sender, ex);
} else {
onException(exchange, sender, new ClosedChannelException());
}
}

Expand All @@ -88,13 +92,21 @@ public void onComplete(final HttpServerExchange exchange, final Sender sender) {
@Override
public void onException(
final HttpServerExchange exchange, final Sender sender, final IOException ex) {
done();
callback.onException(exchange, sender, ex);
try {
callback.onException(exchange, sender, ex);
} finally {
done();
}
}

private void done() {
pooled.close();
pooled = null;
IoUtils.safeClose(source);
if (pooled != null) {
try {
pooled.close();
IoUtils.safeClose(source);
} finally {
pooled = null;
}
}
}
}

0 comments on commit ea3298c

Please sign in to comment.