Skip to content

Commit

Permalink
[playframework#603] Fix matching host in routes
Browse files Browse the repository at this point in the history
  • Loading branch information
pepite committed Aug 17, 2011
1 parent ba82ff0 commit e1e2424
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
26 changes: 12 additions & 14 deletions framework/src/play/mvc/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ public static void detectChanges(String prefix) {
public static void routeOnlyStatic(Http.Request request) {
for (Route route : routes) {
try {
String format = request.format;
String host = request.host;
if (route.matches(request.method, request.path, format, host) != null) {
if (route.matches(request.method, request.path, request.format, request.domain) != null) {
break;
}
} catch (Throwable t) {
Expand Down Expand Up @@ -264,9 +262,7 @@ public static Route route(Http.Request request) {
}
}
for (Route route : routes) {
String format = request.format;
String host = request.host;
Map<String, String> args = route.matches(request.method, request.path, format, host);
Map<String, String> args = route.matches(request.method, request.path, request.format, request.domain);
if (args != null) {
request.routeArgs = args;
request.action = route.action;
Expand Down Expand Up @@ -363,7 +359,9 @@ public static String reverse(VirtualFile file, boolean absolute) {
String base = Http.Request.current() == null ? Play.configuration.getProperty("application.baseUrl", "application.baseUrl") : Http.Request.current().getBase();
if (!StringUtils.isEmpty(route.host)) {
// Compute the host
to = (isSecure ? "https://" : "http://") + route.host + to;
int port = Http.Request.current() == null ? 80 : Http.Request.current().get().port;
String host = (port != 80 && port != 443) ? route.host + ":" + port : route.host;
to = (isSecure ? "https://" : "http://") + host + to;
} else {
to = base + to;
}
Expand Down Expand Up @@ -797,10 +795,10 @@ public Map<String, String> matches(String method, String path, String accept) {
* @param method GET/POST/etc.
* @param path Part after domain and before query-string. Starts with a "/".
* @param accept Format, e.g. html.
* @param host AKA the domain.
* @param domain The domain (host without port).
* @return ???
*/
public Map<String, String> matches(String method, String path, String accept, String host) {
public Map<String, String> matches(String method, String path, String accept, String domain) {
// Normalize
if (path.equals(Play.ctxPath)) {
path = path + "/";
Expand All @@ -810,10 +808,10 @@ public Map<String, String> matches(String method, String path, String accept, St

Matcher matcher = pattern.matcher(path);

boolean hostMatches = (host == null);
if (host != null) {
boolean hostMatches = (domain == null);
if (domain != null) {

Matcher hostMatcher = hostPattern.matcher(host);
Matcher hostMatcher = hostPattern.matcher(domain);
hostMatches = hostMatcher.matches();
}
// Extract the host variable
Expand Down Expand Up @@ -854,10 +852,10 @@ public Map<String, String> matches(String method, String path, String accept, St
localArgs.put(arg.name, Utils.decodeBytes(matcher.group(arg.name), charset.newDecoder()));
}
}
if (hostArg != null && host != null) {
if (hostArg != null && domain != null) {
// Parse the hostname and get only the part we are interested in
String routeValue = hostArg.defaultValue.replaceAll("\\{.*}", "");
host = host.replace(routeValue, "");
domain = domain.replace(routeValue, "");
localArgs.put(hostArg.name, host);
}
localArgs.putAll(staticArgs);
Expand Down
4 changes: 2 additions & 2 deletions samples-and-tests/just-test-cases/test/routing.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@

// Host
open('/')
assertTextPresent('1: http://static.foo.com/assets/hello.html')
assertTextPresent('1: http://static.foo.com:9003/assets/hello.html')
assertTextPresent('2: /assets/hello.html')
assertTextPresent('3: http://static2.foo.com/x/hello.html')
assertTextPresent('3: http://static2.foo.com:9003/x/hello.html')
assertTextPresent('4: /x/hello.html')
assertTextPresent('5: http://localhost:9003/public/image.gif')
assertTextPresent('6: /public/image.gif')
Expand Down

0 comments on commit e1e2424

Please sign in to comment.