Search in sources :

Example 1 with ChunkedNioFile

use of io.netty.handler.stream.ChunkedNioFile in project java-in-action by xinghalo.

the class HttpRequestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    if (wsUri.equalsIgnoreCase(request.getUri())) {
        ctx.fireChannelRead(request.retain());
    } else {
        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }
        RandomAccessFile file = new RandomAccessFile(INDEX, "r");
        HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
        boolean keepAlive = HttpHeaders.isKeepAlive(request);
        if (keepAlive) {
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length());
            response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        }
        ctx.write(response);
        if (ctx.pipeline().get(SslHandler.class) == null) {
            ctx.write(new DefaultFileRegion(file.getChannel(), 0, file.length()));
        } else {
            ctx.write(new ChunkedNioFile(file.getChannel()));
        }
        ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        if (!keepAlive) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ChunkedNioFile(io.netty.handler.stream.ChunkedNioFile) SslHandler(io.netty.handler.ssl.SslHandler)

Example 2 with ChunkedNioFile

use of io.netty.handler.stream.ChunkedNioFile in project benchmark by seelunzi.

the class HttpRequestHandler method messageReceived.

@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    // 如果请求是 WebSocket 升级,递增引用计数器(保留)并且将它传递给在 ChannelPipeline 中的下个 ChannelInboundHandler
    if (wsUri.equalsIgnoreCase(request.getUri())) {
        ctx.fireChannelRead(request.retain());
    } else {
        // 处理符合 HTTP 1.1的 “100 Continue” 请求
        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }
        // 读取默认的 WebsocketChatClient.html 页面
        RandomAccessFile file = new RandomAccessFile(INDEX, "r");
        HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
        boolean keepAlive = HttpHeaders.isKeepAlive(request);
        // 判断 keepalive 是否在请求头里面
        if (keepAlive) {
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length());
            response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        }
        // 写 HttpResponse 到客户端
        ctx.write(response);
        // 写 index.html 到客户端,判断 SslHandler 是否在 ChannelPipeline 来决定是使用 DefaultFileRegion 还是 ChunkedNioFile
        if (ctx.pipeline().get(SslHandler.class) == null) {
            ctx.write(new DefaultFileRegion(file.getChannel(), 0, file.length()));
        } else {
            ctx.write(new ChunkedNioFile(file.getChannel()));
        }
        ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        if (!keepAlive) {
            // 如果 keepalive 没有要求,当写完成时,关闭 Channel
            future.addListener(ChannelFutureListener.CLOSE);
        }
        file.close();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ChunkedNioFile(io.netty.handler.stream.ChunkedNioFile) SslHandler(io.netty.handler.ssl.SslHandler)

Example 3 with ChunkedNioFile

use of io.netty.handler.stream.ChunkedNioFile in project async-http-client by AsyncHttpClient.

the class NettyFileBody method write.

@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
    @SuppressWarnings("resource") FileChannel // netty will close the FileChannel
    fileChannel = new RandomAccessFile(file, "r").getChannel();
    boolean noZeroCopy = ChannelManager.isSslHandlerConfigured(channel.pipeline()) || config.isDisableZeroCopy();
    Object body = noZeroCopy ? new ChunkedNioFile(fileChannel, offset, length, config.getChunkedFileChunkSize()) : new DefaultFileRegion(fileChannel, offset, length);
    channel.write(body, channel.newProgressivePromise()).addListener(new WriteProgressListener(future, false, length));
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}
Also used : WriteProgressListener(org.asynchttpclient.netty.request.WriteProgressListener) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) ChunkedNioFile(io.netty.handler.stream.ChunkedNioFile) DefaultFileRegion(io.netty.channel.DefaultFileRegion)

Aggregations

ChunkedNioFile (io.netty.handler.stream.ChunkedNioFile)3 RandomAccessFile (java.io.RandomAccessFile)3 SslHandler (io.netty.handler.ssl.SslHandler)2 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 FileChannel (java.nio.channels.FileChannel)1 WriteProgressListener (org.asynchttpclient.netty.request.WriteProgressListener)1