HttpStaticFileServerHandler Class — netty Architecture
Architecture documentation for the HttpStaticFileServerHandler class in HttpStaticFileServerHandler.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 02ca82d7_508c_aabe_bb4d_dd1603c38b01["HttpStaticFileServerHandler"] 1d122dae_f478_d1f7_6e69_1c1c8b5520be["HttpStaticFileServerHandler.java"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|defined in| 1d122dae_f478_d1f7_6e69_1c1c8b5520be d39f8eb8_b352_59dc_d244_4e113cd078df["channelRead0()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| d39f8eb8_b352_59dc_d244_4e113cd078df aee9bb4c_cfe3_2c43_6afd_034b46bbd060["exceptionCaught()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| aee9bb4c_cfe3_2c43_6afd_034b46bbd060 0e1a5727_14de_fa86_3484_ed704c82ce5d["String()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 0e1a5727_14de_fa86_3484_ed704c82ce5d 2d9abdcf_0bde_4318_74f6_8a20cb48fc08["sendListing()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 2d9abdcf_0bde_4318_74f6_8a20cb48fc08 6c5cbb5e_8e83_4646_b043_db847af45914["sendRedirect()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 6c5cbb5e_8e83_4646_b043_db847af45914 e8aaebf5_6ec4_adec_aa31_f32cf1d41e60["sendError()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| e8aaebf5_6ec4_adec_aa31_f32cf1d41e60 33129c23_d788_1580_659d_7761765ab670["sendNotModified()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 33129c23_d788_1580_659d_7761765ab670 387357b5_0623_04d6_d7dd_f0074f835df9["sendAndCleanupConnection()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 387357b5_0623_04d6_d7dd_f0074f835df9 1a5a1970_da05_7dc6_1738_6d81add6a986["setDateHeader()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 1a5a1970_da05_7dc6_1738_6d81add6a986 5f02226e_55b8_c086_1b85_70b1ef80f5af["setDateAndCacheHeaders()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| 5f02226e_55b8_c086_1b85_70b1ef80f5af b0cb1193_1463_ea80_ea13_6aa7971584f8["setContentTypeHeader()"] 02ca82d7_508c_aabe_bb4d_dd1603c38b01 -->|method| b0cb1193_1463_ea80_ea13_6aa7971584f8
Relationship Graph
Source Code
example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java lines 107–422
public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
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 FullHttpRequest request;
@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);
Source
Frequently Asked Questions
What is the HttpStaticFileServerHandler class?
HttpStaticFileServerHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java.
Where is HttpStaticFileServerHandler defined?
HttpStaticFileServerHandler is defined in example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java at line 107.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free