Search in sources :

Example 1 with ChannelProgressiveFuture

use of io.netty.channel.ChannelProgressiveFuture in project netty by netty.

the class HttpStaticFileServerHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    this.request = request;
    if (!request.decoderResult().isSuccess()) {
        sendError(ctx, BAD_REQUEST);
        return;
    }
    if (!GET.equals(request.method())) {
        sendError(ctx, METHOD_NOT_ALLOWED);
        return;
    }
    final boolean keepAlive = HttpUtil.isKeepAlive(request);
    final String uri = request.uri();
    final String path = sanitizeUri(uri);
    if (path == null) {
        sendError(ctx, FORBIDDEN);
        return;
    }
    File file = new File(path);
    if (file.isHidden() || !file.exists()) {
        sendError(ctx, NOT_FOUND);
        return;
    }
    if (file.isDirectory()) {
        if (uri.endsWith("/")) {
            sendListing(ctx, file, uri);
        } else {
            sendRedirect(ctx, uri + '/');
        }
        return;
    }
    if (!file.isFile()) {
        sendError(ctx, FORBIDDEN);
        return;
    }
    // Cache Validation
    String ifModifiedSince = request.headers().get(HttpHeaderNames.IF_MODIFIED_SINCE);
    if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
        Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
        // Only compare up to the second because the datetime format we send to the client
        // does not have milliseconds
        long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
        long fileLastModifiedSeconds = file.lastModified() / 1000;
        if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
            sendNotModified(ctx);
            return;
        }
    }
    RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(file, "r");
    } catch (FileNotFoundException ignore) {
        sendError(ctx, NOT_FOUND);
        return;
    }
    long fileLength = raf.length();
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    HttpUtil.setContentLength(response, fileLength);
    setContentTypeHeader(response, file);
    setDateAndCacheHeaders(response, file);
    if (!keepAlive) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    } else if (request.protocolVersion().equals(HTTP_1_0)) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    // Write the initial line and the header.
    ctx.write(response);
    // Write the content.
    ChannelFuture sendFileFuture;
    ChannelFuture lastContentFuture;
    if (ctx.pipeline().get(SslHandler.class) == null) {
        sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
        // Write the end marker.
        lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    } else {
        sendFileFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());
        // HttpChunkedInput will write the end marker (LastHttpContent) for us.
        lastContentFuture = sendFileFuture;
    }
    sendFileFuture.addListener(new ChannelProgressiveFutureListener() {

        @Override
        public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
            if (total < 0) {
                // total unknown
                System.err.println(future.channel() + " Transfer progress: " + progress);
            } else {
                System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
            }
        }

        @Override
        public void operationComplete(ChannelProgressiveFuture future) {
            System.err.println(future.channel() + " Transfer complete.");
        }
    });
    // Decide whether to close the connection or not.
    if (!keepAlive) {
        // Close the connection when the whole content is written out.
        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChunkedFile(io.netty.handler.stream.ChunkedFile) FileNotFoundException(java.io.FileNotFoundException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture) DefaultFileRegion(io.netty.channel.DefaultFileRegion) Date(java.util.Date) SslHandler(io.netty.handler.ssl.SslHandler) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) RandomAccessFile(java.io.RandomAccessFile) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) RandomAccessFile(java.io.RandomAccessFile) ChunkedFile(io.netty.handler.stream.ChunkedFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) ChannelProgressiveFutureListener(io.netty.channel.ChannelProgressiveFutureListener)

Example 2 with ChannelProgressiveFuture

use of io.netty.channel.ChannelProgressiveFuture in project netty by netty.

the class Http2StaticFileServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof Http2HeadersFrame) {
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) msg;
        this.stream = headersFrame.stream();
        if (!GET.toString().equals(headersFrame.headers().method().toString())) {
            sendError(ctx, METHOD_NOT_ALLOWED);
            return;
        }
        final String uri = headersFrame.headers().path().toString();
        final String path = sanitizeUri(uri);
        if (path == null) {
            sendError(ctx, FORBIDDEN);
            return;
        }
        File file = new File(path);
        if (file.isHidden() || !file.exists()) {
            sendError(ctx, NOT_FOUND);
            return;
        }
        if (file.isDirectory()) {
            if (uri.endsWith("/")) {
                sendListing(ctx, file, uri);
            } else {
                sendRedirect(ctx, uri + '/');
            }
            return;
        }
        if (!file.isFile()) {
            sendError(ctx, FORBIDDEN);
            return;
        }
        // Cache Validation
        CharSequence ifModifiedSince = headersFrame.headers().get(HttpHeaderNames.IF_MODIFIED_SINCE);
        if (ifModifiedSince != null && !ifModifiedSince.toString().isEmpty()) {
            SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
            Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince.toString());
            // Only compare up to the second because the datetime format we send to the client
            // does not have milliseconds
            long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
            long fileLastModifiedSeconds = file.lastModified() / 1000;
            if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
                sendNotModified(ctx);
                return;
            }
        }
        RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");
        } catch (FileNotFoundException ignore) {
            sendError(ctx, NOT_FOUND);
            return;
        }
        long fileLength = raf.length();
        Http2Headers headers = new DefaultHttp2Headers();
        headers.status("200");
        headers.setLong(HttpHeaderNames.CONTENT_LENGTH, fileLength);
        setContentTypeHeader(headers, file);
        setDateAndCacheHeaders(headers, file);
        // Write the initial line and the header.
        ctx.writeAndFlush(new DefaultHttp2HeadersFrame(headers).stream(stream));
        // Write the content.
        ChannelFuture sendFileFuture;
        sendFileFuture = ctx.writeAndFlush(new Http2DataChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192), stream), ctx.newProgressivePromise());
        sendFileFuture.addListener(new ChannelProgressiveFutureListener() {

            @Override
            public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
                if (total < 0) {
                    // total unknown
                    System.err.println(future.channel() + " Transfer progress: " + progress);
                } else {
                    System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
                }
            }

            @Override
            public void operationComplete(ChannelProgressiveFuture future) {
                System.err.println(future.channel() + " Transfer complete.");
            }
        });
    } else {
        // Unsupported message type
        System.out.println("Unsupported message type: " + msg);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) Http2HeadersFrame(io.netty.handler.codec.http2.Http2HeadersFrame) ChunkedFile(io.netty.handler.stream.ChunkedFile) FileNotFoundException(java.io.FileNotFoundException) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture) Date(java.util.Date) DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) RandomAccessFile(java.io.RandomAccessFile) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2DataChunkedInput(io.netty.handler.codec.http2.Http2DataChunkedInput) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) RandomAccessFile(java.io.RandomAccessFile) ChunkedFile(io.netty.handler.stream.ChunkedFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) ChannelProgressiveFutureListener(io.netty.channel.ChannelProgressiveFutureListener)

Example 3 with ChannelProgressiveFuture

use of io.netty.channel.ChannelProgressiveFuture in project async-http-client by AsyncHttpClient.

the class NettyBodyBody method write.

@Override
public void write(final Channel channel, NettyResponseFuture<?> future) {
    Object msg;
    if (body instanceof RandomAccessBody && !ChannelManager.isSslHandlerConfigured(channel.pipeline()) && !config.isDisableZeroCopy() && getContentLength() > 0) {
        msg = new BodyFileRegion((RandomAccessBody) body);
    } else {
        msg = new BodyChunkedInput(body);
        BodyGenerator bg = future.getTargetRequest().getBodyGenerator();
        if (bg instanceof FeedableBodyGenerator && !(bg instanceof ReactiveStreamsBodyGenerator)) {
            final ChunkedWriteHandler chunkedWriteHandler = channel.pipeline().get(ChunkedWriteHandler.class);
            FeedableBodyGenerator.class.cast(bg).setListener(new FeedListener() {

                @Override
                public void onContentAdded() {
                    chunkedWriteHandler.resumeTransfer();
                }

                @Override
                public void onError(Throwable t) {
                }
            });
        }
    }
    channel.write(msg, channel.newProgressivePromise()).addListener(new WriteProgressListener(future, false, getContentLength()) {

        public void operationComplete(ChannelProgressiveFuture cf) {
            closeSilently(body);
            super.operationComplete(cf);
        }
    });
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}
Also used : WriteProgressListener(org.asynchttpclient.netty.request.WriteProgressListener) FeedListener(org.asynchttpclient.request.body.generator.FeedListener) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture) ReactiveStreamsBodyGenerator(org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) BodyGenerator(org.asynchttpclient.request.body.generator.BodyGenerator) ReactiveStreamsBodyGenerator(org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator) FeedableBodyGenerator(org.asynchttpclient.request.body.generator.FeedableBodyGenerator) FeedableBodyGenerator(org.asynchttpclient.request.body.generator.FeedableBodyGenerator) RandomAccessBody(org.asynchttpclient.request.body.RandomAccessBody)

Example 4 with ChannelProgressiveFuture

use of io.netty.channel.ChannelProgressiveFuture in project async-http-client by AsyncHttpClient.

the class NettyInputStreamBody method write.

@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
    final InputStream is = inputStream;
    if (future.isStreamConsumed()) {
        if (is.markSupported())
            is.reset();
        else {
            LOGGER.warn("Stream has already been consumed and cannot be reset");
            return;
        }
    } else {
        future.setStreamConsumed(true);
    }
    channel.write(new ChunkedStream(is), channel.newProgressivePromise()).addListener(new WriteProgressListener(future, false, getContentLength()) {

        public void operationComplete(ChannelProgressiveFuture cf) {
            closeSilently(is);
            super.operationComplete(cf);
        }
    });
    channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT, channel.voidPromise());
}
Also used : WriteProgressListener(org.asynchttpclient.netty.request.WriteProgressListener) InputStream(java.io.InputStream) ChunkedStream(io.netty.handler.stream.ChunkedStream) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture)

Example 5 with ChannelProgressiveFuture

use of io.netty.channel.ChannelProgressiveFuture in project crate by crate.

the class HttpBlobHandler method transferFile.

private ChannelFuture transferFile(final String digest, RandomAccessFile raf, long position, long count) throws IOException {
    Channel channel = ctx.channel();
    final ChannelFuture fileFuture;
    final ChannelFuture endMarkerFuture;
    if (sslEnabled) {
        HttpChunkedInput httpChunkedInput = new HttpChunkedInput(new ChunkedFile(raf, 0, count, HTTPS_CHUNK_SIZE));
        fileFuture = channel.writeAndFlush(httpChunkedInput, ctx.newProgressivePromise());
        // HttpChunkedInput also writes the end marker (LastHttpContent) for us.
        endMarkerFuture = fileFuture;
    } else {
        FileRegion region = new DefaultFileRegion(raf.getChannel(), position, count);
        fileFuture = channel.write(region, ctx.newProgressivePromise());
        // Flushes and sets the ending marker
        endMarkerFuture = channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    }
    fileFuture.addListener(new ChannelProgressiveFutureListener() {

        @Override
        public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {
            LOGGER.debug("transferFile digest={} progress={} total={}", digest, progress, total);
        }

        @Override
        public void operationComplete(ChannelProgressiveFuture future) throws Exception {
            LOGGER.trace("transferFile operationComplete");
        }
    });
    return endMarkerFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) Channel(io.netty.channel.Channel) ChunkedFile(io.netty.handler.stream.ChunkedFile) DefaultFileRegion(io.netty.channel.DefaultFileRegion) FileRegion(io.netty.channel.FileRegion) ChannelProgressiveFuture(io.netty.channel.ChannelProgressiveFuture) DefaultFileRegion(io.netty.channel.DefaultFileRegion) ChannelProgressiveFutureListener(io.netty.channel.ChannelProgressiveFutureListener) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) NotSslRecordException(io.netty.handler.ssl.NotSslRecordException) BlobsDisabledException(io.crate.blob.v2.BlobsDisabledException) MissingHTTPEndpointException(io.crate.blob.exceptions.MissingHTTPEndpointException) ClosedChannelException(java.nio.channels.ClosedChannelException) DigestNotFoundException(io.crate.blob.exceptions.DigestNotFoundException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

ChannelProgressiveFuture (io.netty.channel.ChannelProgressiveFuture)5 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelProgressiveFutureListener (io.netty.channel.ChannelProgressiveFutureListener)3 ChunkedFile (io.netty.handler.stream.ChunkedFile)3 DefaultFileRegion (io.netty.channel.DefaultFileRegion)2 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 RandomAccessFile (java.io.RandomAccessFile)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 WriteProgressListener (org.asynchttpclient.netty.request.WriteProgressListener)2 DigestMismatchException (io.crate.blob.exceptions.DigestMismatchException)1 DigestNotFoundException (io.crate.blob.exceptions.DigestNotFoundException)1 MissingHTTPEndpointException (io.crate.blob.exceptions.MissingHTTPEndpointException)1 BlobsDisabledException (io.crate.blob.v2.BlobsDisabledException)1 Channel (io.netty.channel.Channel)1 FileRegion (io.netty.channel.FileRegion)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1