Home / Function/ channelRead() — netty Function Reference

channelRead() — netty Function Reference

Architecture documentation for the channelRead() function in Http2StaticFileServerHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  f3d3a002_604b_1387_a2bd_93edd96b6d76["channelRead()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862["Http2StaticFileServerHandler"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|defined in| 0106d702_a5aa_70a0_2d44_3e8c79971862
  7601eca6_4386_67de_d63f_2e414c773a40["sendError()"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|calls| 7601eca6_4386_67de_d63f_2e414c773a40
  d930130d_166e_d545_8a2f_33a40d608cd0["sendListing()"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|calls| d930130d_166e_d545_8a2f_33a40d608cd0
  f018bf1d_420e_60b8_aca3_2d8a7339412e["sendRedirect()"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|calls| f018bf1d_420e_60b8_aca3_2d8a7339412e
  2e2a061a_4004_0da1_139d_0c0c613f21e9["sendNotModified()"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|calls| 2e2a061a_4004_0da1_139d_0c0c613f21e9
  aa8b1cd3_4459_6b13_7b4b_0e5cc278a7a8["setContentTypeHeader()"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|calls| aa8b1cd3_4459_6b13_7b4b_0e5cc278a7a8
  ecbbd3fe_4494_4d38_50f0_f80f99f7efbf["setDateAndCacheHeaders()"]
  f3d3a002_604b_1387_a2bd_93edd96b6d76 -->|calls| ecbbd3fe_4494_4d38_50f0_f80f99f7efbf
  style f3d3a002_604b_1387_a2bd_93edd96b6d76 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/file/Http2StaticFileServerHandler.java lines 116–213

    @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) {

Domain

Subdomains

Frequently Asked Questions

What does channelRead() do?
channelRead() is a function in the netty codebase, defined in example/src/main/java/io/netty/example/http2/file/Http2StaticFileServerHandler.java.
Where is channelRead() defined?
channelRead() is defined in example/src/main/java/io/netty/example/http2/file/Http2StaticFileServerHandler.java at line 116.
What does channelRead() call?
channelRead() calls 6 function(s): sendError, sendListing, sendNotModified, sendRedirect, setContentTypeHeader, setDateAndCacheHeaders.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free