Skip to content

Commit

Permalink
handle URISyntaxException in JettyHttpContainer
Browse files Browse the repository at this point in the history
Signed-off-by: aserkes <andrii.serkes@oracle.com>
  • Loading branch information
aserkes committed Jun 14, 2021
1 parent e129ced commit 57b5693
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
10 changes: 7 additions & 3 deletions containers/jetty-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -37,7 +38,6 @@
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>jakarta.inject</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand All @@ -50,6 +50,11 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -69,7 +74,6 @@
<artifactId>maven-bundle-plugin</artifactId>
<inherited>true</inherited>
</plugin>

</plugins>

<resources>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -145,9 +145,9 @@ public void handle(final String target, final Request request, final HttpServlet

final Response response = request.getResponse();
final ResponseWriter responseWriter = new ResponseWriter(request, response, configSetStatusOverSendError);
final URI baseUri = getBaseUri(request);
final URI requestUri = getRequestUri(request, baseUri);
try {
final URI baseUri = getBaseUri(request);
final URI requestUri = getRequestUri(request, baseUri);
final ContainerRequest requestContext = new ContainerRequest(
baseUri,
requestUri,
Expand All @@ -171,25 +171,35 @@ public void handle(final String target, final Request request, final HttpServlet
// Mark the request as handled before generating the body of the response
request.setHandled(true);
appHandler.handle(requestContext);
} catch (URISyntaxException e) {
setResponseForInvalidUri(response, e);
} catch (final Exception ex) {
throw new RuntimeException(ex);
}

}

private URI getRequestUri(final Request request, final URI baseUri) {
try {
final String serverAddress = getServerAddress(baseUri);
String uri = request.getRequestURI();
private URI getRequestUri(final Request request, final URI baseUri) throws URISyntaxException {
final String serverAddress = getServerAddress(baseUri);
String uri = request.getRequestURI();

final String queryString = request.getQueryString();
if (queryString != null) {
uri = uri + "?" + ContainerUtils.encodeUnsafeCharacters(queryString);
}
final String queryString = request.getQueryString();
if (queryString != null) {
uri = uri + "?" + ContainerUtils.encodeUnsafeCharacters(queryString);
}

return new URI(serverAddress + uri);
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(ex);
return new URI(serverAddress + uri);
}

private void setResponseForInvalidUri(final HttpServletResponse response, final Throwable throwable) throws IOException {
LOGGER.log(Level.FINER, "Error while processing request.", throwable);

final javax.ws.rs.core.Response.Status badRequest = javax.ws.rs.core.Response.Status.BAD_REQUEST;
if (configSetStatusOverSendError) {
response.reset();
//noinspection deprecation
response.setStatus(badRequest.getStatusCode(), badRequest.getReasonPhrase());
} else {
response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +16,11 @@

package org.glassfish.jersey.jetty;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHttpRequest;
import org.junit.Test;

import javax.ws.rs.GET;
Expand All @@ -28,6 +33,7 @@
import javax.ws.rs.core.Response;

import java.io.IOException;
import java.net.URI;

import static org.junit.Assert.assertEquals;

Expand All @@ -44,6 +50,19 @@ public String get(@PathParam("status") int status) {

}

@Test
public void test400StatusCodeForIllegalSymbolsInURI() throws IOException {
startServer(ExceptionResource.class);
URI testUri = getUri().build();
String incorrectFragment = "&params[0]=test_status";
BasicHttpRequest request = new BasicHttpRequest("GET", testUri + incorrectFragment);
CloseableHttpClient client = HttpClientBuilder.create().build();

CloseableHttpResponse response = client.execute(new HttpHost(testUri.getHost(), testUri.getPort()), request);

assertEquals(400, response.getStatusLine().getStatusCode());
}

@Test
public void test400StatusCode() throws IOException {
startServer(ExceptionResource.class);
Expand Down

0 comments on commit 57b5693

Please sign in to comment.