From f4e637d97fde7b3572791777b3979bbe2745edac Mon Sep 17 00:00:00 2001 From: Simonwep Date: Thu, 26 Apr 2018 18:15:22 +0200 Subject: [PATCH] Root level path fix --- src/main/java/express/filter/FilterImpl.java | 2 ++ .../java/express/http/request/Request.java | 19 +++++++++++++++++++ .../java/express/middleware/FileProvider.java | 16 +++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/express/filter/FilterImpl.java b/src/main/java/express/filter/FilterImpl.java index 953581e..7b7eebd 100644 --- a/src/main/java/express/filter/FilterImpl.java +++ b/src/main/java/express/filter/FilterImpl.java @@ -62,6 +62,7 @@ public void handle(Request req, Response res) { if (!(REQ_ALL || REQ.equals(requestMethod))) { return; } else if (CONTEXT_ALL) { + req.setContext(CONTEXT); REQUEST.handle(req, res); return; } @@ -87,6 +88,7 @@ public void handle(Request req, Response res) { return; // Handle request + req.setContext(CONTEXT); REQUEST.handle(req, res); } diff --git a/src/main/java/express/http/request/Request.java b/src/main/java/express/http/request/Request.java index b0d4eba..0f0c09d 100644 --- a/src/main/java/express/http/request/Request.java +++ b/src/main/java/express/http/request/Request.java @@ -47,6 +47,7 @@ public class Request { private final HashMap FORM_QUERYS; // Form Query's (application/x-www-form-urlencoded) private HashMap params; // URL Params, would be added in ExpressFilterImpl + private String context; // Context which matched { this.MIDDLEWARE = new HashMap<>(); @@ -386,6 +387,24 @@ public void setParams(HashMap params) { this.params = params; } + /** + * Returns the corresponding context. + * + * @return The corresponding context. + */ + public String getContext() { + return context; + } + + /** + * Set the corresponding context. + * + * @param context The corresponding context. + */ + public void setContext(String context) { + this.context = context; + } + /** * Return all url-query's. * diff --git a/src/main/java/express/middleware/FileProvider.java b/src/main/java/express/middleware/FileProvider.java index be0e9a3..b8b13fe 100644 --- a/src/main/java/express/middleware/FileProvider.java +++ b/src/main/java/express/middleware/FileProvider.java @@ -6,7 +6,6 @@ import express.utils.Status; import express.utils.Utils; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -20,7 +19,7 @@ * @author Simon Reinisch * An middleware to provide access to static server-files. */ -final class FileProvider implements HttpRequest { +public final class FileProvider implements HttpRequest { private final Logger LOGGER; private FileProviderOptions OPTIONS; @@ -32,12 +31,12 @@ final class FileProvider implements HttpRequest { } FileProvider(String root, FileProviderOptions options) throws IOException { - File rootDir = new File(root); + Path rootDir = Paths.get(root); - if (!rootDir.exists() || !rootDir.isDirectory()) + if (!Files.exists(rootDir) || !Files.isDirectory(rootDir)) throw new IOException(rootDir + " does not exists or isn't an directory."); - this.ROOT = rootDir.getAbsolutePath(); + this.ROOT = rootDir.toAbsolutePath().toString(); this.OPTIONS = options; } @@ -45,6 +44,13 @@ final class FileProvider implements HttpRequest { public void handle(Request req, Response res) { String path = req.getURI().getPath(); + // Check context + String context = req.getContext(); + if (path.indexOf(context) == 0) { + path = path.substring(context.length()); + } + + // If the path is empty try index.html if (path.length() <= 1) path = "index.html";