FileServer output modified.

This commit is contained in:
zaxar163 2018-09-26 20:31:47 +03:00
parent b139f3e69f
commit 11c5ab5464
2 changed files with 15 additions and 10 deletions

View file

@ -14,10 +14,12 @@ public class FileInitializer extends ChannelInitializer<SocketChannel> {
private final SslContext sslCtx;
private final File base;
private final boolean outDirs;
public FileInitializer(SslContext sslCtx, File base) {
public FileInitializer(SslContext sslCtx, File base, boolean outDirs) {
this.sslCtx = sslCtx;
this.base = base;
this.outDirs = outDirs;
}
@Override
@ -29,6 +31,6 @@ public void initChannel(SocketChannel ch) {
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new FileServerHandler(base));
pipeline.addLast(new FileServerHandler(base, outDirs));
}
}

View file

@ -42,15 +42,18 @@
import static io.netty.handler.codec.http.HttpMethod.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.*;
public class FileServerHandler 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 final File base;
private final boolean fullOut;
public FileServerHandler(File base) {
public FileServerHandler(File base, boolean fullOut) {
this.base = base;
this.fullOut = fullOut;
}
@Override
@ -79,11 +82,13 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
}
if (file.isDirectory()) {
if (uri.endsWith("/")) {
sendListing(ctx, file, uri);
} else {
sendRedirect(ctx, uri + '/');
}
if (fullOut) {
if (uri.endsWith("/")) {
sendListing(ctx, file, uri);
} else {
sendRedirect(ctx, uri + '/');
}
} else sendError(ctx, FORBIDDEN);
return;
}
@ -207,8 +212,6 @@ private static String sanitizeUri(String uri) {
private static final Pattern ALLOWED_FILE_NAME = Pattern.compile("[^-\\._]?[^<>&\\\"]*");
// TODO rewrite if needed
private static void sendListing(ChannelHandlerContext ctx, File dir, String dirPath) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");