Skip to content

Commit

Permalink
Multi-address client: take advantage of lowercase scheme (#2975)
Browse files Browse the repository at this point in the history
Motivation:

In #2938 we made it guaranteed that `HttpRequestMetaData#scheme()` result is always in lowercase. We can use that to simplify scheme checks inside `DefaultMultiAddressUrlHttpClientBuilder`.

Modifications:
- Replace all scheme checks from `equalsIgnoreCase` to `equals`;
- Extract exact same instance of `https` string from `Uri3986` to speed up `equals` check;

Result:

No need to worry about cases for scheme check.
  • Loading branch information
idelpivnitskiy committed Jun 17, 2024
1 parent 7b68059 commit ff218cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.servicetalk.context.api.ContextMap;
import io.servicetalk.http.api.DefaultHttpHeadersFactory;
import io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory;
import io.servicetalk.http.api.EmptyHttpHeaders;
import io.servicetalk.http.api.FilterableReservedStreamingHttpConnection;
import io.servicetalk.http.api.FilterableStreamingHttpClient;
import io.servicetalk.http.api.HttpContextKeys;
Expand Down Expand Up @@ -58,6 +59,7 @@

import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
Expand All @@ -76,6 +78,8 @@
import static io.servicetalk.http.api.HttpHeaderNames.HOST;
import static io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_0;
import static io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1;
import static io.servicetalk.http.api.HttpRequestMetaDataFactory.newRequestMetaData;
import static io.servicetalk.http.api.HttpRequestMethod.GET;
import static io.servicetalk.http.netty.DefaultSingleAddressHttpClientBuilder.setExecutionContext;
import static java.util.Objects.requireNonNull;

Expand All @@ -96,7 +100,11 @@ final class DefaultMultiAddressUrlHttpClientBuilder

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultMultiAddressUrlHttpClientBuilder.class);

private static final String HTTPS_SCHEME = HTTPS.toString();
// Use HttpRequestMetaData to access "https" constant used by Uri3986 class to optimize "equals" check to be a
// trivial reference check.
@SuppressWarnings("DataFlowIssue")
private static final String HTTPS_SCHEME = newRequestMetaData(HTTP_1_1, GET, "https://invalid./",
EmptyHttpHeaders.INSTANCE).scheme();

private final Function<HostAndPort, SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress>> builderFactory;
private final HttpExecutionContextBuilder executionContextBuilder = new HttpExecutionContextBuilder();
Expand Down Expand Up @@ -159,10 +167,11 @@ private static final class CachingKeyFactory implements AsyncCloseable {

UrlKey get(final HttpRequestMetaData metaData) throws MalformedURLException {
final String scheme = ensureUrlComponentNonNull(metaData.scheme(), "scheme");
assert scheme.equals(scheme.toLowerCase(Locale.ENGLISH)) : "scheme must be in lowercase";
final String host = ensureUrlComponentNonNull(metaData.host(), "host");
final int parsedPort = metaData.port();
final int port = parsedPort >= 0 ? parsedPort :
(HTTPS_SCHEME.equalsIgnoreCase(scheme) ? defaultHttpsPort : defaultHttpPort);
(HTTPS_SCHEME.equals(scheme) ? defaultHttpsPort : defaultHttpPort);
setHostHeader(metaData);
metaData.requestTarget(absoluteToRelativeFormRequestTarget(metaData.requestTarget(), scheme, host));

Expand Down Expand Up @@ -260,7 +269,7 @@ public StreamingHttpClient apply(final UrlKey urlKey) {
requireNonNull(builderFactory.apply(urlKey.hostAndPort));

setExecutionContext(builder, executionContext);
if (HTTPS_SCHEME.equalsIgnoreCase(urlKey.scheme)) {
if (HTTPS_SCHEME.equals(urlKey.scheme)) {
builder.sslConfig(DEFAULT_CLIENT_SSL_CONFIG);
}

Expand Down Expand Up @@ -318,7 +327,6 @@ protected Single<StreamingHttpResponse> request(
final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return defer(() -> {
singleClientStrategyUpdate(request.context(), client.executionContext().executionStrategy());

return delegate.request(request);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void multiAddressClientWithSslToSecureServer() throws Exception {
void multiAddressClientToSecureServerThenToNonSecureServer() throws Exception {
try (BlockingHttpClient client = HttpClients.forMultiAddressUrl(getClass().getSimpleName())
.initializer((scheme, address, builder) -> {
if ("https".equalsIgnoreCase(scheme)) {
if ("https".equals(scheme)) {
builder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem)
.peerHost(serverPemHostname()).build());
}
Expand All @@ -251,7 +251,7 @@ void multiAddressClientToSecureServerThenToNonSecureServer() throws Exception {
void multiAddressClientToNonSecureServerThenToSecureServer() throws Exception {
try (BlockingHttpClient client = HttpClients.forMultiAddressUrl(getClass().getSimpleName())
.initializer((scheme, address, builder) -> {
if ("https".equalsIgnoreCase(scheme)) {
if ("https".equals(scheme)) {
builder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem)
.peerHost(serverPemHostname()).build());
}
Expand Down

0 comments on commit ff218cc

Please sign in to comment.