From eae7b2d6624782086d6911b02dff50eb1f24763a Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 28 Sep 2012 16:58:26 +0900 Subject: [PATCH] [#532] HttpStaticFileServer should generate an index page * Add index page listing and directory redirection --- .../file/HttpStaticFileServerHandler.java | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java index 4f641bbacd..eeaa3d65d5 100644 --- a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java +++ b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java @@ -113,7 +113,8 @@ public class HttpStaticFileServerHandler extends ChannelInboundMessageHandlerAda return; } - final String path = sanitizeUri(request.getUri()); + final String uri = request.getUri(); + final String path = sanitizeUri(uri); if (path == null) { sendError(ctx, FORBIDDEN); return; @@ -124,6 +125,16 @@ public class HttpStaticFileServerHandler extends ChannelInboundMessageHandlerAda sendError(ctx, NOT_FOUND); return; } + + if (file.isDirectory()) { + if (uri.endsWith("/")) { + sendListing(ctx, file); + } else { + sendRedirect(ctx, uri + '/'); + } + return; + } + if (!file.isFile()) { sendError(ctx, FORBIDDEN); return; @@ -195,6 +206,10 @@ public class HttpStaticFileServerHandler extends ChannelInboundMessageHandlerAda } } + if (!uri.startsWith("/")) { + return null; + } + // Convert file separators. uri = uri.replace('/', File.separatorChar); @@ -210,6 +225,58 @@ public class HttpStaticFileServerHandler extends ChannelInboundMessageHandlerAda return System.getProperty("user.dir") + File.separator + uri; } + private static void sendListing(ChannelHandlerContext ctx, File dir) { + HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); + response.setHeader(CONTENT_TYPE, "text/html; charset=UTF-8"); + + StringBuilder buf = new StringBuilder(); + + buf.append("\r\n"); + buf.append(""); + buf.append("Listing of: "); + buf.append(dir.getPath()); + buf.append("\r\n"); + + buf.append("

Listing of: "); + buf.append(dir.getPath()); + buf.append("

\r\n"); + + buf.append("\r\n"); + + response.setContent(Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8)); + + // Close the connection as soon as the error message is sent. + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } + + private static void sendRedirect(ChannelHandlerContext ctx, String newUri) { + HttpResponse response = new DefaultHttpResponse(HTTP_1_1, FOUND); + response.setHeader(LOCATION, newUri); + + // Close the connection as soon as the error message is sent. + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } + private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");