use of io.netty.handler.stream.ChunkedWriteHandler in project reactor-netty by reactor.
the class NettyOutboundTest method sendFileWithForceChunkedFileUsesStrategyChunks.
@Test
public void sendFileWithForceChunkedFileUsesStrategyChunks() throws URISyntaxException, IOException {
List<Class<?>> messageWritten = new ArrayList<>(2);
EmbeddedChannel channel = new EmbeddedChannel(// transform the ByteBuf chunks into Strings:
new MessageToMessageEncoder<ByteBuf>() {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
out.add(msg.toString(CharsetUtil.UTF_8));
}
}, // transform the ChunkedFile into ByteBuf chunks:
new ChunkedWriteHandler(), // helps to ensure a ChunkedFile was written outs
new MessageToMessageEncoder<Object>() {
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
messageWritten.add(msg.getClass());
out.add(msg);
}
});
NettyContext mockContext = () -> channel;
NettyOutbound outbound = new NettyOutbound() {
@Override
public NettyContext context() {
return mockContext;
}
@Override
public FileChunkedStrategy getFileChunkedStrategy() {
return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
}
};
Path path = Paths.get(getClass().getResource("/largeFile.txt").toURI());
channel.writeOneOutbound(1);
outbound.sendFileChunked(path, 0, Files.size(path)).then().block();
assertThat(channel.inboundMessages()).isEmpty();
assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
assertThat(channel.outboundMessages()).hasSize(3).element(1).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("1024 mark here ->");
assertThat(channel.outboundMessages()).last().asString().startsWith("<- 1024 mark here").endsWith("End of File");
assertThat(channel.finishAndReleaseAll()).isTrue();
}
use of io.netty.handler.stream.ChunkedWriteHandler in project reactor-netty by reactor.
the class NettyOutboundTest method sendFileWithTlsUsesChunkedFile.
@Test
public void sendFileWithTlsUsesChunkedFile() throws URISyntaxException, SSLException, CertificateException {
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
final SslHandler sslHandler = sslCtx.newHandler(ByteBufAllocator.DEFAULT);
List<Class<?>> messageWritten = new ArrayList<>(2);
List<Object> clearMessages = new ArrayList<>(2);
EmbeddedChannel channel = new EmbeddedChannel(// bytes are encrypted
sslHandler, // capture the chunks unencrypted, transform as Strings:
new MessageToMessageEncoder<ByteBuf>() {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
clearMessages.add(msg.toString(CharsetUtil.UTF_8));
// the encoder will release the buffer, make sure it is retained for SslHandler
out.add(msg.retain());
}
}, // transform the ChunkedFile into ByteBuf chunks:
new ChunkedWriteHandler(), // helps to ensure a ChunkedFile was written outs
new MessageToMessageEncoder<Object>() {
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
messageWritten.add(msg.getClass());
// passing the ChunkedFile through this method releases it, which is undesired
ReferenceCountUtil.retain(msg);
out.add(msg);
}
});
NettyContext mockContext = () -> channel;
NettyOutbound outbound = new NettyOutbound() {
@Override
public NettyContext context() {
return mockContext;
}
@Override
public FileChunkedStrategy getFileChunkedStrategy() {
return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE;
}
};
channel.writeOneOutbound(1);
try {
outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then().block(// TODO investigate why this hangs
Duration.ofSeconds(1));
} catch (IllegalStateException e) {
if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage()))
throw e;
System.err.println(e);
}
assertThat(messageWritten).containsExactly(Integer.class, ChunkedNioFile.class);
assertThat(clearMessages).hasSize(2).element(0).asString().startsWith("This is an UTF-8 file that is larger than 1024 bytes. It contains accents like é. GARBAGE").endsWith("1024 mark here ->");
assertThat(clearMessages).element(1).asString().startsWith("<- 1024 mark here").endsWith("End of File");
assertThat(channel.finishAndReleaseAll()).isTrue();
}
use of io.netty.handler.stream.ChunkedWriteHandler in project vert.x by eclipse.
the class NetServerImpl method initChannel.
protected void initChannel(ChannelPipeline pipeline, boolean ssl) {
if (options.getLogActivity()) {
pipeline.addLast("logging", new LoggingHandler(options.getActivityLogDataFormat()));
}
if (ssl) {
// only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used.
// For large file / sendfile support
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
}
int idleTimeout = options.getIdleTimeout();
int readIdleTimeout = options.getReadIdleTimeout();
int writeIdleTimeout = options.getWriteIdleTimeout();
if (idleTimeout > 0 || readIdleTimeout > 0 || writeIdleTimeout > 0) {
pipeline.addLast("idle", new IdleStateHandler(readIdleTimeout, writeIdleTimeout, idleTimeout, options.getIdleTimeoutUnit()));
}
}
use of io.netty.handler.stream.ChunkedWriteHandler in project benchmark by seelunzi.
the class WebsocketChatServerInitializer method initChannel.
@Override
public void initChannel(SocketChannel ch) throws Exception {
// 2
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(64 * 1024));
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpRequestHandler("/ws"));
pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast(new TextWebSocketFrameHandler());
}
use of io.netty.handler.stream.ChunkedWriteHandler in project benchmark by seelunzi.
the class ChildChannelHandler method initChannel.
@Override
protected void initChannel(SocketChannel e) throws Exception {
// 设置30秒没有读到数据,则触发一个READER_IDLE事件。
// pipeline.addLast(new IdleStateHandler(30, 0, 0));
// HttpServerCodec:将请求和应答消息解码为HTTP消息
e.pipeline().addLast("http-codec", new HttpServerCodec());
// HttpObjectAggregator:将HTTP消息的多个部分合成一条完整的HTTP消息
e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
// ChunkedWriteHandler:向客户端发送HTML5文件
e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
// 在管道中添加我们自己的接收数据实现方法
e.pipeline().addLast("handler", new MyWebSocketServerHandler());
}
Aggregations