2018-09-27 08:47:48 +03:00
|
|
|
package ru.gravit.launchserver.fileserver;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelProgressiveFuture;
|
|
|
|
import io.netty.channel.ChannelProgressiveFutureListener;
|
|
|
|
import io.netty.channel.DefaultFileRegion;
|
|
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
|
|
|
import io.netty.handler.codec.http.DefaultHttpResponse;
|
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
|
|
|
import io.netty.handler.codec.http.HttpChunkedInput;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
|
|
|
import io.netty.handler.codec.http.HttpUtil;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaderValues;
|
|
|
|
import io.netty.handler.codec.http.HttpResponse;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.handler.codec.http.LastHttpContent;
|
|
|
|
import io.netty.handler.ssl.SslHandler;
|
|
|
|
import io.netty.handler.stream.ChunkedFile;
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
|
|
|
import javax.activation.MimetypesFileTypeMap;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.RandomAccessFile;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
2018-10-02 15:20:57 +03:00
|
|
|
import java.nio.file.Path;
|
2018-09-27 08:47:48 +03:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
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";
|
2018-10-02 15:20:57 +03:00
|
|
|
public static final String READ = "r";
|
2018-09-27 08:47:48 +03:00
|
|
|
public static final int HTTP_CACHE_SECONDS = 60;
|
2018-10-02 15:20:57 +03:00
|
|
|
private final Path base;
|
|
|
|
private final boolean fullOut;
|
|
|
|
|
|
|
|
public FileServerHandler(Path base, boolean fullOut) {
|
|
|
|
this.base = base;
|
|
|
|
this.fullOut = fullOut;
|
|
|
|
}
|
|
|
|
|
2018-09-27 08:47:48 +03:00
|
|
|
@Override
|
|
|
|
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
|
|
|
|
if (!request.decoderResult().isSuccess()) {
|
|
|
|
sendError(ctx, BAD_REQUEST);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.method() != GET) {
|
|
|
|
sendError(ctx, METHOD_NOT_ALLOWED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String uri = request.uri();
|
|
|
|
final String path = sanitizeUri(uri);
|
|
|
|
if (path == null) {
|
|
|
|
sendError(ctx, FORBIDDEN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:20:57 +03:00
|
|
|
File file = base.resolve(path).toFile();
|
2018-09-27 08:47:48 +03:00
|
|
|
if (file.isHidden() || !file.exists()) {
|
|
|
|
sendError(ctx, NOT_FOUND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.isDirectory()) {
|
2018-10-02 15:20:57 +03:00
|
|
|
if (fullOut) {
|
|
|
|
if (uri.endsWith("/")) {
|
|
|
|
sendListing(ctx, file, uri);
|
|
|
|
} else {
|
|
|
|
sendRedirect(ctx, uri + '/');
|
|
|
|
}
|
2018-10-03 11:12:48 +03:00
|
|
|
} else sendError(ctx, NOT_FOUND); // can not handle dirs
|
2018-09-27 08:47:48 +03:00
|
|
|
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 {
|
2018-10-02 15:20:57 +03:00
|
|
|
raf = new RandomAccessFile(file, READ);
|
2018-09-27 08:47:48 +03:00
|
|
|
} 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 (HttpUtil.isKeepAlive(request)) {
|
|
|
|
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the initial line and the header.
|
|
|
|
ctx.write(response);
|
|
|
|
|
|
|
|
// Write the content.
|
|
|
|
ChannelFuture sendFileFuture;
|
|
|
|
ChannelFuture lastContentFuture;
|
|
|
|
if (ctx.pipeline().get(SslHandler.class) == null) {
|
|
|
|
sendFileFuture =
|
|
|
|
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
|
|
|
|
// Write the end marker.
|
|
|
|
lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
|
|
|
|
} else {
|
|
|
|
sendFileFuture =
|
|
|
|
ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)),
|
|
|
|
ctx.newProgressivePromise());
|
|
|
|
// HttpChunkedInput will write the end marker (LastHttpContent) for us.
|
|
|
|
lastContentFuture = sendFileFuture;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
|
|
|
|
@Override
|
|
|
|
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
|
|
|
|
if (total < 0) { // total unknown
|
|
|
|
System.err.println(future.channel() + " Transfer progress: " + progress);
|
|
|
|
} else {
|
|
|
|
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void operationComplete(ChannelProgressiveFuture future) {
|
|
|
|
System.err.println(future.channel() + " Transfer complete.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Decide whether to close the connection or not.
|
|
|
|
if (!HttpUtil.isKeepAlive(request)) {
|
2018-10-02 15:51:07 +03:00
|
|
|
lastContentFuture.addListener(new ClosingChannelFutureListener(raf));
|
2018-09-27 08:47:48 +03:00
|
|
|
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
|
|
|
cause.printStackTrace();
|
|
|
|
if (ctx.channel().isActive()) {
|
|
|
|
sendError(ctx, INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Pattern INSECURE_URI = Pattern.compile(".*[<>&\"].*");
|
|
|
|
|
|
|
|
private static String sanitizeUri(String uri) {
|
|
|
|
// Decode the path.
|
|
|
|
try {
|
|
|
|
uri = URLDecoder.decode(uri, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uri.isEmpty() || uri.charAt(0) != '/') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert file separators.
|
2018-10-03 11:12:48 +03:00
|
|
|
uri = uri.replace(File.separatorChar, '/');
|
2018-09-27 08:47:48 +03:00
|
|
|
|
|
|
|
// Simplistic dumb security check.
|
|
|
|
// You will have to do something serious in the production environment.
|
|
|
|
if (uri.contains(File.separator + '.') ||
|
2018-10-02 15:20:57 +03:00
|
|
|
uri.contains('.' + File.separator) ||
|
|
|
|
uri.charAt(0) == '.' || uri.charAt(uri.length() - 1) == '.' ||
|
|
|
|
INSECURE_URI.matcher(uri).matches()) {
|
2018-09-27 08:47:48 +03:00
|
|
|
return null;
|
|
|
|
}
|
2018-10-03 11:12:48 +03:00
|
|
|
return uri.substring(1);
|
2018-09-27 08:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final Pattern ALLOWED_FILE_NAME = Pattern.compile("[^-\\._]?[^<>&\\\"]*");
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
StringBuilder buf = new StringBuilder()
|
2018-10-02 15:20:57 +03:00
|
|
|
.append("<!DOCTYPE html>\r\n")
|
|
|
|
.append("<html><head><meta charset='utf-8' /><title>")
|
|
|
|
.append("Listing of: ")
|
|
|
|
.append(dirPath)
|
|
|
|
.append("</title></head><body>\r\n")
|
2018-09-27 08:47:48 +03:00
|
|
|
|
2018-10-02 15:20:57 +03:00
|
|
|
.append("<h3>Listing of: ")
|
|
|
|
.append(dirPath)
|
|
|
|
.append("</h3>\r\n")
|
2018-09-27 08:47:48 +03:00
|
|
|
|
2018-10-02 15:20:57 +03:00
|
|
|
.append("<ul>")
|
|
|
|
.append("<li><a href=\"../\">..</a></li>\r\n");
|
2018-09-27 08:47:48 +03:00
|
|
|
|
2018-11-08 15:30:16 +03:00
|
|
|
for (File f : dir.listFiles()) {
|
2018-09-27 08:47:48 +03:00
|
|
|
if (f.isHidden() || !f.canRead()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String name = f.getName();
|
|
|
|
if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.append("<li><a href=\"")
|
2018-10-02 15:20:57 +03:00
|
|
|
.append(name)
|
|
|
|
.append("\">")
|
|
|
|
.append(name)
|
|
|
|
.append("</a></li>\r\n");
|
2018-09-27 08:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf.append("</ul></body></html>\r\n");
|
|
|
|
ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
|
|
|
|
response.content().writeBytes(buffer);
|
|
|
|
buffer.release();
|
|
|
|
|
|
|
|
// Close the connection as soon as the error message is sent.
|
|
|
|
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendRedirect(ChannelHandlerContext ctx, String newUri) {
|
|
|
|
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND);
|
|
|
|
response.headers().set(HttpHeaderNames.LOCATION, newUri);
|
|
|
|
|
|
|
|
// Close the connection as soon as the error message is sent.
|
|
|
|
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
|
|
|
|
FullHttpResponse response = new DefaultFullHttpResponse(
|
|
|
|
HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
|
|
|
|
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
|
|
|
|
|
|
|
// Close the connection as soon as the error message is sent.
|
|
|
|
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When file timestamp is the same as what the browser is sending up, send a "304 Not Modified"
|
|
|
|
*
|
2018-11-08 15:30:16 +03:00
|
|
|
* @param ctx Context
|
2018-09-27 08:47:48 +03:00
|
|
|
*/
|
|
|
|
private static void sendNotModified(ChannelHandlerContext ctx) {
|
|
|
|
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
|
|
|
|
setDateHeader(response);
|
|
|
|
|
|
|
|
// Close the connection as soon as the error message is sent.
|
|
|
|
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Date header for the HTTP response
|
|
|
|
*
|
2018-11-08 15:30:16 +03:00
|
|
|
* @param response HTTP response
|
2018-09-27 08:47:48 +03:00
|
|
|
*/
|
|
|
|
private static void setDateHeader(FullHttpResponse response) {
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
|
|
|
|
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
|
|
|
|
|
|
|
|
Calendar time = new GregorianCalendar();
|
|
|
|
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Date and Cache headers for the HTTP Response
|
|
|
|
*
|
2018-11-08 15:30:16 +03:00
|
|
|
* @param response HTTP response
|
|
|
|
* @param fileToCache file to extract content type
|
2018-09-27 08:47:48 +03:00
|
|
|
*/
|
|
|
|
private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
|
|
|
|
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
|
|
|
|
|
|
|
|
// Date header
|
|
|
|
Calendar time = new GregorianCalendar();
|
|
|
|
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
|
|
|
|
|
|
|
|
// Add cache headers
|
|
|
|
time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
|
|
|
|
response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
|
|
|
|
response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
|
|
|
|
response.headers().set(
|
|
|
|
HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the content type header for the HTTP Response
|
|
|
|
*
|
2018-11-08 15:30:16 +03:00
|
|
|
* @param response HTTP response
|
|
|
|
* @param file file to extract content type
|
2018-09-27 08:47:48 +03:00
|
|
|
*/
|
|
|
|
private static void setContentTypeHeader(HttpResponse response, File file) {
|
|
|
|
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
|
|
|
|
response.headers().set(HttpHeaderNames.CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
|
|
|
|
}
|
|
|
|
}
|