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

6962 include proxy setting for relative uris 4.x #7425

Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -179,8 +179,7 @@ default ClientRequestHeaders defaultRequestHeaders() {
*
* @return relative URIs flag
*/
// TODO Set the default value to false when proxy is implemented and see Http1CallChainBase.prologue for other changes
@ConfiguredOption("true")
@ConfiguredOption("false")
boolean relativeUris();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public void decorate(HttpClientConfig.BuilderBase<?, ?> target) {
}

if (target.proxy().isEmpty()) {
target.proxy(Proxy.create());
// Defaults to NoProxy type if Proxy is not set
target.proxy(Proxy.noProxy());
klustria marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public ProxyType type() {
* @param uri the uri
* @return true if it is in no hosts, otherwise false
*/
private boolean isNoHosts(InetSocketAddress uri) {
public boolean isNoHosts(InetSocketAddress uri) {
return noProxy.apply(uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -157,7 +158,13 @@ void prologue(BufferData nonEntityData, WebClientServiceRequest request, ClientU
+ request.headers().get(Http.HeaderNames.HOST).value()
+ " HTTP/1.1\r\n");
} else {
String schemeHostPort = clientConfig.relativeUris() ? "" : uri.scheme() + "://" + uri.host() + ":" + uri.port();
// When proxy is set, ensure that the request uses absolute URI because of Section 5.1.2 Request-URI in
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html which states: "The absoluteURI form is REQUIRED when the
// request is being made to a proxy."
InetSocketAddress uriAddress = new InetSocketAddress(uri.host(), uri.port());
String schemeHostPort = proxy == Proxy.noProxy() || proxy.isNoHosts(uriAddress) || clientConfig.relativeUris()
? "" // relative URI
: uri.scheme() + "://" + uri.host() + ":" + uri.port(); // absolute URI
nonEntityData.writeAscii(request.method().text()
+ " "
+ schemeHostPort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.helidon.logging.common.LogConfig;
import io.helidon.webclient.api.ClientConnection;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.Proxy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -78,6 +79,9 @@ class Http1ClientTest {
.sendExpectContinue(false)
.build();
private static final int dummyPort = 1234;
private enum RelativeUrisValue {
TRUE, FALSE, DEFAULT;
}

@Test
void testMaxHeaderSizeFail() {
Expand Down Expand Up @@ -218,17 +222,19 @@ void testSkipUrlEncoding() {

@ParameterizedTest
@MethodSource("relativeUris")
void testRelativeUris(boolean relativeUris, boolean outputStream, String requestUri, String expectedUriStart) {
Http1Client client = Http1Client.builder().relativeUris(relativeUris).build();
void testRelativeUris(Proxy proxy, RelativeUrisValue relativeUris, boolean outputStream, String requestUri, String expectedUriStart) {
Http1Client client;
switch (relativeUris) {
case TRUE -> client = Http1Client.builder().relativeUris(true).build();
case FALSE -> client = Http1Client.builder().relativeUris(false).build();
default -> client = Http1Client.create(); // Don't set relativeUris and accept whatever is the default
}
FakeHttp1ClientConnection connection = new FakeHttp1ClientConnection();
Http1ClientRequest request = client.put(requestUri);
Http1ClientRequest request = proxy != null ? client.put(requestUri).proxy(proxy) : client.put(requestUri);
request.connection(connection);
HttpClientResponse response;
if (outputStream) {
response = getHttp1ClientResponseFromOutputStream(request, new String[] {"Sending Something"});
} else {
response = request.submit("Sending Something");
}
HttpClientResponse response = outputStream
? getHttp1ClientResponseFromOutputStream(request, new String[] {"Sending Something"})
: request.submit("Sending Something");

assertThat(response.status(), is(Http.Status.OK_200));
StringTokenizer st = new StringTokenizer(connection.getPrologue(), " ");
Expand Down Expand Up @@ -393,25 +399,73 @@ private static Http1ClientResponse getHttp1ClientResponseFromOutputStream(Http1C
}

private static Stream<Arguments> relativeUris() {
String host = "www.oracle.com";
String path = "/test";
Proxy.Builder httpProxyroxyBuilder = Proxy.builder()
.type(Proxy.ProxyType.HTTP)
.host("proxyhost")
.port(80);
// Request type
boolean isOutputStream = true;
boolean isEntity = !isOutputStream;
// Proxy configuration
Proxy proxyEmptyNoProxyList = httpProxyroxyBuilder.build();
Proxy proxyWithNoProxyHost = httpProxyroxyBuilder.addNoProxy(host).build();
Proxy proxyNoProxy = Proxy.noProxy();
Proxy proxyNotSet = null;

return Stream.of(
// OutputStream (chunk request)
arguments(false, true, "http://www.dummy.com/test", "http://www.dummy.com:80/"),
arguments(false, true, "http://www.dummy.com:1111/test", "http://www.dummy.com:1111/"),
arguments(false, true, "https://www.dummy.com/test", "https://www.dummy.com:443/"),
arguments(false, true, "https://www.dummy.com:1111/test", "https://www.dummy.com:1111/"),
arguments(true, true, "http://www.dummy.com/test", "/test"),
arguments(true, true, "http://www.dummy.com:1111/test", "/test"),
arguments(true, true, "https://www.dummy.com/test", "/test"),
arguments(true, true, "https://www.dummy.com:1111/test", "/test"),
// Expects absolute URI
arguments(proxyEmptyNoProxyList, RelativeUrisValue.FALSE, isOutputStream,
"http://" + host + path, "http://" + host + ":80/"),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.FALSE, isOutputStream,
"https://" + host + path, "https://" + host + ":443/"),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.TRUE, isOutputStream,
"http://" + host + ":1111/test", path),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.FALSE, isOutputStream,
"http://" + host + ":1111/test", "http://" + host + ":1111/"),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.DEFAULT, isOutputStream,
"http://" + host + path, "http://" + host + ":80/"),
// Expects relative URI
arguments(proxyEmptyNoProxyList, RelativeUrisValue.TRUE, isOutputStream,
"http://" + host + path, path),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.TRUE, isOutputStream,
"https://" + host + path, path),
arguments(proxyWithNoProxyHost, RelativeUrisValue.DEFAULT, isOutputStream,
"http://" + host + path, path),
arguments(proxyNoProxy, RelativeUrisValue.DEFAULT, isOutputStream,
"http://" + host + path, path),
arguments(proxyNoProxy, RelativeUrisValue.FALSE, isOutputStream,
"http://" + host + path, path),
arguments(proxyNotSet, RelativeUrisValue.DEFAULT, isOutputStream,
"http://" + host + path, path),
// non-OutputStream (single entity request)
arguments(false, false, "http://www.dummy.com/test", "http://www.dummy.com:80/"),
arguments(false, false, "http://www.dummy.com:1111/test", "http://www.dummy.com:1111/"),
arguments(false, false, "https://www.dummy.com/test", "https://www.dummy.com:443/"),
arguments(false, false, "https://www.dummy.com:1111/test", "https://www.dummy.com:1111/"),
arguments(true, false, "http://www.dummy.com/test", "/test"),
arguments(true, false, "http://www.dummy.com:1111/test", "/test"),
arguments(true, false, "https://www.dummy.com/test", "/test"),
arguments(true, false, "https://www.dummy.com:1111/test", "/test"));
// Expects absolute URI
arguments(proxyEmptyNoProxyList, RelativeUrisValue.FALSE, isEntity,
"http://" + host + path, "http://" + host + ":80/"),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.FALSE, isEntity,
"https://" + host + path, "https://" + host + ":443/"),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.TRUE, isEntity,
"http://" + host + ":1111/test", path),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.FALSE, isEntity,
"http://" + host + ":1111/test", "http://" + host + ":1111/"),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.DEFAULT, isEntity,
"http://" + host + path, "http://" + host + ":80/"),
// Expects relative URI
arguments(proxyEmptyNoProxyList, RelativeUrisValue.TRUE, isEntity,
"http://" + host + path, path),
arguments(proxyEmptyNoProxyList, RelativeUrisValue.TRUE, isEntity,
"https://" + host + path, path),
arguments(proxyWithNoProxyHost, RelativeUrisValue.DEFAULT, isEntity,
"http://" + host + path, path),
arguments(proxyNoProxy, RelativeUrisValue.DEFAULT, isEntity,
"http://" + host + path, path),
arguments(proxyNoProxy, RelativeUrisValue.FALSE, isEntity,
"http://" + host + path, path),
arguments(proxyNotSet, RelativeUrisValue.DEFAULT, isEntity,
"http://" + host + path, path)
);
}

private static Stream<Arguments> headerValues() {
Expand Down