Some fixes in FilePage

This commit is contained in:
Ziver Koc 2020-10-17 23:21:26 +02:00
parent 23090d2bd5
commit e116f999c5

View file

@ -82,8 +82,7 @@ public class HttpFilePage implements HttpPage{
// Is the root only one file or a folder // Is the root only one file or a folder
if (resource_root.isFile()) { if (resource_root.isFile()) {
deliverFileWithCache(headers, resource_root, out); deliverFileWithCache(headers, resource_root, out);
} } else { // Resource root is a folder
else { // Resource root is a folder
File file = new File(resource_root, File file = new File(resource_root,
headers.getRequestURL()); headers.getRequestURL());
if (file.getCanonicalPath().startsWith(resource_root.getCanonicalPath())) { if (file.getCanonicalPath().startsWith(resource_root.getCanonicalPath())) {
@ -114,8 +113,7 @@ public class HttpFilePage implements HttpPage{
else { else {
deliverFileWithCache(headers, file, out); deliverFileWithCache(headers, file, out);
} }
} } else {
else {
throw new SecurityException("File is outside of root directory: root=" + resource_root.getAbsolutePath() + " file=" + file.getAbsolutePath()); throw new SecurityException("File is outside of root directory: root=" + resource_root.getAbsolutePath() + " file=" + file.getAbsolutePath());
} }
} }
@ -136,15 +134,18 @@ public class HttpFilePage implements HttpPage{
private void deliverFileWithCache(HttpHeader headers, File file, HttpPrintStream out) throws IOException { private void deliverFileWithCache(HttpHeader headers, File file, HttpPrintStream out) throws IOException {
String eTag = getFileHash(file); String eTag = getFileHash(file);
out.setHeader("Cache-Control", "max-age=" + MAX_CACHE_AGE_SECONDS); out.setHeader("Cache-Control", "max-age=" + MAX_CACHE_AGE_SECONDS);
if (eTag != null) {
out.setHeader("ETag", "\"" + eTag + "\""); out.setHeader("ETag", "\"" + eTag + "\"");
if (eTag != null && headers.getHeader("If-None-Match") != null && if (headers.getHeader("If-None-Match") != null &&
eTag.equals(StringUtil.trimQuotes(headers.getHeader("If-None-Match")))) { // File has not changed eTag.equals(StringUtil.trimQuotes(headers.getHeader("If-None-Match")))) { // File has not changed
out.setResponseStatusCode(304); out.setResponseStatusCode(304);
} else { } else {
deliverFile(file, out); deliverFile(file, out);
} }
} }
}
private void deliverFile(File file, HttpPrintStream out) throws IOException { private void deliverFile(File file, HttpPrintStream out) throws IOException {
String fileExt = FileUtil.getFileExtension(file); String fileExt = FileUtil.getFileExtension(file);