1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp; 17 18 import com.squareup.okhttp.internal.Util; 19 import java.net.Proxy; 20 import java.net.ProxySelector; 21 import java.util.List; 22 import javax.net.SocketFactory; 23 import javax.net.ssl.HostnameVerifier; 24 import javax.net.ssl.SSLSocketFactory; 25 26 import static com.squareup.okhttp.internal.Util.equal; 27 28 /** 29 * A specification for a connection to an origin server. For simple connections, 30 * this is the server's hostname and port. If an explicit proxy is requested (or 31 * {@linkplain Proxy#NO_PROXY no proxy} is explicitly requested), this also includes 32 * that proxy information. For secure connections the address also includes the 33 * SSL socket factory and hostname verifier. 34 * 35 * <p>HTTP requests that share the same {@code Address} may also share the same 36 * {@link Connection}. 37 */ 38 public final class Address { 39 final Proxy proxy; 40 final String uriHost; 41 final int uriPort; 42 final SocketFactory socketFactory; 43 final SSLSocketFactory sslSocketFactory; 44 final HostnameVerifier hostnameVerifier; 45 final CertificatePinner certificatePinner; 46 final Authenticator authenticator; 47 final List<Protocol> protocols; 48 final List<ConnectionSpec> connectionSpecs; 49 final ProxySelector proxySelector; 50 Address(String uriHost, int uriPort, SocketFactory socketFactory, SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier, CertificatePinner certificatePinner, Authenticator authenticator, Proxy proxy, List<Protocol> protocols, List<ConnectionSpec> connectionSpecs, ProxySelector proxySelector)51 public Address(String uriHost, int uriPort, SocketFactory socketFactory, 52 SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier, 53 CertificatePinner certificatePinner, Authenticator authenticator, Proxy proxy, 54 List<Protocol> protocols, List<ConnectionSpec> connectionSpecs, ProxySelector proxySelector) { 55 if (uriHost == null) throw new NullPointerException("uriHost == null"); 56 if (uriPort <= 0) throw new IllegalArgumentException("uriPort <= 0: " + uriPort); 57 if (authenticator == null) throw new IllegalArgumentException("authenticator == null"); 58 if (protocols == null) throw new IllegalArgumentException("protocols == null"); 59 if (proxySelector == null) throw new IllegalArgumentException("proxySelector == null"); 60 this.proxy = proxy; 61 this.uriHost = uriHost; 62 this.uriPort = uriPort; 63 this.socketFactory = socketFactory; 64 this.sslSocketFactory = sslSocketFactory; 65 this.hostnameVerifier = hostnameVerifier; 66 this.certificatePinner = certificatePinner; 67 this.authenticator = authenticator; 68 this.protocols = Util.immutableList(protocols); 69 this.connectionSpecs = Util.immutableList(connectionSpecs); 70 this.proxySelector = proxySelector; 71 } 72 73 /** Returns the hostname of the origin server. */ getRfc2732Host()74 public String getRfc2732Host() { 75 return uriHost; 76 } 77 78 /** 79 * Returns the port of the origin server; typically 80 or 443. Unlike 80 * may {@code getPort()} accessors, this method never returns -1. 81 */ getUriPort()82 public int getUriPort() { 83 return uriPort; 84 } 85 86 /** Returns the socket factory for new connections. */ getSocketFactory()87 public SocketFactory getSocketFactory() { 88 return socketFactory; 89 } 90 91 /** 92 * Returns the SSL socket factory, or null if this is not an HTTPS 93 * address. 94 */ getSslSocketFactory()95 public SSLSocketFactory getSslSocketFactory() { 96 return sslSocketFactory; 97 } 98 99 /** 100 * Returns the hostname verifier, or null if this is not an HTTPS 101 * address. 102 */ getHostnameVerifier()103 public HostnameVerifier getHostnameVerifier() { 104 return hostnameVerifier; 105 } 106 107 /** 108 * Returns the client's authenticator. This method never returns null. 109 */ getAuthenticator()110 public Authenticator getAuthenticator() { 111 return authenticator; 112 } 113 114 /** 115 * Returns the protocols the client supports. This method always returns a 116 * non-null list that contains minimally {@link Protocol#HTTP_1_1}. 117 */ getProtocols()118 public List<Protocol> getProtocols() { 119 return protocols; 120 } 121 getConnectionSpecs()122 public List<ConnectionSpec> getConnectionSpecs() { 123 return connectionSpecs; 124 } 125 126 /** 127 * Returns this address's explicitly-specified HTTP proxy, or null to 128 * delegate to the {@linkplain #getProxySelector proxy selector}. 129 */ getProxy()130 public Proxy getProxy() { 131 return proxy; 132 } 133 134 /** 135 * Returns this address's proxy selector. Only used if the proxy is null. If none of this 136 * selector's proxies are reachable, a direct connection will be attempted. 137 */ getProxySelector()138 public ProxySelector getProxySelector() { 139 return proxySelector; 140 } 141 142 /** 143 * Returns this address's certificate pinner. Only used for secure connections. 144 */ getCertificatePinner()145 public CertificatePinner getCertificatePinner() { 146 return certificatePinner; 147 } 148 equals(Object other)149 @Override public boolean equals(Object other) { 150 if (other instanceof Address) { 151 Address that = (Address) other; 152 return equal(this.proxy, that.proxy) 153 && this.uriHost.equals(that.uriHost) 154 && this.uriPort == that.uriPort 155 && equal(this.sslSocketFactory, that.sslSocketFactory) 156 && equal(this.hostnameVerifier, that.hostnameVerifier) 157 && equal(this.certificatePinner, that.certificatePinner) 158 && equal(this.authenticator, that.authenticator) 159 && equal(this.protocols, that.protocols) 160 && equal(this.connectionSpecs, that.connectionSpecs) 161 && equal(this.proxySelector, that.proxySelector); 162 } 163 return false; 164 } 165 hashCode()166 @Override public int hashCode() { 167 int result = 17; 168 result = 31 * result + (proxy != null ? proxy.hashCode() : 0); 169 result = 31 * result + uriHost.hashCode(); 170 result = 31 * result + uriPort; 171 result = 31 * result + (sslSocketFactory != null ? sslSocketFactory.hashCode() : 0); 172 result = 31 * result + (hostnameVerifier != null ? hostnameVerifier.hashCode() : 0); 173 result = 31 * result + (certificatePinner != null ? certificatePinner.hashCode() : 0); 174 result = 31 * result + authenticator.hashCode(); 175 result = 31 * result + protocols.hashCode(); 176 result = 31 * result + connectionSpecs.hashCode(); 177 result = 31 * result + proxySelector.hashCode(); 178 return result; 179 } 180 } 181