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,20 +82,19 @@ 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())) {
// Web Gui // Web Gui
if(file.isDirectory() && showFolders){ if (file.isDirectory() && showFolders) {
File indexFile = new File(file, "index.html"); File indexFile = new File(file, "index.html");
// Redirect to index.html // Redirect to index.html
if(redirectToIndex && indexFile.isFile()) { if (redirectToIndex && indexFile.isFile()) {
deliverFile(indexFile, out); deliverFile(indexFile, out);
} }
// Show folder contents // Show folder contents
else if(showFolders){ else if (showFolders) {
out.println("<HTML><BODY><H1>Directory: " + headers.getRequestURL() + "</H1>"); out.println("<HTML><BODY><H1>Directory: " + headers.getRequestURL() + "</H1>");
out.println("<HR><UL>"); out.println("<HR><UL>");
for (String f : file.list()) { for (String f : file.list()) {
@ -114,19 +113,18 @@ 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());
} }
} }
}catch (FileNotFoundException | SecurityException e){ } catch (FileNotFoundException | SecurityException e) {
if(!out.isHeaderSent()) if (!out.isHeaderSent())
out.setResponseStatusCode(404); out.setResponseStatusCode(404);
log.log(Level.WARNING, e.getMessage()); log.log(Level.WARNING, e.getMessage());
out.println("404 Page Not Found: " + headers.getRequestURL()); out.println("404 Page Not Found: " + headers.getRequestURL());
} catch (IOException e){ } catch (IOException e) {
if(!out.isHeaderSent()) if (!out.isHeaderSent())
out.setResponseStatusCode(500); out.setResponseStatusCode(500);
log.log(Level.WARNING, null, e); log.log(Level.WARNING, null, e);
out.println("500 Internal Server Error: "+e.getMessage() ); out.println("500 Internal Server Error: "+e.getMessage() );
@ -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);
@ -169,7 +170,7 @@ public class HttpFilePage implements HttpPage{
fileCache.lastModified = file.lastModified(); fileCache.lastModified = file.lastModified();
} }
return fileCache.hash; return fileCache.hash;
} catch (NoSuchAlgorithmException e){ } catch (NoSuchAlgorithmException e) {
log.log(Level.WARNING, "Unable to generate hash", e); log.log(Level.WARNING, "Unable to generate hash", e);
} }
return ""; return "";