1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Make OkHostnameVerifier a constant. This fixes connection reuse.

Previously HTTPS connection reuse was broken because the
hostname verifier is included as a route identifier, and
with a different hostname verifier on every request we weren't
reusing connections.
This commit is contained in:
jwilson
2013-06-15 09:03:38 -04:00
parent 1482bc4032
commit 812ba5ea63
3 changed files with 7 additions and 2 deletions

View File

@@ -284,7 +284,7 @@ public final class OkHttpClient {
: HttpsURLConnection.getDefaultSSLSocketFactory();
result.hostnameVerifier = hostnameVerifier != null
? hostnameVerifier
: new OkHostnameVerifier();
: OkHostnameVerifier.INSTANCE;
result.authenticator = authenticator != null
? authenticator
: HttpAuthenticator.SYSTEM_DEFAULT;

View File

@@ -36,6 +36,8 @@ import javax.security.auth.x500.X500Principal;
* href="http://www.ietf.org/rfc/rfc2818.txt">RFC 2818</a>.
*/
public final class OkHostnameVerifier implements HostnameVerifier {
public static final OkHostnameVerifier INSTANCE = new OkHostnameVerifier();
/**
* Quick and dirty pattern to differentiate IP addresses from hostnames. This
* is an approximation of Android's private InetAddress#isNumeric API.
@@ -53,6 +55,9 @@ public final class OkHostnameVerifier implements HostnameVerifier {
private static final int ALT_DNS_NAME = 2;
private static final int ALT_IPA_NAME = 7;
private OkHostnameVerifier() {
}
public boolean verify(String host, SSLSession session) {
try {
Certificate[] certificates = session.getPeerCertificates();

View File

@@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue;
* itself includes tests from the Apache HTTP Client test suite.
*/
public final class HostnameVerifierTest {
private HostnameVerifier verifier = new OkHostnameVerifier();
private HostnameVerifier verifier = OkHostnameVerifier.INSTANCE;
@Test public void verify() throws Exception {
FakeSSLSession session = new FakeSSLSession();