12231db3e6bb54447a9b14cf004a6cb03c373651cjwilson/* 22231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Copyright (C) 2012 The Android Open Source Project 32231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 42231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Licensed under the Apache License, Version 2.0 (the "License"); 52231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * you may not use this file except in compliance with the License. 62231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * You may obtain a copy of the License at 72231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 82231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * http://www.apache.org/licenses/LICENSE-2.0 92231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 102231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Unless required by applicable law or agreed to in writing, software 112231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * distributed under the License is distributed on an "AS IS" BASIS, 122231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * See the License for the specific language governing permissions and 142231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * limitations under the License. 152231db3e6bb54447a9b14cf004a6cb03c373651cjwilson */ 162231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpackage com.squareup.okhttp; 172231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 18faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamathimport com.squareup.okhttp.internal.Util; 192231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonimport java.net.Proxy; 202231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonimport java.net.UnknownHostException; 21faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamathimport java.util.List; 223f968acf193178145e9d227f2f08b95e2d6a6c26Jake Whartonimport javax.net.SocketFactory; 232231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonimport javax.net.ssl.HostnameVerifier; 242231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonimport javax.net.ssl.SSLSocketFactory; 252231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 2654cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonimport static com.squareup.okhttp.internal.Util.equal; 2754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson 282231db3e6bb54447a9b14cf004a6cb03c373651cjwilson/** 292231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * A specification for a connection to an origin server. For simple connections, 302231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * this is the server's hostname and port. If an explicit proxy is requested (or 312231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * {@link Proxy#NO_PROXY no proxy} is explicitly requested), this also includes 322231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * that proxy information. For secure connections the address also includes the 332231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * SSL socket factory and hostname verifier. 342231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 352231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * <p>HTTP requests that share the same {@code Address} may also share the same 362231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * {@link Connection}. 372231db3e6bb54447a9b14cf004a6cb03c373651cjwilson */ 382231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpublic final class Address { 3954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final Proxy proxy; 4054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final String uriHost; 4154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final int uriPort; 423f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton final SocketFactory socketFactory; 4354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final SSLSocketFactory sslSocketFactory; 4454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final HostnameVerifier hostnameVerifier; 45faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath final OkAuthenticator authenticator; 463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller final List<Protocol> protocols; 472231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 483f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton public Address(String uriHost, int uriPort, SocketFactory socketFactory, 493f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier, 503f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton OkAuthenticator authenticator, Proxy proxy, List<Protocol> protocols) 513f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton throws UnknownHostException { 5254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson if (uriHost == null) throw new NullPointerException("uriHost == null"); 5354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson if (uriPort <= 0) throw new IllegalArgumentException("uriPort <= 0: " + uriPort); 54faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath if (authenticator == null) throw new IllegalArgumentException("authenticator == null"); 553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller if (protocols == null) throw new IllegalArgumentException("protocols == null"); 5654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.proxy = proxy; 5754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.uriHost = uriHost; 5854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.uriPort = uriPort; 593f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton this.socketFactory = socketFactory; 6054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.sslSocketFactory = sslSocketFactory; 6154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.hostnameVerifier = hostnameVerifier; 62faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath this.authenticator = authenticator; 633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller this.protocols = Util.immutableList(protocols); 6454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 652231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 6654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** Returns the hostname of the origin server. */ 6754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson public String getUriHost() { 6854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return uriHost; 6954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 702231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 7154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 7254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Returns the port of the origin server; typically 80 or 443. Unlike 7354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * may {@code getPort()} accessors, this method never returns -1. 7454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 7554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson public int getUriPort() { 7654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return uriPort; 7754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 782231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 793f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton /** Returns the socket factory for new connections. */ 803f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton public SocketFactory getSocketFactory() { 813f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton return socketFactory; 823f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton } 833f968acf193178145e9d227f2f08b95e2d6a6c26Jake Wharton 8454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 8554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Returns the SSL socket factory, or null if this is not an HTTPS 8654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * address. 8754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 8854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson public SSLSocketFactory getSslSocketFactory() { 8954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return sslSocketFactory; 9054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 912231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 9254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 9354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Returns the hostname verifier, or null if this is not an HTTPS 9454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * address. 9554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 9654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson public HostnameVerifier getHostnameVerifier() { 9754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return hostnameVerifier; 9854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 992231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 100faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath 101faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath /** 102faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath * Returns the client's authenticator. This method never returns null. 103faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath */ 104faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath public OkAuthenticator getAuthenticator() { 105faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath return authenticator; 106faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath } 107faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath 108faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath /** 1093c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Returns the protocols the client supports. This method always returns a 1103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * non-null list that contains minimally 1113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * {@link Protocol#HTTP_11}. 112faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath */ 1133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public List<Protocol> getProtocols() { 1143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return protocols; 115faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath } 116faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath 11754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 11854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * Returns this address's explicitly-specified HTTP proxy, or null to 11954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * delegate to the HTTP client's proxy selector. 12054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 12154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson public Proxy getProxy() { 12254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return proxy; 12354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 1242231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 12554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson @Override public boolean equals(Object other) { 12654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson if (other instanceof Address) { 12754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson Address that = (Address) other; 12854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return equal(this.proxy, that.proxy) 12954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson && this.uriHost.equals(that.uriHost) 13054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson && this.uriPort == that.uriPort 13154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson && equal(this.sslSocketFactory, that.sslSocketFactory) 132faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath && equal(this.hostnameVerifier, that.hostnameVerifier) 133faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath && equal(this.authenticator, that.authenticator) 1343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller && equal(this.protocols, that.protocols); 1352231db3e6bb54447a9b14cf004a6cb03c373651cjwilson } 13654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return false; 13754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 1382231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 13954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson @Override public int hashCode() { 14054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson int result = 17; 14154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson result = 31 * result + uriHost.hashCode(); 14254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson result = 31 * result + uriPort; 14354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson result = 31 * result + (sslSocketFactory != null ? sslSocketFactory.hashCode() : 0); 14454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson result = 31 * result + (hostnameVerifier != null ? hostnameVerifier.hashCode() : 0); 145faf49723fb689c626f69876e718c58018eff8ee7Narayan Kamath result = 31 * result + (authenticator != null ? authenticator.hashCode() : 0); 14654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson result = 31 * result + (proxy != null ? proxy.hashCode() : 0); 1473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller result = 31 * result + protocols.hashCode(); 14854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson return result; 14954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 1502231db3e6bb54447a9b14cf004a6cb03c373651cjwilson} 151