Refactored checks for secure schemes.

This commit is contained in:
Simone Bordet 2015-10-29 19:27:33 +01:00
parent 6b0e1d48c5
commit 915a905df4
4 changed files with 16 additions and 8 deletions

View File

@ -1039,7 +1039,7 @@ public class HttpClient extends ContainerLifeCycle
{ {
if (port > 0) if (port > 0)
return port; return port;
else if (HttpScheme.HTTPS.is(scheme) || HttpScheme.WSS.is(scheme)) else if (isSchemeSecure(scheme))
return 443; return 443;
else else
return 80; return 80;
@ -1047,12 +1047,17 @@ public class HttpClient extends ContainerLifeCycle
public boolean isDefaultPort(String scheme, int port) public boolean isDefaultPort(String scheme, int port)
{ {
if (HttpScheme.HTTPS.is(scheme) || HttpScheme.WSS.is(scheme)) if (isSchemeSecure(scheme))
return port == 443; return port == 443;
else else
return port == 80; return port == 80;
} }
public boolean isSchemeSecure(String scheme)
{
return HttpScheme.HTTPS.is(scheme) || HttpScheme.WSS.is(scheme);
}
private class ContentDecoderFactorySet implements Set<ContentDecoder.Factory> private class ContentDecoderFactorySet implements Set<ContentDecoder.Factory>
{ {
private final Set<ContentDecoder.Factory> set = new HashSet<>(); private final Set<ContentDecoder.Factory> set = new HashSet<>();

View File

@ -31,7 +31,6 @@ import org.eclipse.jetty.client.api.Destination;
import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.io.ssl.SslClientConnectionFactory; import org.eclipse.jetty.io.ssl.SslClientConnectionFactory;
import org.eclipse.jetty.util.BlockingArrayQueue; import org.eclipse.jetty.util.BlockingArrayQueue;
@ -76,7 +75,7 @@ public abstract class HttpDestination extends ContainerLifeCycle implements Dest
} }
else else
{ {
if (HttpScheme.HTTPS.is(getScheme()) || HttpScheme.WSS.is(getScheme())) if (isSecure())
connectionFactory = newSslClientConnectionFactory(connectionFactory); connectionFactory = newSslClientConnectionFactory(connectionFactory);
} }
this.connectionFactory = connectionFactory; this.connectionFactory = connectionFactory;
@ -97,6 +96,11 @@ public abstract class HttpDestination extends ContainerLifeCycle implements Dest
return new SslClientConnectionFactory(client.getSslContextFactory(), client.getByteBufferPool(), client.getExecutor(), connectionFactory); return new SslClientConnectionFactory(client.getSslContextFactory(), client.getByteBufferPool(), client.getExecutor(), connectionFactory);
} }
public boolean isSecure()
{
return client.isSchemeSecure(getScheme());
}
public HttpClient getHttpClient() public HttpClient getHttpClient()
{ {
return client; return client;

View File

@ -107,7 +107,7 @@ public class HttpProxy extends ProxyConfiguration.Proxy
public void succeeded(Connection connection) public void succeeded(Connection connection)
{ {
HttpDestination destination = (HttpDestination)context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY); HttpDestination destination = (HttpDestination)context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY);
if (HttpScheme.HTTPS.is(destination.getScheme()) || HttpScheme.WSS.is(destination.getScheme())) if (destination.isSecure())
{ {
SslContextFactory sslContextFactory = destination.getHttpClient().getSslContextFactory(); SslContextFactory sslContextFactory = destination.getHttpClient().getSslContextFactory();
if (sslContextFactory != null) if (sslContextFactory != null)

View File

@ -27,7 +27,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.eclipse.jetty.client.api.Connection; import org.eclipse.jetty.client.api.Connection;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.AbstractConnection; import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.ClientConnectionFactory; import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EndPoint;
@ -196,7 +195,7 @@ public class Socks4Proxy extends ProxyConfiguration.Proxy
HttpDestination destination = (HttpDestination)context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY); HttpDestination destination = (HttpDestination)context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY);
HttpClient client = destination.getHttpClient(); HttpClient client = destination.getHttpClient();
ClientConnectionFactory connectionFactory = this.connectionFactory; ClientConnectionFactory connectionFactory = this.connectionFactory;
if (HttpScheme.HTTPS.is(destination.getScheme()) || HttpScheme.WSS.is(destination.getScheme())) if (destination.isSecure())
connectionFactory = new SslClientConnectionFactory(client.getSslContextFactory(), client.getByteBufferPool(), client.getExecutor(), connectionFactory); connectionFactory = new SslClientConnectionFactory(client.getSslContextFactory(), client.getByteBufferPool(), client.getExecutor(), connectionFactory);
org.eclipse.jetty.io.Connection newConnection = connectionFactory.newConnection(getEndPoint(), context); org.eclipse.jetty.io.Connection newConnection = connectionFactory.newConnection(getEndPoint(), context);
getEndPoint().upgrade(newConnection); getEndPoint().upgrade(newConnection);