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

Prevent duplicate invocation of error handler #2667

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void handle(HttpServerRequest request) {
LOG.trace("Router: " + System.identityHashCode(this) + " accepting request " + request.method() + " " + request.absoluteURI());
}

new RoutingContextImpl(null, this, request, state.getRoutes()).next();
RoutingContextImpl routingContext = new RoutingContextImpl(null, this, request, state.getRoutes());
routingContext.route();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public RoutingContextImpl(String mountPoint, RouterImpl router, HttpServerReques
this.router = router;
this.request = new HttpServerRequestWrapper(request, router.getAllowForward(), this);
this.body = new RequestBodyImpl(this);
}

void route() {
final String path = request.path();
// optimized method which try hard to not allocate
final boolean hasValidAuthority = ((HttpServerRequestInternal) request).isValidAuthority();
Expand All @@ -88,6 +90,8 @@ public RoutingContextImpl(String mountPoint, RouterImpl router, HttpServerReques
} else if (path.charAt(0) != '/') {
// For compatiblity we return `Not Found` when a path does not start with `/`
fail(404);
} else {
next();
}
}

Expand Down
13 changes: 13 additions & 0 deletions vertx-web/src/test/java/io/vertx/ext/web/RouterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3655,4 +3655,17 @@ public void testPausedConnection4() {

await();
}

@Test
public void testErrorHandlerInvokedOnce() throws Exception {
AtomicInteger errorHandlerInvocations = new AtomicInteger();

router.errorHandler(404, rc -> {
errorHandlerInvocations.incrementAndGet();
rc.response().setStatusCode(404).end();
});

testRequest(HttpMethod.GET, "path-without-slash-prefix", HttpResponseStatus.NOT_FOUND);
assertEquals(1, errorHandlerInvocations.get());
}
}
Loading