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

Fix for issue 1218 in master #1246

Merged
merged 5 commits into from
Dec 18, 2019
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 @@ -41,9 +41,11 @@
import io.helidon.common.configurable.ThreadPool;
import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;
import io.helidon.common.http.Http;
import io.helidon.common.http.HttpRequest;
import io.helidon.config.Config;
import io.helidon.webserver.Handler;
import io.helidon.webserver.HttpException;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
Expand Down Expand Up @@ -165,7 +167,7 @@ private static URI requestUri(ServerRequest req) {
}
return new URI(sb.toString());
} catch (URISyntaxException | MalformedURLException e) {
throw new IllegalStateException("Unable to create a request URI from the request info.", e);
throw new HttpException("Unable to parse request URL", Http.Status.BAD_REQUEST_400, e);
}
}

Expand All @@ -174,7 +176,7 @@ private static URI baseUri(ServerRequest req) {
return new URI(req.isSecure() ? "https" : "http", null, req.localAddress(),
req.localPort(), basePath(req.path()), null, null);
} catch (URISyntaxException e) {
throw new IllegalStateException("Unable to create a base URI from the request info.", e);
throw new HttpException("Unable to parse request URL", Http.Status.BAD_REQUEST_400, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,21 @@ public void errorThrownEntity() throws Exception {
public void errorThrownError() throws Exception {
Response response = get("jersey/first/error/thrown/error");

doAssert(response, "", 500);
doAssert(response, null, 500);
}

@Test
public void errorThrownUnhandled() throws Exception {
Response response = get("jersey/first/error/thrown/unhandled");

doAssert(response, "", 500);
doAssert(response, null, 500);
}

@Test
public void simplePostNotFound() throws Exception {
Response response = post("jersey/first/non-existent-resource");

doAssert(response, "", Response.Status.NOT_FOUND);
doAssert(response, null, Response.Status.NOT_FOUND);
}

@Test
Expand All @@ -225,7 +225,7 @@ public void longPostNotFound() throws Exception {
return;
}
assertNotNull(response);
doAssert(response, "", Response.Status.NOT_FOUND);
doAssert(response, null, Response.Status.NOT_FOUND);
}

/**
Expand All @@ -245,7 +245,7 @@ public void noResponseEntityGet() throws Exception {
public void simpleGetNotFound() throws Exception {
Response response = get("jersey/first/non-existent-resource");

doAssert(response, "", Response.Status.NOT_FOUND);
doAssert(response, null, Response.Status.NOT_FOUND);
}

@Test
Expand Down Expand Up @@ -410,7 +410,9 @@ private void doAssert(Response response, String expectedContent, int expectedSta
try {
assertEquals(expectedStatusCode, response.getStatus(),
"Unexpected error: " + response.getStatus());
assertEquals(expectedContent, response.readEntity(String.class));
if (expectedContent != null) {
assertEquals(expectedContent, response.readEntity(String.class));
}
} finally {
response.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
send100Continue(ctx);
}

routing.route(bareRequest, bareResponse);
// If a problem during routing, return 400 response
try {
routing.route(bareRequest, bareResponse);
} catch (IllegalArgumentException e) {
send400BadRequest(ctx, e.getMessage());
return;
}
}

if (msg instanceof HttpContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ private void defaultHandler(Throwable t) {
"handler.class", "DEFAULT-ERROR-HANDLER",
"handled.error.message", t.toString()));
}
String message = null;
try {
if (t instanceof HttpException) {
response.status(((HttpException) t).status());
Expand All @@ -380,13 +381,14 @@ private void defaultHandler(Throwable t) {

response.status(Http.Status.INTERNAL_SERVER_ERROR_500);
}
message = t.getMessage();
} catch (AlreadyCompletedException e) {
LOGGER.log(Level.WARNING,
"Cannot perform error handling of the throwable (see cause of this exception) because headers "
+ "were already sent",
new IllegalStateException("Headers already sent. Cannot handle the cause of this exception.", t));
}
response.send().exceptionally(throwable -> {
response.send(message).exceptionally(throwable -> {
LOGGER.log(Level.WARNING, "Default error handler: Response wasn't successfully sent.", throwable);
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ public void testBadURL() throws Exception {
assertThat(headers, IsMapContaining.hasKey("content-length"));
}

@Test
public void testBadContentType() throws Exception {
String s = SocketHttpClient.sendAndReceive("/",
Http.Method.GET,
null,
List.of("Content-Type: %", "Connection: close"),
webServer);
assertThat(s, containsString("400 Bad Request"));
Map<String, String> headers = cutHeaders(s);
assertThat(headers, IsMapContaining.hasKey("content-type"));
assertThat(headers, IsMapContaining.hasKey("content-length"));
}

private Map<String, String> cutHeaders(String response) {
assertThat(response, notNullValue());
Expand Down