Skip to content

Commit f4e637d

Browse files
committed
Root level path fix
1 parent f4294ae commit f4e637d

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/main/java/express/filter/FilterImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void handle(Request req, Response res) {
6262
if (!(REQ_ALL || REQ.equals(requestMethod))) {
6363
return;
6464
} else if (CONTEXT_ALL) {
65+
req.setContext(CONTEXT);
6566
REQUEST.handle(req, res);
6667
return;
6768
}
@@ -87,6 +88,7 @@ public void handle(Request req, Response res) {
8788
return;
8889

8990
// Handle request
91+
req.setContext(CONTEXT);
9092
REQUEST.handle(req, res);
9193
}
9294

src/main/java/express/http/request/Request.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class Request {
4747
private final HashMap<String, String> FORM_QUERYS; // Form Query's (application/x-www-form-urlencoded)
4848

4949
private HashMap<String, String> params; // URL Params, would be added in ExpressFilterImpl
50+
private String context; // Context which matched
5051

5152
{
5253
this.MIDDLEWARE = new HashMap<>();
@@ -386,6 +387,24 @@ public void setParams(HashMap<String, String> params) {
386387
this.params = params;
387388
}
388389

390+
/**
391+
* Returns the corresponding context.
392+
*
393+
* @return The corresponding context.
394+
*/
395+
public String getContext() {
396+
return context;
397+
}
398+
399+
/**
400+
* Set the corresponding context.
401+
*
402+
* @param context The corresponding context.
403+
*/
404+
public void setContext(String context) {
405+
this.context = context;
406+
}
407+
389408
/**
390409
* Return all url-query's.
391410
*

src/main/java/express/middleware/FileProvider.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import express.utils.Status;
77
import express.utils.Utils;
88

9-
import java.io.File;
109
import java.io.IOException;
1110
import java.nio.file.Files;
1211
import java.nio.file.Path;
@@ -20,7 +19,7 @@
2019
* @author Simon Reinisch
2120
* An middleware to provide access to static server-files.
2221
*/
23-
final class FileProvider implements HttpRequest {
22+
public final class FileProvider implements HttpRequest {
2423

2524
private final Logger LOGGER;
2625
private FileProviderOptions OPTIONS;
@@ -32,19 +31,26 @@ final class FileProvider implements HttpRequest {
3231
}
3332

3433
FileProvider(String root, FileProviderOptions options) throws IOException {
35-
File rootDir = new File(root);
34+
Path rootDir = Paths.get(root);
3635

37-
if (!rootDir.exists() || !rootDir.isDirectory())
36+
if (!Files.exists(rootDir) || !Files.isDirectory(rootDir))
3837
throw new IOException(rootDir + " does not exists or isn't an directory.");
3938

40-
this.ROOT = rootDir.getAbsolutePath();
39+
this.ROOT = rootDir.toAbsolutePath().toString();
4140
this.OPTIONS = options;
4241
}
4342

4443
@Override
4544
public void handle(Request req, Response res) {
4645
String path = req.getURI().getPath();
4746

47+
// Check context
48+
String context = req.getContext();
49+
if (path.indexOf(context) == 0) {
50+
path = path.substring(context.length());
51+
}
52+
53+
// If the path is empty try index.html
4854
if (path.length() <= 1)
4955
path = "index.html";
5056

0 commit comments

Comments
 (0)