Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
fixed responsechannel handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AZ committed Jan 7, 2017
1 parent 5055256 commit b0c8989
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/main/java/com/iota/iri/service/API.java
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnio.channels.StreamSinkChannel;
import org.xnio.streams.ChannelInputStream;

import com.google.gson.Gson;
Expand Down Expand Up @@ -516,10 +517,25 @@ private void sendResponse(final HttpServerExchange exchange, final AbstractRespo

setupResponseHeaders(exchange);

final int writtenBytes = exchange.getResponseChannel()
.write(ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
exchange.endExchange();
log.debug("Sent {} bytes back in the response.", writtenBytes);
ByteBuffer responseBuf = ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8));
exchange.setResponseContentLength(responseBuf.array().length);
StreamSinkChannel sinkChannel = exchange.getResponseChannel();
sinkChannel.getWriteSetter().set( channel -> {
if (responseBuf.remaining() > 0)
try {
sinkChannel.write(responseBuf);
if (responseBuf.remaining() == 0) {
exchange.endExchange();
}
} catch (IOException e) {
log.error("Error writing response",e);
exchange.endExchange();
}
else {
exchange.endExchange();
}
});
sinkChannel.resumeWrites();
}

private static void setupResponseHeaders(final HttpServerExchange exchange) {
Expand Down

1 comment on commit b0c8989

@davassi
Copy link
Contributor

@davassi davassi commented on b0c8989 Jan 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Please sign in to comment.