Skip to content

Commit

Permalink
Fix "Response head already sent"
Browse files Browse the repository at this point in the history
Fixes #19621
  • Loading branch information
stuartwdouglas committed Aug 27, 2021
1 parent 5930131 commit 8f46cd9
Showing 1 changed file with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,49 @@ public QuarkusErrorHandler(boolean showStack) {

@Override
public void handle(RoutingContext event) {
if (event.failure() == null) {
event.response().setStatusCode(event.statusCode());
event.response().end();
return;
}
//this can happen if there is no auth mechanisms
if (event.failure() instanceof UnauthorizedException) {
HttpAuthenticator authenticator = event.get(HttpAuthenticator.class.getName());
if (authenticator != null) {
authenticator.sendChallenge(event).subscribe().with(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) {
event.response().end();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
event.fail(throwable);
}
});
} else {
try {
if (event.failure() == null) {
event.response().setStatusCode(event.statusCode());
event.response().end();
return;
}
//this can happen if there is no auth mechanisms
if (event.failure() instanceof UnauthorizedException) {
HttpAuthenticator authenticator = event.get(HttpAuthenticator.class.getName());
if (authenticator != null) {
authenticator.sendChallenge(event).subscribe().with(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) {
event.response().end();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
event.fail(throwable);
}
});
} else {
event.response().setStatusCode(HttpResponseStatus.UNAUTHORIZED.code()).end();
}
return;
}
if (event.failure() instanceof ForbiddenException) {
event.response().setStatusCode(HttpResponseStatus.FORBIDDEN.code()).end();
return;
}
if (event.failure() instanceof AuthenticationFailedException) {
//generally this should be handled elsewhere
//but if we get to this point bad things have happened
//so it is better to send a response than to hang
event.response().setStatusCode(HttpResponseStatus.UNAUTHORIZED.code()).end();
return;
}
} catch (IllegalStateException e) {
//ignore this if the response is already started
if (!event.response().ended()) {
//could be that just the head is committed
event.response().end();
}
return;
}
if (event.failure() instanceof ForbiddenException) {
event.response().setStatusCode(HttpResponseStatus.FORBIDDEN.code()).end();
return;
}
if (event.failure() instanceof AuthenticationFailedException) {
//generally this should be handled elsewhere
//but if we get to this point bad things have happened
//so it is better to send a response than to hang
event.response().setStatusCode(HttpResponseStatus.UNAUTHORIZED.code()).end();
return;
}

Expand All @@ -100,6 +109,15 @@ public void accept(Throwable throwable) {
} else {
log.errorf(exception, "HTTP Request to %s failed, error id: %s", event.request().uri(), uuid);
}
//we have logged the error
//now lets see if we can actually send a response
//if not we just return
if (event.response().ended()) {
return;
} else if (event.response().headWritten()){
event.response().end();
return;
}
String accept = event.request().getHeader("Accept");
if (accept != null && accept.contains("application/json")) {
event.response().headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8");
Expand Down

0 comments on commit 8f46cd9

Please sign in to comment.