diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/async/TestAsyncClient.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/async/TestAsyncClient.java index 7de2e4aca..adefc4560 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/async/TestAsyncClient.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/async/TestAsyncClient.java @@ -97,6 +97,10 @@ protected Future doExecute(final HttpHost target, return client.execute(target, requestProducer, responseConsumer, pushHandlerFactory, context, callback); } + /** + * @deprecated Do not use. + */ + @Deprecated @Override public void register(final String hostname, final String uriPattern, diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/sync/TestServerBootstrap.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/sync/TestServerBootstrap.java index 870948398..dc65bcb90 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/sync/TestServerBootstrap.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/extension/sync/TestServerBootstrap.java @@ -108,9 +108,9 @@ public TestServer build() throws Exception { .build()); for (final HandlerEntry entry: handlerList) { if (entry.hostname != null) { - server.registerHandlerVirtual(entry.hostname, entry.uriPattern, entry.handler); + server.register(entry.hostname, entry.uriPattern, entry.handler); } else { - server.registerHandler(entry.uriPattern, entry.handler); + server.register(entry.uriPattern, entry.handler); } } return new TestServer( diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractHttpAsyncClientBase.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractHttpAsyncClientBase.java index 32c1020f7..264faa2c7 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractHttpAsyncClientBase.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractHttpAsyncClientBase.java @@ -70,6 +70,11 @@ public final void start() { } } + /** + * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter} + * at the construction time + */ + @Deprecated @Override public void register(final String hostname, final String uriPattern, final Supplier supplier) { pushConsumerRegistry.register(hostname, uriPattern, supplier); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncPushConsumerRegistry.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncPushConsumerRegistry.java index cdc87a966..7c965ec9c 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncPushConsumerRegistry.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncPushConsumerRegistry.java @@ -34,25 +34,25 @@ import org.apache.hc.core5.function.Supplier; import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.nio.AsyncPushConsumer; -import org.apache.hc.core5.http.protocol.UriPatternMatcher; import org.apache.hc.core5.net.URIAuthority; import org.apache.hc.core5.util.Args; +@SuppressWarnings("deprecation") class AsyncPushConsumerRegistry { - private final UriPatternMatcher> primary; - private final ConcurrentMap>> hostMap; + private final org.apache.hc.core5.http.protocol.UriPatternMatcher> primary; + private final ConcurrentMap>> hostMap; public AsyncPushConsumerRegistry() { - this.primary = new UriPatternMatcher<>(); + this.primary = new org.apache.hc.core5.http.protocol.UriPatternMatcher<>(); this.hostMap = new ConcurrentHashMap<>(); } - private UriPatternMatcher> getPatternMatcher(final String hostname) { + private org.apache.hc.core5.http.protocol.UriPatternMatcher> getPatternMatcher(final String hostname) { if (hostname == null) { return primary; } - final UriPatternMatcher> hostMatcher = hostMap.get(hostname); + final org.apache.hc.core5.http.protocol.UriPatternMatcher> hostMatcher = hostMap.get(hostname); if (hostMatcher != null) { return hostMatcher; } @@ -63,7 +63,7 @@ public AsyncPushConsumer get(final HttpRequest request) { Args.notNull(request, "Request"); final URIAuthority authority = request.getAuthority(); final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null; - final UriPatternMatcher> patternMatcher = getPatternMatcher(key); + final org.apache.hc.core5.http.protocol.UriPatternMatcher> patternMatcher = getPatternMatcher(key); if (patternMatcher == null) { return null; } @@ -83,9 +83,9 @@ public void register(final String hostname, final String uriPattern, final Suppl primary.register(uriPattern, supplier); } else { final String key = hostname.toLowerCase(Locale.ROOT); - UriPatternMatcher> matcher = hostMap.get(key); + org.apache.hc.core5.http.protocol.UriPatternMatcher> matcher = hostMap.get(key); if (matcher == null) { - final UriPatternMatcher> newMatcher = new UriPatternMatcher<>(); + final org.apache.hc.core5.http.protocol.UriPatternMatcher> newMatcher = new org.apache.hc.core5.http.protocol.UriPatternMatcher<>(); matcher = hostMap.putIfAbsent(key, newMatcher); if (matcher == null) { matcher = newMatcher; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java index dde15a9c4..4e92c68c2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java @@ -129,8 +129,18 @@ public final Future execute( return execute(request, null, callback); } + /** + * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter} + * at the construction time + */ + @Deprecated public abstract void register(String hostname, String uriPattern, Supplier supplier); + /** + * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter} + * at the construction time + */ + @Deprecated public final void register(final String uriPattern, final Supplier supplier) { register(null, uriPattern, supplier); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java index 84b7b5536..22e62f7b4 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClients.java @@ -37,8 +37,12 @@ import org.apache.hc.client5.http.nio.AsyncClientConnectionManager; import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy; import org.apache.hc.core5.concurrent.DefaultThreadFactory; +import org.apache.hc.core5.function.Supplier; import org.apache.hc.core5.http.config.CharCodingConfig; import org.apache.hc.core5.http.config.Http1Config; +import org.apache.hc.core5.http.impl.routing.RequestRouter; +import org.apache.hc.core5.http.nio.AsyncPushConsumer; +import org.apache.hc.core5.http.nio.HandlerFactory; import org.apache.hc.core5.http.nio.ssl.TlsStrategy; import org.apache.hc.core5.http.protocol.DefaultHttpProcessor; import org.apache.hc.core5.http.protocol.HttpProcessor; @@ -333,4 +337,16 @@ public static MinimalH2AsyncClient createHttp2Minimal() { return createHttp2Minimal(H2Config.DEFAULT); } + /** + * Creates {@link HandlerFactory} backed by a push {@link RequestRouter}. + * + * @since 5.4 + */ + public static HandlerFactory pushRouter(final RequestRouter> requestRouter) { + return (request, context) -> { + final Supplier supplier = requestRouter.resolve(request, context); + return supplier != null ? supplier.get() : null; + }; + } + } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/BasicDomainHandler.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/BasicDomainHandler.java index e80cd3f36..8286f5d87 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/BasicDomainHandler.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/BasicDomainHandler.java @@ -101,7 +101,7 @@ public void validate(final Cookie cookie, final CookieOrigin origin) } static boolean domainMatch(final String domain, final String host) { - if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) { + if (InetAddressUtils.isIPv4(host) || InetAddressUtils.isIPv6(host)) { return false; } final String normalizedDomain = domain.startsWith(".") ? domain.substring(1) : domain; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java index 2c030ac9d..25a871e50 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java @@ -273,14 +273,14 @@ static String extractCN(final String subjectPrincipal) throws SSLException { } static HostNameType determineHostFormat(final String host) { - if (InetAddressUtils.isIPv4Address(host)) { + if (InetAddressUtils.isIPv4(host)) { return HostNameType.IPv4; } String s = host; if (s.startsWith("[") && s.endsWith("]")) { s = host.substring(1, host.length() - 1); } - if (InetAddressUtils.isIPv6Address(s)) { + if (InetAddressUtils.isIPv6(s)) { return HostNameType.IPv6; } return HostNameType.DNS; diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java index dc36ade3d..e965ac303 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java @@ -37,12 +37,15 @@ import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; import org.apache.hc.client5.http.impl.async.HttpAsyncClients; import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; +import org.apache.hc.core5.function.Supplier; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.impl.routing.RequestRouter; import org.apache.hc.core5.http.message.BasicHttpRequest; import org.apache.hc.core5.http.message.StatusLine; +import org.apache.hc.core5.http.nio.AsyncPushConsumer; import org.apache.hc.core5.http.nio.support.BasicRequestProducer; import org.apache.hc.core5.http.support.BasicRequestBuilder; import org.apache.hc.core5.http2.HttpVersionPolicy; @@ -69,39 +72,44 @@ public static void main(final String[] args) throws Exception { client.start(); - client.register("*", () -> new AbstractBinPushConsumer() { + final RequestRouter> pushRequestRouter = RequestRouter.>builder() + // Route all requests to the local authority + .resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER) + // Use the same route for all requests + .addRoute(RequestRouter.LOCAL_AUTHORITY, "*", () -> new AbstractBinPushConsumer() { - @Override - protected void start( - final HttpRequest promise, - final HttpResponse response, - final ContentType contentType) throws HttpException, IOException { - System.out.println(promise.getPath() + " (push)->" + new StatusLine(response)); - } + @Override + protected void start( + final HttpRequest promise, + final HttpResponse response, + final ContentType contentType) throws HttpException, IOException { + System.out.println(promise.getPath() + " (push)->" + new StatusLine(response)); + } - @Override - protected int capacityIncrement() { - return Integer.MAX_VALUE; - } + @Override + protected int capacityIncrement() { + return Integer.MAX_VALUE; + } - @Override - protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException { - } + @Override + protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException { + } - @Override - protected void completed() { - } + @Override + protected void completed() { + } - @Override - public void failed(final Exception cause) { - System.out.println("(push)->" + cause); - } + @Override + public void failed(final Exception cause) { + System.out.println("(push)->" + cause); + } - @Override - public void releaseResources() { - } + @Override + public void releaseResources() { + } - }); + }) + .build(); final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").build(); @@ -140,7 +148,10 @@ public void failed(final Exception cause) { public void releaseResources() { } - }, null); + }, + HttpAsyncClients.pushRouter(pushRequestRouter), + null, + null); future.get(); System.out.println("Shutting down");