Skip to content

Commit f5ee206

Browse files
committed
Clean up Assets don't work on HTTPS with Netty #621
1 parent 314369f commit f5ee206

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

jooby-netty/src/main/java/org/jooby/internal/netty/NettyResponse.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import static io.netty.channel.ChannelFutureListener.CLOSE;
2222

23-
import java.io.IOException;
2423
import java.io.InputStream;
2524
import java.nio.ByteBuffer;
2625
import java.nio.channels.FileChannel;
@@ -51,7 +50,6 @@
5150
import io.netty.handler.codec.http.LastHttpContent;
5251
import io.netty.handler.codec.http2.HttpConversionUtil;
5352
import io.netty.handler.ssl.SslHandler;
54-
import io.netty.handler.stream.ChunkedFile;
5553
import io.netty.handler.stream.ChunkedNioFile;
5654
import io.netty.handler.stream.ChunkedStream;
5755
import io.netty.handler.stream.ChunkedWriteHandler;
@@ -147,10 +145,7 @@ public void send(final InputStream stream) throws Exception {
147145
ctx.channel().attr(NettyRequest.NEED_FLUSH).set(false);
148146

149147
// add chunker
150-
ChannelPipeline pipeline = ctx.pipeline();
151-
if (pipeline.get("chunker") == null) {
152-
pipeline.addAfter("codec", "chunker", new ChunkedWriteHandler());
153-
}
148+
chunkHandler(ctx.pipeline());
154149

155150
// group all write
156151
ctx.channel().eventLoop().execute(() -> {
@@ -190,28 +185,32 @@ public void send(final FileChannel channel, final long offset, final long count)
190185
ChannelHandlerContext ctx = this.ctx;
191186
ctx.channel().attr(NettyRequest.NEED_FLUSH).set(false);
192187

193-
// add chunker
194188
ChannelPipeline pipeline = ctx.pipeline();
195-
if (pipeline.get("chunker") == null) {
196-
pipeline.addAfter("codec", "chunker", new ChunkedWriteHandler());
197-
}
189+
boolean ssl = pipeline.get(SslHandler.class) != null;
198190

199-
// Create the cunked input here already, to properly handle the IOException
200-
final HttpChunkedInput chunkedInput = new HttpChunkedInput(new ChunkedNioFile(channel, offset, count, 8192));
191+
if (ssl) {
192+
// add chunker
193+
chunkHandler(pipeline);
201194

202-
ctx.channel().eventLoop().execute(() -> {
203-
// send headers
204-
ctx.write(rsp);
195+
// Create the chunked input here already, to properly handle the IOException
196+
HttpChunkedInput chunkedInput = new HttpChunkedInput(
197+
new ChunkedNioFile(channel, offset, count, bufferSize));
205198

206-
// For SSL, we cannot send a file region
207-
if (ctx.pipeline().get(SslHandler.class) == null)
208-
{
199+
ctx.channel().eventLoop().execute(() -> {
200+
// send headers
201+
ctx.write(rsp);
202+
// chunked file
203+
keepAlive(ctx.writeAndFlush(chunkedInput));
204+
});
205+
} else {
206+
ctx.channel().eventLoop().execute(() -> {
207+
// send headers
208+
ctx.write(rsp);
209+
// file region
209210
ctx.write(new DefaultFileRegion(channel, offset, count));
210211
keepAlive(ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT));
211-
} else {
212-
keepAlive(ctx.writeAndFlush(chunkedInput));
213-
}
214-
});
212+
});
213+
}
215214

216215
committed = true;
217216

@@ -298,4 +297,10 @@ public void reset() {
298297
status = HttpResponseStatus.OK;
299298
}
300299

300+
private void chunkHandler(final ChannelPipeline pipeline) {
301+
if (pipeline.get("chunker") == null) {
302+
pipeline.addAfter("codec", "chunker", new ChunkedWriteHandler());
303+
}
304+
}
305+
301306
}

jooby-netty/src/test/java/org/jooby/internal/netty/NettyResponseTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121
import java.util.Optional;
2222

23-
import org.apache.http.impl.bootstrap.SSLServerSetupHandler;
2423
import org.jooby.test.MockUnit;
2524
import org.jooby.test.MockUnit.Block;
2625
import org.junit.Test;
@@ -603,14 +602,10 @@ public void sendFileChannel() throws Exception {
603602
})
604603
.expect(unit -> {
605604
ChannelPipeline pipeline = unit.mock(ChannelPipeline.class);
606-
expect(pipeline.get("chunker")).andReturn(null);
607605
expect(pipeline.get(SslHandler.class)).andReturn(null);
608-
expect(pipeline.addAfter(eq("codec"), eq("chunker"), isA(ChunkedWriteHandler.class)))
609-
.andReturn(pipeline);
610606

611607
ChannelHandlerContext ctx = unit.get(ChannelHandlerContext.class);
612608
expect(ctx.pipeline()).andReturn(pipeline);
613-
expect(ctx.pipeline()).andReturn(pipeline);
614609
})
615610
.expect(noKeepAliveNoLen)
616611
.run(unit -> {
@@ -688,7 +683,6 @@ public void sendFileChannelSSL() throws Exception {
688683

689684
ChannelHandlerContext ctx = unit.get(ChannelHandlerContext.class);
690685
expect(ctx.pipeline()).andReturn(pipeline);
691-
expect(ctx.pipeline()).andReturn(pipeline);
692686
})
693687
.expect(noKeepAliveNoLen)
694688
.run(unit -> {
@@ -751,14 +745,10 @@ public void sendFileChannelNoLen() throws Exception {
751745
})
752746
.expect(unit -> {
753747
ChannelPipeline pipeline = unit.mock(ChannelPipeline.class);
754-
expect(pipeline.get("chunker")).andReturn(null);
755748
expect(pipeline.get(SslHandler.class)).andReturn(null);
756-
expect(pipeline.addAfter(eq("codec"), eq("chunker"), isA(ChunkedWriteHandler.class)))
757-
.andReturn(pipeline);
758749

759750
ChannelHandlerContext ctx = unit.get(ChannelHandlerContext.class);
760751
expect(ctx.pipeline()).andReturn(pipeline);
761-
expect(ctx.pipeline()).andReturn(pipeline);
762752
})
763753
.expect(setNeedFlush)
764754
.expect(noKeepAliveNoLen)

0 commit comments

Comments
 (0)