Merge pull request #4878 from lorban/jetty-9.4.x-4822-reduce-garbage

Reduce client-generated garbage
This commit is contained in:
Simone Bordet 2020-05-14 13:27:45 +02:00 committed by GitHub
commit ad51bc1af6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 18 deletions

View File

@ -93,4 +93,10 @@ public class HttpAuthenticationStore implements AuthenticationStore
}
return null;
}
@Override
public boolean hasAuthenticationResults()
{
return !results.isEmpty();
}
}

View File

@ -163,7 +163,7 @@ public class HttpClient extends ContainerLifeCycle
*/
public HttpClient()
{
this(null);
this(new HttpClientTransportOverHTTP(), null);
}
/**
@ -178,6 +178,18 @@ public class HttpClient extends ContainerLifeCycle
this(new HttpClientTransportOverHTTP(), sslContextFactory);
}
/**
* Creates a {@link HttpClient} instance that can perform requests to non-TLS destinations only
* (that is, requests with the "http" scheme only, and not "https").
*
* @param transport the {@link HttpClientTransport}
* @see #HttpClient(HttpClientTransport, SslContextFactory) to perform requests to TLS destinations.
*/
public HttpClient(HttpClientTransport transport)
{
this(transport, null);
}
public HttpClient(HttpClientTransport transport, SslContextFactory sslContextFactory)
{
this.transport = transport;
@ -1145,7 +1157,7 @@ public class HttpClient extends ContainerLifeCycle
protected String normalizeHost(String host)
{
if (host != null && host.matches("\\[.*\\]"))
if (host != null && host.startsWith("[") && host.endsWith("]"))
return host.substring(1, host.length() - 1);
return host;
}

View File

@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.client.api.Authentication;
import org.eclipse.jetty.client.api.AuthenticationStore;
import org.eclipse.jetty.client.api.Connection;
import org.eclipse.jetty.client.api.ContentProvider;
import org.eclipse.jetty.client.api.Request;
@ -103,12 +104,14 @@ public abstract class HttpConnection implements Connection
request.path(path);
}
URI uri = request.getURI();
if (proxy instanceof HttpProxy && !HttpClient.isSchemeSecure(request.getScheme()) && uri != null)
if (proxy instanceof HttpProxy && !HttpClient.isSchemeSecure(request.getScheme()))
{
path = uri.toString();
request.path(path);
URI uri = request.getURI();
if (uri != null)
{
path = uri.toString();
request.path(path);
}
}
// If we are HTTP 1.1, add the Host header
@ -147,9 +150,10 @@ public abstract class HttpConnection implements Connection
// Cookies
CookieStore cookieStore = getHttpClient().getCookieStore();
if (cookieStore != null)
if (cookieStore != null && cookieStore.getClass() != HttpCookieStore.Empty.class)
{
StringBuilder cookies = null;
URI uri = request.getURI();
if (uri != null)
cookies = convertCookies(HttpCookieStore.matchPath(uri, cookieStore.get(uri)), null);
cookies = convertCookies(request.getCookies(), cookies);
@ -158,8 +162,8 @@ public abstract class HttpConnection implements Connection
}
// Authentication
applyAuthentication(request, proxy != null ? proxy.getURI() : null);
applyAuthentication(request, uri);
applyProxyAuthentication(request, proxy);
applyRequestAuthentication(request);
}
private StringBuilder convertCookies(List<HttpCookie> cookies, StringBuilder builder)
@ -175,11 +179,26 @@ public abstract class HttpConnection implements Connection
return builder;
}
private void applyAuthentication(Request request, URI uri)
private void applyRequestAuthentication(Request request)
{
if (uri != null)
AuthenticationStore authenticationStore = getHttpClient().getAuthenticationStore();
if (authenticationStore.hasAuthenticationResults())
{
Authentication.Result result = getHttpClient().getAuthenticationStore().findAuthenticationResult(uri);
URI uri = request.getURI();
if (uri != null)
{
Authentication.Result result = authenticationStore.findAuthenticationResult(uri);
if (result != null)
result.apply(request);
}
}
}
private void applyProxyAuthentication(Request request, ProxyConfiguration.Proxy proxy)
{
if (proxy != null)
{
Authentication.Result result = getHttpClient().getAuthenticationStore().findAuthenticationResult(proxy.getURI());
if (result != null)
result.apply(request);
}

View File

@ -117,9 +117,9 @@ public class HttpConversation extends AttributesMap
// will notify a listener that may send a new request and trigger
// another call to this method which will build different listeners
// which may be iterated over when the iteration continues.
List<Response.ResponseListener> listeners = new ArrayList<>();
HttpExchange firstExchange = exchanges.peekFirst();
HttpExchange lastExchange = exchanges.peekLast();
List<Response.ResponseListener> listeners = new ArrayList<>(firstExchange.getResponseListeners().size() + lastExchange.getResponseListeners().size());
if (firstExchange == lastExchange)
{
// We don't have a conversation, just a single request.

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.client;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@ -31,7 +32,6 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.LongConsumer;
import java.util.function.LongUnaryOperator;
import java.util.stream.Collectors;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
@ -693,10 +693,11 @@ public abstract class HttpReceiver
private ContentListeners(List<Response.ResponseListener> responseListeners)
{
listeners = responseListeners.stream()
listeners = new ArrayList<>(responseListeners.size());
responseListeners.stream()
.filter(Response.DemandedContentListener.class::isInstance)
.map(Response.DemandedContentListener.class::cast)
.collect(Collectors.toList());
.forEach(listeners::add);
}
private boolean isEmpty()

View File

@ -75,4 +75,12 @@ public interface AuthenticationStore
* @return the {@link Authentication.Result} that matches the given URI, or null
*/
Authentication.Result findAuthenticationResult(URI uri);
/**
* @return false if there are no stored authentication results, true if there may be some.
*/
default boolean hasAuthenticationResults()
{
return true;
}
}

View File

@ -44,7 +44,6 @@ public class BlockheadClient extends HttpClient implements WebSocketContainerSco
public BlockheadClient()
{
super(null);
setName("Blockhead-CLIENT");
this.extensionFactory = new WebSocketExtensionFactory(this);
}