Skip to content

Commit

Permalink
Root level path fix
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Apr 26, 2018
1 parent f4294ae commit f4e637d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/express/filter/FilterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -87,6 +88,7 @@ public void handle(Request req, Response res) {
return;

// Handle request
req.setContext(CONTEXT);
REQUEST.handle(req, res);
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/express/http/request/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Request {
private final HashMap<String, String> FORM_QUERYS; // Form Query's (application/x-www-form-urlencoded)

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

{
this.MIDDLEWARE = new HashMap<>();
Expand Down Expand Up @@ -386,6 +387,24 @@ public void setParams(HashMap<String, String> 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.
*
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/express/middleware/FileProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -32,19 +31,26 @@ 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;
}

@Override
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";

Expand Down

0 comments on commit f4e637d

Please sign in to comment.