diff --git a/pom.xml b/pom.xml index f7551ecd10d..d2d98848293 100644 --- a/pom.xml +++ b/pom.xml @@ -79,6 +79,7 @@ 1.9.3 3.2.3 2.23.4 + 5.15.0 0.9.1 0.1.0 2.2.10 @@ -1034,6 +1035,11 @@ mockito-core ${version.lib.mockito} + + org.mock-server + mockserver-netty-no-dependencies + ${version.lib.mockserver} + org.eclipse.jgit org.eclipse.jgit.junit diff --git a/webclient/api/src/main/java/io/helidon/webclient/api/Proxy.java b/webclient/api/src/main/java/io/helidon/webclient/api/Proxy.java index 269d53136fc..5cb33ba039f 100644 --- a/webclient/api/src/main/java/io/helidon/webclient/api/Proxy.java +++ b/webclient/api/src/main/java/io/helidon/webclient/api/Proxy.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2023, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -90,6 +90,7 @@ public class Proxy { private final Optional password; private final ProxySelector systemProxySelector; private final Optional proxyAuthHeader; + private final boolean httpConnect; private Proxy(Proxy.Builder builder) { this.host = builder.host(); @@ -102,6 +103,7 @@ private Proxy(Proxy.Builder builder) { this.port = builder.port(); this.username = builder.username(); this.password = builder.password(); + this.httpConnect = builder.httpConnect(); if (type == ProxyType.SYSTEM) { this.noProxy = inetSocketAddress -> true; @@ -469,26 +471,26 @@ private static Socket connectToProxy(WebClient webClient, it -> { }) .connect(); - - HttpClientRequest request = webClient.method(Method.CONNECT) - .followRedirects(false) // do not follow redirects for proxy connect itself - .connection(connection) - .uri("http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort()) - .protocolId("http/1.1") // MUST be 1.1, if not available, proxy connection will fail - .header(HeaderNames.HOST, targetAddress.getHostName() + ":" + targetAddress.getPort()) - .accept(MediaTypes.WILDCARD); - if (clientConfig.keepAlive()) { - request.header(HeaderValues.CONNECTION_KEEP_ALIVE) - .header(PROXY_CONNECTION); - } - proxy.proxyAuthHeader.ifPresent(request::header); - // we cannot close the response, as that would close the connection - HttpClientResponse response = request.request(); - if (response.status().family() != Status.Family.SUCCESSFUL) { - response.close(); - throw new IllegalStateException("Proxy sent wrong HTTP response code: " + response.status()); + if (proxy.httpConnect) { + HttpClientRequest request = webClient.method(Method.CONNECT) + .followRedirects(false) // do not follow redirects for proxy connect itself + .connection(connection) + .uri("http://" + proxyAddress.getHostName() + ":" + proxyAddress.getPort()) + .protocolId("http/1.1") // MUST be 1.1, if not available, proxy connection will fail + .header(HeaderNames.HOST, targetAddress.getHostName() + ":" + targetAddress.getPort()) + .accept(MediaTypes.WILDCARD); + if (clientConfig.keepAlive()) { + request.header(HeaderValues.CONNECTION_KEEP_ALIVE) + .header(PROXY_CONNECTION); + } + proxy.proxyAuthHeader.ifPresent(request::header); + // we cannot close the response, as that would close the connection + HttpClientResponse response = request.request(); + if (response.status().family() != Status.Family.SUCCESSFUL) { + response.close(); + throw new IllegalStateException("Proxy sent wrong HTTP response code: " + response.status()); + } } - return connection.socket(); } @@ -587,6 +589,7 @@ public static class Builder implements io.helidon.common.Builder private int port = 80; private String username; private char[] password; + private boolean httpConnect = true; private Builder() { } @@ -632,6 +635,11 @@ public Proxy build() { * Proxy password * * + * httpConnect + * {@code true} + * Specify whether the http client will make an HTTP CONNECT to the proxy + * + * * no-proxy * {@code no default} * Contains list of the hosts which should be excluded from using proxy @@ -667,6 +675,19 @@ public Builder type(ProxyType type) { return this; } + /** + * Sets a httpConnect value to specify whether the client will + * make an HTTP CONNECT request to the proxy server. + * + * @param httpConnect HTTP CONNECT + * @return updated builder instance + */ + @ConfiguredOption + public Builder httpConnect(boolean httpConnect) { + this.httpConnect = httpConnect; + return this; + } + /** * Sets a new host value. * @@ -742,6 +763,10 @@ ProxyType type() { return type; } + boolean httpConnect() { + return httpConnect; + } + String host() { return host; } diff --git a/webclient/tests/http1/pom.xml b/webclient/tests/http1/pom.xml index 318419589d8..6737fe9b28a 100644 --- a/webclient/tests/http1/pom.xml +++ b/webclient/tests/http1/pom.xml @@ -67,5 +67,15 @@ vertx-core test + + org.mock-server + mockserver-netty-no-dependencies + test + + + io.helidon.logging + helidon-logging-jul + test + diff --git a/webclient/tests/http1/src/test/java/io/helidon/webclient/tests/HttpMockProxyTest.java b/webclient/tests/http1/src/test/java/io/helidon/webclient/tests/HttpMockProxyTest.java new file mode 100644 index 00000000000..2f95a8a114a --- /dev/null +++ b/webclient/tests/http1/src/test/java/io/helidon/webclient/tests/HttpMockProxyTest.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.webclient.tests; + +import static io.helidon.http.Method.GET; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockserver.integration.ClientAndServer.startClientAndServer; +import static org.mockserver.model.HttpForward.forward; +import static org.mockserver.model.HttpRequest.request; + +import io.helidon.http.Status; +import io.helidon.webclient.api.HttpClientResponse; +import io.helidon.webclient.api.Proxy; +import io.helidon.webclient.api.WebClient; +import io.helidon.webserver.WebServer; +import io.helidon.webserver.http.HttpRouting; +import io.helidon.webserver.testing.junit5.ServerTest; +import io.helidon.webserver.testing.junit5.SetUpRoute; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockserver.integration.ClientAndServer; +import org.mockserver.model.HttpForward.Scheme; + +@ServerTest +class HttpMockProxyTest { + + private static final int PROXY_PORT = 1080; + private final WebClient webClient; + private final int serverPort; + private ClientAndServer mockServer; + + HttpMockProxyTest(WebServer server) { + this.serverPort = server.port(); + this.webClient = WebClient.builder() + .baseUri("http://localhost:" + serverPort) + .proxy(Proxy.builder().httpConnect(false).host("localhost").port(PROXY_PORT).build()) + .build(); + } + + @SetUpRoute + static void routing(HttpRouting.Builder router) { + router.route(GET, "/get", Routes::get); + } + + @BeforeEach + public void before() { + mockServer = startClientAndServer(PROXY_PORT); + mockServer.when(request()).forward(forward().withScheme(Scheme.HTTP).withHost("localhost").withPort(serverPort)); + } + + @AfterEach + public void after() { + mockServer.stop(); + } + + @Test + public void issue9022() { + try (HttpClientResponse response = webClient.get("/get").request()) { + assertThat(response.status(), is(Status.OK_200)); + String entity = response.entity().as(String.class); + assertThat(entity, is("Hello")); + } + } + + private static class Routes { + private static String get() { + return "Hello"; + } + } +} diff --git a/webclient/tests/http1/src/test/resources/logging-test.properties b/webclient/tests/http1/src/test/resources/logging-test.properties index fe804c3ed3a..a2c90082755 100644 --- a/webclient/tests/http1/src/test/resources/logging-test.properties +++ b/webclient/tests/http1/src/test/resources/logging-test.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2023 Oracle and/or its affiliates. +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,11 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. # -handlers=java.util.logging.ConsoleHandler -java.util.logging.ConsoleHandler.level=INFO -java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter +#All attributes details +handlers=io.helidon.logging.jul.HelidonConsoleHandler java.util.logging.SimpleFormatter.format=%1$tH:%1$tM:%1$tS %4$s %3$s %5$s%6$s%n -# Global logging level. Can be overridden by specific loggers + +#All log level details .level=INFO io.helidon.webclient.level=INFO io.helidon.webclient.http1.ClientRequestImpl.level=INFO