Home | History | Annotate | Download | only in okhttp
      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, hostname verifier, and certificate pinner.
     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 HttpUrl url;
     40   final Dns dns;
     41   final SocketFactory socketFactory;
     42   final Authenticator authenticator;
     43   final List<Protocol> protocols;
     44   final List<ConnectionSpec> connectionSpecs;
     45   final ProxySelector proxySelector;
     46   final Proxy proxy;
     47   final SSLSocketFactory sslSocketFactory;
     48   final HostnameVerifier hostnameVerifier;
     49   final CertificatePinner certificatePinner;
     50 
     51   public Address(String uriHost, int uriPort, Dns dns, SocketFactory socketFactory,
     52       SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier,
     53       CertificatePinner certificatePinner, Authenticator authenticator, Proxy proxy,
     54       List<Protocol> protocols, List<ConnectionSpec> connectionSpecs, ProxySelector proxySelector) {
     55     this.url = new HttpUrl.Builder()
     56         .scheme(sslSocketFactory != null ? "https" : "http")
     57         .host(uriHost)
     58         .port(uriPort)
     59         .build();
     60 
     61     if (dns == null) throw new IllegalArgumentException("dns == null");
     62     this.dns = dns;
     63 
     64     if (socketFactory == null) throw new IllegalArgumentException("socketFactory == null");
     65     this.socketFactory = socketFactory;
     66 
     67     if (authenticator == null) throw new IllegalArgumentException("authenticator == null");
     68     this.authenticator = authenticator;
     69 
     70     if (protocols == null) throw new IllegalArgumentException("protocols == null");
     71     this.protocols = Util.immutableList(protocols);
     72 
     73     if (connectionSpecs == null) throw new IllegalArgumentException("connectionSpecs == null");
     74     this.connectionSpecs = Util.immutableList(connectionSpecs);
     75 
     76     if (proxySelector == null) throw new IllegalArgumentException("proxySelector == null");
     77     this.proxySelector = proxySelector;
     78 
     79     this.proxy = proxy;
     80     this.sslSocketFactory = sslSocketFactory;
     81     this.hostnameVerifier = hostnameVerifier;
     82     this.certificatePinner = certificatePinner;
     83   }
     84 
     85   /**
     86    * Returns a URL with the hostname and port of the origin server. The path, query, and fragment of
     87    * this URL are always empty, since they are not significant for planning a route.
     88    */
     89   public HttpUrl url() {
     90     return url;
     91   }
     92 
     93   /**
     94    * Returns the hostname of the origin server.
     95    *
     96    * @deprecated prefer {@code address.url().host()}.
     97    */
     98   @Deprecated
     99   public String getUriHost() {
    100     return url.host();
    101   }
    102 
    103   /**
    104    * Returns the port of the origin server; typically 80 or 443. Unlike
    105    * may {@code getPort()} accessors, this method never returns -1.
    106    *
    107    * @deprecated prefer {@code address.url().port()}.
    108    */
    109   @Deprecated
    110   public int getUriPort() {
    111     return url.port();
    112   }
    113 
    114   /** Returns the service that will be used to resolve IP addresses for hostnames. */
    115   public Dns getDns() {
    116     return dns;
    117   }
    118 
    119   /** Returns the socket factory for new connections. */
    120   public SocketFactory getSocketFactory() {
    121     return socketFactory;
    122   }
    123 
    124   /** Returns the client's authenticator. */
    125   public Authenticator getAuthenticator() {
    126     return authenticator;
    127   }
    128 
    129   /**
    130    * Returns the protocols the client supports. This method always returns a
    131    * non-null list that contains minimally {@link Protocol#HTTP_1_1}.
    132    */
    133   public List<Protocol> getProtocols() {
    134     return protocols;
    135   }
    136 
    137   public List<ConnectionSpec> getConnectionSpecs() {
    138     return connectionSpecs;
    139   }
    140 
    141   /**
    142    * Returns this address's proxy selector. Only used if the proxy is null. If none of this
    143    * selector's proxies are reachable, a direct connection will be attempted.
    144    */
    145   public ProxySelector getProxySelector() {
    146     return proxySelector;
    147   }
    148 
    149   /**
    150    * Returns this address's explicitly-specified HTTP proxy, or null to
    151    * delegate to the {@linkplain #getProxySelector proxy selector}.
    152    */
    153   public Proxy getProxy() {
    154     return proxy;
    155   }
    156 
    157   /** Returns the SSL socket factory, or null if this is not an HTTPS address. */
    158   public SSLSocketFactory getSslSocketFactory() {
    159     return sslSocketFactory;
    160   }
    161 
    162   /** Returns the hostname verifier, or null if this is not an HTTPS address. */
    163   public HostnameVerifier getHostnameVerifier() {
    164     return hostnameVerifier;
    165   }
    166 
    167   /** Returns this address's certificate pinner, or null if this is not an HTTPS address. */
    168   public CertificatePinner getCertificatePinner() {
    169     return certificatePinner;
    170   }
    171 
    172   @Override public boolean equals(Object other) {
    173     if (other instanceof Address) {
    174       Address that = (Address) other;
    175       return this.url.equals(that.url)
    176           && this.dns.equals(that.dns)
    177           && this.authenticator.equals(that.authenticator)
    178           && this.protocols.equals(that.protocols)
    179           && this.connectionSpecs.equals(that.connectionSpecs)
    180           && this.proxySelector.equals(that.proxySelector)
    181           && equal(this.proxy, that.proxy)
    182           && equal(this.sslSocketFactory, that.sslSocketFactory)
    183           && equal(this.hostnameVerifier, that.hostnameVerifier)
    184           && equal(this.certificatePinner, that.certificatePinner);
    185     }
    186     return false;
    187   }
    188 
    189   @Override public int hashCode() {
    190     int result = 17;
    191     result = 31 * result + url.hashCode();
    192     result = 31 * result + dns.hashCode();
    193     result = 31 * result + authenticator.hashCode();
    194     result = 31 * result + protocols.hashCode();
    195     result = 31 * result + connectionSpecs.hashCode();
    196     result = 31 * result + proxySelector.hashCode();
    197     result = 31 * result + (proxy != null ? proxy.hashCode() : 0);
    198     result = 31 * result + (sslSocketFactory != null ? sslSocketFactory.hashCode() : 0);
    199     result = 31 * result + (hostnameVerifier != null ? hostnameVerifier.hashCode() : 0);
    200     result = 31 * result + (certificatePinner != null ? certificatePinner.hashCode() : 0);
    201     return result;
    202   }
    203 }
    204