Home / Class/ Http2StaticFileServerHandler Class — netty Architecture

Http2StaticFileServerHandler Class — netty Architecture

Architecture documentation for the Http2StaticFileServerHandler class in Http2StaticFileServerHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  0106d702_a5aa_70a0_2d44_3e8c79971862["Http2StaticFileServerHandler"]
  c31daba1_0828_0199_f185_52a24bafc13b["Http2StaticFileServerHandler.java"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|defined in| c31daba1_0828_0199_f185_52a24bafc13b
  f3d3a002_604b_1387_a2bd_93edd96b6d76["channelRead()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| f3d3a002_604b_1387_a2bd_93edd96b6d76
  c3d4de5a_dda3_0bd9_aa70_b9e5ec44b1ef["exceptionCaught()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| c3d4de5a_dda3_0bd9_aa70_b9e5ec44b1ef
  bee797c3_b154_b94c_1693_d12a0b7044ff["String()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| bee797c3_b154_b94c_1693_d12a0b7044ff
  d930130d_166e_d545_8a2f_33a40d608cd0["sendListing()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| d930130d_166e_d545_8a2f_33a40d608cd0
  f018bf1d_420e_60b8_aca3_2d8a7339412e["sendRedirect()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| f018bf1d_420e_60b8_aca3_2d8a7339412e
  7601eca6_4386_67de_d63f_2e414c773a40["sendError()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| 7601eca6_4386_67de_d63f_2e414c773a40
  2e2a061a_4004_0da1_139d_0c0c613f21e9["sendNotModified()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| 2e2a061a_4004_0da1_139d_0c0c613f21e9
  74e3660b_1424_7252_96d9_d4584354600b["setDateHeader()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| 74e3660b_1424_7252_96d9_d4584354600b
  ecbbd3fe_4494_4d38_50f0_f80f99f7efbf["setDateAndCacheHeaders()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| ecbbd3fe_4494_4d38_50f0_f80f99f7efbf
  aa8b1cd3_4459_6b13_7b4b_0e5cc278a7a8["setContentTypeHeader()"]
  0106d702_a5aa_70a0_2d44_3e8c79971862 -->|method| aa8b1cd3_4459_6b13_7b4b_0e5cc278a7a8

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/file/Http2StaticFileServerHandler.java lines 108–380

public class Http2StaticFileServerHandler extends ChannelDuplexHandler {

    public static final String HTTP_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
    public static final String HTTP_DATE_GMT_TIMEZONE = "GMT";
    public static final int HTTP_CACHE_SECONDS = 60;

    private Http2FrameStream stream;

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

Frequently Asked Questions

What is the Http2StaticFileServerHandler class?
Http2StaticFileServerHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http2/file/Http2StaticFileServerHandler.java.
Where is Http2StaticFileServerHandler defined?
Http2StaticFileServerHandler is defined in example/src/main/java/io/netty/example/http2/file/Http2StaticFileServerHandler.java at line 108.

Analyze Your Own Codebase

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

Try Supermodel Free