diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java index 28e5ce19a2c..54ace6faf68 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java @@ -134,10 +134,14 @@ public abstract class HttpConnection implements Connection CookieStore cookieStore = getHttpClient().getCookieStore(); if (cookieStore != null) { - StringBuilder cookies = convertCookies(cookieStore.get(request.getURI()), null); - cookies = convertCookies(request.getCookies(), cookies); - if (cookies != null) - request.header(HttpHeader.COOKIE.asString(), cookies.toString()); + URI uri = request.getURI(); + if (uri != null) + { + StringBuilder cookies = convertCookies(cookieStore.get(uri), null); + cookies = convertCookies(request.getCookies(), cookies); + if (cookies != null) + request.header(HttpHeader.COOKIE.asString(), cookies.toString()); + } } // Authorization diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java index 57c79facc4e..2dd02292c5c 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.HttpCookie; import java.net.URI; +import java.net.URISyntaxException; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.ByteBuffer; @@ -57,6 +58,8 @@ import org.eclipse.jetty.util.Fields; public class HttpRequest implements Request { + private static final URI NULL_URI = URI.create("null:0"); + private final HttpFields headers = new HttpFields(); private final Fields params = new Fields(true); private final List responseListeners = new ArrayList<>(); @@ -158,22 +161,30 @@ public class HttpRequest implements Request @Override public Request path(String path) { - URI uri = URI.create(path); - String rawPath = uri.getRawPath(); - if (uri.isOpaque()) - rawPath = path; - if (rawPath == null) - rawPath = ""; - this.path = rawPath; - String query = uri.getRawQuery(); - if (query != null) + URI uri = newURI(path); + if (uri == null) { - this.query = query; - params.clear(); - extractParams(query); + this.path = path; + this.query = null; + } + else + { + String rawPath = uri.getRawPath(); + if (uri.isOpaque()) + rawPath = path; + if (rawPath == null) + rawPath = ""; + this.path = rawPath; + String query = uri.getRawQuery(); + if (query != null) + { + this.query = query; + params.clear(); + extractParams(query); + } + if (uri.isAbsolute()) + this.path = buildURI(false).toString(); } - if (uri.isAbsolute()) - this.path = buildURI(false).toString(); this.uri = null; return this; } @@ -187,9 +198,9 @@ public class HttpRequest implements Request @Override public URI getURI() { - if (uri != null) - return uri; - return uri = buildURI(true); + if (uri == null) + uri = buildURI(true); + return uri == NULL_URI ? null : uri; } @Override @@ -762,12 +773,28 @@ public class HttpRequest implements Request String query = getQuery(); if (query != null && withQuery) path += "?" + query; - URI result = URI.create(path); + URI result = newURI(path); + if (result == null) + return NULL_URI; if (!result.isAbsolute() && !result.isOpaque()) result = URI.create(new Origin(getScheme(), getHost(), getPort()).asString() + path); return result; } + private URI newURI(String uri) + { + try + { + return new URI(uri); + } + catch (URISyntaxException x) + { + // The "path" of a HTTP request may not be a URI, + // for example for CONNECT 127.0.0.1:8080 or OPTIONS *. + return null; + } + } + @Override public String toString() { diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyTunnellingTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyTunnellingTest.java index eab6ca76719..dd89a0715b1 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyTunnellingTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyTunnellingTest.java @@ -18,8 +18,6 @@ package org.eclipse.jetty.proxy; -import static org.junit.Assert.assertEquals; - import java.io.IOException; import java.net.ConnectException; import java.net.Socket; @@ -28,7 +26,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; - import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; @@ -64,6 +61,8 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class ProxyTunnellingTest { @Rule @@ -148,8 +147,10 @@ public class ProxyTunnellingTest try { + // Use a numeric host to test the URI of the CONNECT request. + String host = "127.0.0.1"; String body = "BODY"; - ContentResponse response = httpClient.newRequest("localhost", serverConnector.getLocalPort()) + ContentResponse response = httpClient.newRequest(host, serverConnector.getLocalPort()) .scheme(HttpScheme.HTTPS.asString()) .method(HttpMethod.GET) .path("/echo?body=" + URLEncoder.encode(body, "UTF-8"))