|
20 | 20 |
|
21 | 21 | import static io.netty.channel.ChannelFutureListener.CLOSE; |
22 | 22 |
|
23 | | -import java.io.IOException; |
24 | 23 | import java.io.InputStream; |
25 | 24 | import java.nio.ByteBuffer; |
26 | 25 | import java.nio.channels.FileChannel; |
|
51 | 50 | import io.netty.handler.codec.http.LastHttpContent; |
52 | 51 | import io.netty.handler.codec.http2.HttpConversionUtil; |
53 | 52 | import io.netty.handler.ssl.SslHandler; |
54 | | -import io.netty.handler.stream.ChunkedFile; |
55 | 53 | import io.netty.handler.stream.ChunkedNioFile; |
56 | 54 | import io.netty.handler.stream.ChunkedStream; |
57 | 55 | import io.netty.handler.stream.ChunkedWriteHandler; |
@@ -147,10 +145,7 @@ public void send(final InputStream stream) throws Exception { |
147 | 145 | ctx.channel().attr(NettyRequest.NEED_FLUSH).set(false); |
148 | 146 |
|
149 | 147 | // 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()); |
154 | 149 |
|
155 | 150 | // group all write |
156 | 151 | ctx.channel().eventLoop().execute(() -> { |
@@ -190,28 +185,32 @@ public void send(final FileChannel channel, final long offset, final long count) |
190 | 185 | ChannelHandlerContext ctx = this.ctx; |
191 | 186 | ctx.channel().attr(NettyRequest.NEED_FLUSH).set(false); |
192 | 187 |
|
193 | | - // add chunker |
194 | 188 | 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; |
198 | 190 |
|
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); |
201 | 194 |
|
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)); |
205 | 198 |
|
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 |
209 | 210 | ctx.write(new DefaultFileRegion(channel, offset, count)); |
210 | 211 | keepAlive(ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)); |
211 | | - } else { |
212 | | - keepAlive(ctx.writeAndFlush(chunkedInput)); |
213 | | - } |
214 | | - }); |
| 212 | + }); |
| 213 | + } |
215 | 214 |
|
216 | 215 | committed = true; |
217 | 216 |
|
@@ -298,4 +297,10 @@ public void reset() { |
298 | 297 | status = HttpResponseStatus.OK; |
299 | 298 | } |
300 | 299 |
|
| 300 | + private void chunkHandler(final ChannelPipeline pipeline) { |
| 301 | + if (pipeline.get("chunker") == null) { |
| 302 | + pipeline.addAfter("codec", "chunker", new ChunkedWriteHandler()); |
| 303 | + } |
| 304 | + } |
| 305 | + |
301 | 306 | } |
0 commit comments