From 7375f5b63d5d856a68c23afbe9070ff510b0b1e9 Mon Sep 17 00:00:00 2001 From: Chinmay Pendharkar Date: Mon, 7 Oct 2024 13:44:35 +0800 Subject: [PATCH] fix(agent): remove ability to list files in resource jars --- .../org/arl/fjage/connectors/WebServer.java | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/arl/fjage/connectors/WebServer.java b/src/main/java/org/arl/fjage/connectors/WebServer.java index 889c6e65..a826191f 100644 --- a/src/main/java/org/arl/fjage/connectors/WebServer.java +++ b/src/main/java/org/arl/fjage/connectors/WebServer.java @@ -267,19 +267,13 @@ public void add(String context, String resource, WebServerOptions options) { for (URL r : res){ String staticWebResDir = r.toExternalForm(); ContextHandler handler = new ContextHandler(context); + if (options.directoryListed) log.warning("Directory listing is not supported for resources in jar files"); ResourceHandler resHandler = new ResourceHandler(); resHandler.setResourceBase(staticWebResDir); resHandler.setWelcomeFiles(new String[]{ "index.html" }); resHandler.setDirectoriesListed(false); resHandler.setCacheControl(options.cacheControl); resHandler.setEtags(true); - if (options.directoryListed) { - DirectoryHandler dirHandler = new DirectoryHandler(staticWebResDir); - dirHandler.setHandler(resHandler); - handler.setHandler(dirHandler); - } else{ - handler.setHandler(resHandler); - } handler.setHandler(resHandler); staticContexts.put(context, handler); add(handler); @@ -317,21 +311,12 @@ public void add(String context, String resource) { public void add(String context, File dir, WebServerOptions options) { try { ContextHandler handler = new ContextHandler(context); - ResourceHandler resHandler = new ResourceHandler(); + ResourceHandler resHandler = options.directoryListed ? new DirectoryHandler() : new ResourceHandler(); resHandler.setResourceBase(dir.getCanonicalPath()); resHandler.setWelcomeFiles(new String[]{ "index.html" }); - resHandler.setDirectoriesListed(false); resHandler.setCacheControl(options.cacheControl); resHandler.setEtags(true); handler.setHandler(resHandler); - if (options.directoryListed) { - DirectoryHandler dirHandler = new DirectoryHandler(dir.getCanonicalPath()); - dirHandler.setHandler(resHandler); - handler.setHandler(dirHandler); - } else{ - handler.setHandler(resHandler); - } - staticContexts.put(context, handler); add(handler); }catch (IOException ex){ log.warning("Unable to find the directory : " + dir.toString()); @@ -496,19 +481,15 @@ public WebServerOptions directoryListed(boolean directoryListed) { */ private static class DirectoryHandler extends ResourceHandler { - private DirectoryHandler(String resourceBase) { - super(); - setResourceBase(resourceBase); - } - @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (baseRequest.isHandled()) return; - if (target.endsWith("/")) { + File base = getBaseResource().getFile(); + if (base != null && target.endsWith("/")) { if (request.getContentType() != null && request.getContentType().equals("text/plain")) { String path = request.getPathInfo(); if (path == null) path = "/"; - File dir = new File(getBaseResource().getFile(), path); + File dir = new File(base, path); if (dir.isDirectory()) { response.setContentType("text/plain"); response.setStatus(HttpServletResponse.SC_OK);