Home | History | Annotate | Download | only in http
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package org.apache.harmony.luni.internal.net.www.protocol.http;
     18 
     19 import java.net.InetSocketAddress;
     20 import java.net.Proxy;
     21 import java.net.SocketAddress;
     22 import java.net.URI;
     23 
     24 import org.apache.harmony.luni.util.Msg;
     25 
     26 /**
     27  * An <code>HttpConfiguration</code> contains all the details needed to create an http connection
     28  * and to compare whether or not two connections are the same.  An HttpConfiguration
     29  * will either consist of a <code>Proxy<code> or a port number (<code>int</code>)
     30  * and host name (<code>String</code>) or all three,  depending on whether or not a
     31  * <code>Proxy</code> is used and the type of <code>Proxy</code> it is.
     32  *
     33  * <code>HttpConfiguration</code> is used as a key by <code>HttpConnectionPool</code>
     34  * to retrieve <code>HttpConnection</code>s from its connection pool.
     35  */
     36 public class HttpConfiguration {
     37 
     38     private Proxy proxy;
     39     private int hostPort;
     40     private String hostName;
     41     private URI uri;
     42 
     43     public HttpConfiguration(URI uri) {
     44         this.uri = uri;
     45         this.hostName = uri.getHost();
     46         this.hostPort = uri.getPort();
     47         if(hostPort == -1) {
     48             if(uri.getScheme().equals("https")) { //$NON-NLS-1$
     49                 hostPort = 443;
     50             } else {
     51                 hostPort = 80;
     52             }
     53         }
     54     }
     55 
     56     public HttpConfiguration(URI uri, Proxy proxy) {
     57         this.uri = uri;
     58         this.proxy = proxy;
     59         if (proxy.type() == Proxy.Type.HTTP) {
     60             SocketAddress proxyAddr = proxy.address();
     61             if (!(proxyAddr instanceof InetSocketAddress)) {
     62                throw new IllegalArgumentException(Msg.getString(
     63                    "K0316", proxyAddr.getClass())); //$NON-NLS-1$
     64             }
     65             InetSocketAddress iProxyAddr = (InetSocketAddress) proxyAddr;
     66             this.hostName = iProxyAddr.getHostName();
     67             this.hostPort = iProxyAddr.getPort();
     68         } else {
     69             // using SOCKS proxy
     70             this.hostName = uri.getHost();
     71             this.hostPort = uri.getPort();
     72             if(hostPort == -1) {
     73                 if(uri.getScheme().equals("https")) { //$NON-NLS-1$
     74                     hostPort = 443;
     75                 } else {
     76                     hostPort = 80;
     77                 }
     78             }
     79         }
     80         this.uri = uri;
     81         SocketAddress proxyAddr = proxy.address();
     82         if (!(proxyAddr instanceof InetSocketAddress)) {
     83            throw new IllegalArgumentException(Msg.getString(
     84                "K0316", proxyAddr.getClass())); //$NON-NLS-1$
     85         }
     86         InetSocketAddress iProxyAddr = (InetSocketAddress) proxyAddr;
     87         this.hostName = iProxyAddr.getHostName();
     88         this.hostPort = iProxyAddr.getPort();
     89     }
     90 
     91     /**
     92      * Returns true if this configuration uses a Proxy
     93      */
     94     public boolean usesProxy() {
     95         return proxy != null;
     96     }
     97 
     98     /**
     99      * Returns the Proxy for this configuration, or null if a proxy
    100      * is not used
    101      */
    102     public Proxy getProxy() {
    103         return proxy;
    104     }
    105 
    106     /**
    107      * Returns the host name for this configuration, or null if an http Proxy is used
    108      */
    109     public String getHostName() {
    110         return hostName;
    111     }
    112 
    113     /**
    114      * Returns the port for this configuration, or 0 if an http Proxy is used
    115      */
    116     public int getHostPort() {
    117         return hostPort;
    118     }
    119 
    120     @Override
    121     public boolean equals(Object arg0) {
    122         if(!(arg0 instanceof HttpConfiguration)) {
    123             return false;
    124         } else {
    125             HttpConfiguration config = (HttpConfiguration)arg0;
    126             if(config.proxy != null && proxy != null) {
    127                 return config.proxy.equals(proxy) && uri.equals(config.uri);
    128             }
    129             return uri.equals(config.uri);
    130         }
    131     }
    132 
    133     @Override
    134     public int hashCode() {
    135         return uri.hashCode();
    136     }
    137 
    138 }