Skip to content

Commit

Permalink
Merge pull request #133 from jrsacks/master
Browse files Browse the repository at this point in the history
PathMatchHandler will hand off control to the next handler when a request has an invalid URI
  • Loading branch information
sharvie committed Aug 22, 2014
2 parents 54f9f85 + a9fe59d commit c8ab58f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main/java/org/webbitserver/handler/PathMatchHandler.java
Expand Up @@ -23,11 +23,21 @@ public PathMatchHandler(String path, HttpHandler httpHandler) {
this(Pattern.compile(path), httpHandler);
}

public Boolean pathIsAMatch(HttpRequest request){
try {
String path = URI.create(request.uri()).getPath();
Matcher matcher = pathPattern.matcher(path);
if (matcher.matches()) {
return true;
}
} catch (IllegalArgumentException e) {
}
return false;
}

@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
String path = URI.create(request.uri()).getPath();
Matcher matcher = pathPattern.matcher(path);
if (matcher.matches()) {
if (pathIsAMatch(request)) {
httpHandler.handleHttpRequest(request, response, control);
} else {
control.nextHandler();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/webbitserver/handler/PathMatchHandlerTest.java
Expand Up @@ -53,6 +53,21 @@ public void matchesRequestWithRegexpPath() throws Exception {
verify(handler).handleHttpRequest(req, res, ctl);
}

@Test
public void handsOffWhenIllegalURIPath() throws Exception {
HttpHandler handler = mock(HttpHandler.class);
PathMatchHandler pmh = new PathMatchHandler("/hello", handler);

HttpRequest req = new StubHttpRequest("//");
HttpResponse res = new StubHttpResponse();
HttpControl ctl = mock(HttpControl.class);

pmh.handleHttpRequest(req, res, ctl);

verifyZeroInteractions(handler);
verify(ctl).nextHandler();
}

@Test
public void handsOffWhenNoMatch() throws Exception {
HttpHandler handler = mock(HttpHandler.class);
Expand Down

0 comments on commit c8ab58f

Please sign in to comment.