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

Url's with /:* has a bug in the NonorderedRouter.route method #9

Open
bgrov03 opened this issue Jul 4, 2016 · 0 comments
Open

Url's with /:* has a bug in the NonorderedRouter.route method #9

bgrov03 opened this issue Jul 4, 2016 · 0 comments

Comments

@bgrov03
Copy link

bgrov03 commented Jul 4, 2016

Due to time constraints I downloaded the code and included into my source code to fix a bug when using :* paths. Unfortunately the wrong url with a different root context would be resolved simply because of the wild card.

The bug was on line 109 which had the following:

} else if (!token.equals(token)) {

but should read:

} else if (!currToken.equals(token)) {

Here is the method with the bug fix :

public Routed route(String path) {
final String[] tokens = Pattern.removeSlashAtBothEnds(path).split("/");
final Map<String, String> params = new HashMap<String, String>();
boolean matched = true;

for (final Pattern<T> pattern : patterns) {
  final String[] currTokens = pattern.tokens();
  final T        target     = pattern.target();

  matched = true;
  params.clear();

  if (tokens.length == currTokens.length) {
    for (int i = 0; i < currTokens.length; i++) {
      final String token     = tokens[i];
      final String currToken = currTokens[i];

      if (currToken.length() > 0 && currToken.charAt(0) == ':') {
        params.put(currToken.substring(1), token);
      } else if (!currToken.equals(token)) {
        matched = false;
        break;
      }
    }
  } else if (currTokens.length > 0 && currTokens[currTokens.length - 1].equals(":*") && tokens.length >= currTokens.length) {
    for (int i = 0; i < currTokens.length - 1; i++) {
      final String token     = tokens[i];
      final String currToken = currTokens[i];

      if (currToken.length() > 0 && currToken.charAt(0) == ':') {
        params.put(currToken.substring(1), token);
      } else if (!currToken.equals(token)) {
        matched = false;
        break;
      }
    }

    if (matched) {
      final StringBuilder b = new StringBuilder(tokens[currTokens.length - 1]);
      for (int i = currTokens.length; i < tokens.length; i++) {
        b.append('/');
        b.append(tokens[i]);
      }
      params.put("*", b.toString());
    }
  } else {
    matched = false;
  }

  if (matched) return new Routed<T>(target, false, params);
}

if (notFound != null) {
  params.clear();
  return new Routed<T>(notFound, true, params);
}

return null;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant