Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static file routing #12

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions framework/src/play/mvc/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ public static void prependRoute(String method, String path, String action, Strin
*/
static void parse(VirtualFile routeFile, String prefix) {
String fileAbsolutePath = routeFile.getRealFile().getAbsolutePath();
int lineNumber = 0;
String content = routeFile.contentAsString();
if (content.indexOf("${") > -1 || content.indexOf("#{") > -1) {
content = TemplateLoader.load(routeFile).render(new HashMap<String, Object>());
}
parse(content, prefix, fileAbsolutePath);
}

static void parse(String content, String prefix, String fileAbsolutePath) {
int lineNumber = 0;
for (String line : content.split("\n")) {
lineNumber++;
line = line.trim().replaceAll("\\s+", " ");
Expand Down Expand Up @@ -341,7 +345,7 @@ public static String reverseWithCheck(String name, VirtualFile file, boolean abs
}
return reverse(file, absolute);
}

public static ActionDefinition reverse(String action, Map<String, Object> args) {
if (action.startsWith("controllers.")) {
action = action.substring(12);
Expand Down Expand Up @@ -528,6 +532,7 @@ public static class Route {
Pattern actionPattern;
List<String> actionArgs = new ArrayList<String>();
String staticDir;
boolean staticFile;
Pattern pattern;
Pattern hostPattern;
List<Arg> args = new ArrayList<Arg>();
Expand All @@ -544,8 +549,7 @@ public static class Route {
public void compute() {
this.host = "";
this.hostPattern = new Pattern(".*");
// staticDir
if (action.startsWith("staticDir:")) {
if (action.startsWith("staticDir:") || action.startsWith("staticFile:")) {
// Is there is a host argument, append it.
if (!path.startsWith("/")) {
String p = this.path;
Expand All @@ -560,12 +564,19 @@ public void compute() {
Logger.warn("Static route only support GET method");
return;
}
}
// staticDir
if (action.startsWith("staticDir:")) {
if (!this.path.endsWith("/") && !this.path.equals("/")) {
Logger.warn("The path for a staticDir route must end with / (%s)", this);
this.path += "/";
}
this.pattern = new Pattern("^" + path + "({resource}.*)$");
this.staticDir = action.substring("staticDir:".length());
} else if (action.startsWith("staticFile:")) {
this.pattern = new Pattern("^" + path + "$");
this.staticFile = true;
this.staticDir = action.substring("staticFile:".length());
} else {
// URL pattern
// Is there is a host argument, append it.
Expand Down Expand Up @@ -695,12 +706,16 @@ public Map<String, String> matches(String method, String path, String accept, St
if (matcher.matches() && contains(accept) && hostMatches) {
// Static dir
if (staticDir != null) {
String resource = matcher.group("resource");
String resource = null;
if (!staticFile) {
resource = matcher.group("resource");
}
try {
String root = new File(staticDir).getCanonicalPath();
String child = new File(staticDir + "/" + resource).getCanonicalPath();
String childResourceName = staticDir + (staticFile ? "" : "/" + resource);
String child = new File(childResourceName).getCanonicalPath();
if (child.startsWith(root)) {
throw new RenderStatic(staticDir + "/" + resource);
throw new RenderStatic(childResourceName);
}
} catch (IOException e) {
}
Expand Down