mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FIX] Нормальный фикс для FileServer
This commit is contained in:
parent
4a572d0814
commit
6f6cb96080
2 changed files with 39 additions and 20 deletions
|
@ -0,0 +1,26 @@
|
|||
package pro.gravit.launchserver.socket.handlers;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public enum ContentType {
|
||||
NONE {
|
||||
@Override
|
||||
public String forPath(File p) {
|
||||
return null;
|
||||
}
|
||||
|
||||
},
|
||||
NIO {
|
||||
@Override
|
||||
public String forPath(File p) {
|
||||
try {
|
||||
return Files.probeContentType(p.toPath());
|
||||
} catch (Throwable e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public abstract String forPath(File p);
|
||||
}
|
|
@ -6,13 +6,14 @@
|
|||
import io.netty.handler.codec.http.*;
|
||||
import io.netty.handler.stream.ChunkedFile;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import pro.gravit.launchserver.socket.handlers.ContentType;
|
||||
import pro.gravit.utils.helper.VerifyHelper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
@ -25,12 +26,15 @@
|
|||
|
||||
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 SimpleDateFormat dateFormatter;
|
||||
public static final String READ = "r";
|
||||
public static final int HTTP_CACHE_SECONDS = 60;
|
||||
static {
|
||||
dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
|
||||
dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
}
|
||||
public static final int HTTP_CACHE_SECONDS = VerifyHelper.verifyInt(Integer.parseInt(System.getProperty("launcher.fileserver.cachesec", "60")), VerifyHelper.NOT_NEGATIVE, "HttpCache seconds should be positive");
|
||||
private static final boolean OLD_ALGO = Boolean.parseBoolean(System.getProperty("launcher.fileserver.oldalgo", "true"));
|
||||
private static final boolean TYPE_PROBE = Boolean.parseBoolean(System.getProperty("launcher.fileserver.typeprobe", "true"));
|
||||
private static final ContentType TYPE_PROBE = Arrays.stream(ContentType.values()).filter(e -> e.name().toLowerCase(Locale.US).equals(System.getProperty("launcher.fileserver.typeprobe", "nio"))).findFirst().orElse(ContentType.NONE);
|
||||
private final Path base;
|
||||
private final boolean fullOut;
|
||||
private final boolean showHiddenFiles;
|
||||
|
@ -88,7 +92,6 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
|
|||
// 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
|
||||
|
@ -234,11 +237,7 @@ private static void sendNotModified(ChannelHandlerContext ctx) {
|
|||
* @param response HTTP response
|
||||
*/
|
||||
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()));
|
||||
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(new Date(System.currentTimeMillis())));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -248,9 +247,6 @@ private static void setDateHeader(FullHttpResponse response) {
|
|||
* @param fileToCache file to extract content type
|
||||
*/
|
||||
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()));
|
||||
|
@ -270,11 +266,8 @@ private static void setDateAndCacheHeaders(HttpResponse response, File fileToCac
|
|||
* @param file file to extract content type
|
||||
*/
|
||||
private static void setContentTypeHeader(HttpResponse response, File file) {
|
||||
if (TYPE_PROBE)
|
||||
try {
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, Files.probeContentType(file.toPath()));
|
||||
} catch (Throwable e) {
|
||||
// ignore
|
||||
}
|
||||
String contentType = TYPE_PROBE.forPath(file);
|
||||
if (contentType != null)
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue