Home / Function/ channelRead0() — netty Function Reference

channelRead0() — netty Function Reference

Architecture documentation for the channelRead0() function in HttpStaticFileServerHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  d39f8eb8_b352_59dc_d244_4e113cd078df["channelRead0()"]
  02ca82d7_508c_aabe_bb4d_dd1603c38b01["HttpStaticFileServerHandler"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|defined in| 02ca82d7_508c_aabe_bb4d_dd1603c38b01
  e8aaebf5_6ec4_adec_aa31_f32cf1d41e60["sendError()"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|calls| e8aaebf5_6ec4_adec_aa31_f32cf1d41e60
  2d9abdcf_0bde_4318_74f6_8a20cb48fc08["sendListing()"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|calls| 2d9abdcf_0bde_4318_74f6_8a20cb48fc08
  6c5cbb5e_8e83_4646_b043_db847af45914["sendRedirect()"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|calls| 6c5cbb5e_8e83_4646_b043_db847af45914
  33129c23_d788_1580_659d_7761765ab670["sendNotModified()"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|calls| 33129c23_d788_1580_659d_7761765ab670
  b0cb1193_1463_ea80_ea13_6aa7971584f8["setContentTypeHeader()"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|calls| b0cb1193_1463_ea80_ea13_6aa7971584f8
  5f02226e_55b8_c086_1b85_70b1ef80f5af["setDateAndCacheHeaders()"]
  d39f8eb8_b352_59dc_d244_4e113cd078df -->|calls| 5f02226e_55b8_c086_1b85_70b1ef80f5af
  style d39f8eb8_b352_59dc_d244_4e113cd078df fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java lines 115–232

    @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.

Domain

Subdomains

Frequently Asked Questions

What does channelRead0() do?
channelRead0() is a function in the netty codebase, defined in example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java.
Where is channelRead0() defined?
channelRead0() is defined in example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java at line 115.
What does channelRead0() call?
channelRead0() 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