From 0ad471449ba0d32d7b415633bb1bd92edbeeb822 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Thu, 28 May 2015 15:38:28 +0200 Subject: [PATCH] 468421 - HttpClient#send fails with IllegalArgumentException on non-lowercase schemes. RFC 7230 specifies that both scheme and host must be case insensitive. --- .../org/eclipse/jetty/client/HttpClient.java | 3 +- .../eclipse/jetty/client/HttpDestination.java | 4 +- .../jetty/client/HttpClientURITest.java | 40 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index 1ca2c7598d6..3ec0e02caed 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -517,7 +517,8 @@ public class HttpClient extends ContainerLifeCycle if (!HttpScheme.HTTP.is(scheme) && !HttpScheme.HTTPS.is(scheme)) throw new IllegalArgumentException("Invalid protocol " + scheme); - HttpDestination destination = destinationFor(scheme, request.getHost(), request.getPort()); + String host = request.getHost().toLowerCase(Locale.ENGLISH); + HttpDestination destination = destinationFor(scheme, host, request.getPort()); destination.send(request, listeners); } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java index ad919e51614..75389217922 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java @@ -161,9 +161,9 @@ public abstract class HttpDestination implements Destination, Closeable, Dumpabl protected void send(HttpRequest request, List listeners) { - if (!getScheme().equals(request.getScheme())) + if (!getScheme().equalsIgnoreCase(request.getScheme())) throw new IllegalArgumentException("Invalid request scheme " + request.getScheme() + " for destination " + this); - if (!getHost().equals(request.getHost())) + if (!getHost().equalsIgnoreCase(request.getHost())) throw new IllegalArgumentException("Invalid request host " + request.getHost() + " for destination " + this); int port = request.getPort(); if (port >= 0 && getPort() != port) diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientURITest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientURITest.java index df4351fd13b..d6eb4e9baaf 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientURITest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientURITest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.client; import java.io.IOException; import java.net.URLEncoder; import java.util.concurrent.TimeUnit; + import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -431,4 +432,43 @@ public class HttpClientURITest extends AbstractHttpClientServerTest Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); } + @Test + public void testSchemeIsCaseInsensitive() throws Exception + { + start(new AbstractHandler() + { + @Override + public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + baseRequest.setHandled(true); + } + }); + + ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) + .scheme(scheme.toUpperCase()) + .timeout(5, TimeUnit.SECONDS) + .send(); + + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + } + + @Test + public void testHostIsCaseInsensitive() throws Exception + { + start(new AbstractHandler() + { + @Override + public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + baseRequest.setHandled(true); + } + }); + + ContentResponse response = client.newRequest("LOCALHOST", connector.getLocalPort()) + .scheme(scheme) + .timeout(5, TimeUnit.SECONDS) + .send(); + + Assert.assertEquals(HttpStatus.OK_200, response.getStatus()); + } }