Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.conn; |
michael@0 | 29 | |
michael@0 | 30 | |
michael@0 | 31 | import java.net.InetAddress; |
michael@0 | 32 | import java.net.InetSocketAddress; |
michael@0 | 33 | import java.net.Proxy; |
michael@0 | 34 | import java.net.ProxySelector; |
michael@0 | 35 | import java.net.URI; |
michael@0 | 36 | import java.net.URISyntaxException; |
michael@0 | 37 | import java.util.List; |
michael@0 | 38 | |
michael@0 | 39 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.HttpException; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.HttpHost; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.HttpRequest; |
michael@0 | 43 | import ch.boye.httpclientandroidlib.protocol.HttpContext; |
michael@0 | 44 | |
michael@0 | 45 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
michael@0 | 46 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoutePlanner; |
michael@0 | 47 | import ch.boye.httpclientandroidlib.conn.scheme.Scheme; |
michael@0 | 48 | import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; |
michael@0 | 49 | |
michael@0 | 50 | import ch.boye.httpclientandroidlib.conn.params.ConnRouteParams; |
michael@0 | 51 | |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Default implementation of an {@link HttpRoutePlanner}. |
michael@0 | 55 | * This implementation is based on {@link java.net.ProxySelector}. |
michael@0 | 56 | * By default, it will pick up the proxy settings of the JVM, either |
michael@0 | 57 | * from system properties or from the browser running the application. |
michael@0 | 58 | * Additionally, it interprets some |
michael@0 | 59 | * {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}, |
michael@0 | 60 | * though not the {@link |
michael@0 | 61 | * ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}. |
michael@0 | 62 | * <p> |
michael@0 | 63 | * The following parameters can be used to customize the behavior of this |
michael@0 | 64 | * class: |
michael@0 | 65 | * <ul> |
michael@0 | 66 | * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li> |
michael@0 | 67 | * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li> |
michael@0 | 68 | * </ul> |
michael@0 | 69 | * |
michael@0 | 70 | * @since 4.0 |
michael@0 | 71 | */ |
michael@0 | 72 | @NotThreadSafe // e.g [gs]etProxySelector() |
michael@0 | 73 | public class ProxySelectorRoutePlanner implements HttpRoutePlanner { |
michael@0 | 74 | |
michael@0 | 75 | /** The scheme registry. */ |
michael@0 | 76 | protected final SchemeRegistry schemeRegistry; // @ThreadSafe |
michael@0 | 77 | |
michael@0 | 78 | /** The proxy selector to use, or <code>null</code> for system default. */ |
michael@0 | 79 | protected ProxySelector proxySelector; |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Creates a new proxy selector route planner. |
michael@0 | 83 | * |
michael@0 | 84 | * @param schreg the scheme registry |
michael@0 | 85 | * @param prosel the proxy selector, or |
michael@0 | 86 | * <code>null</code> for the system default |
michael@0 | 87 | */ |
michael@0 | 88 | public ProxySelectorRoutePlanner(SchemeRegistry schreg, |
michael@0 | 89 | ProxySelector prosel) { |
michael@0 | 90 | |
michael@0 | 91 | if (schreg == null) { |
michael@0 | 92 | throw new IllegalArgumentException |
michael@0 | 93 | ("SchemeRegistry must not be null."); |
michael@0 | 94 | } |
michael@0 | 95 | schemeRegistry = schreg; |
michael@0 | 96 | proxySelector = prosel; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /** |
michael@0 | 100 | * Obtains the proxy selector to use. |
michael@0 | 101 | * |
michael@0 | 102 | * @return the proxy selector, or <code>null</code> for the system default |
michael@0 | 103 | */ |
michael@0 | 104 | public ProxySelector getProxySelector() { |
michael@0 | 105 | return this.proxySelector; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Sets the proxy selector to use. |
michael@0 | 110 | * |
michael@0 | 111 | * @param prosel the proxy selector, or |
michael@0 | 112 | * <code>null</code> to use the system default |
michael@0 | 113 | */ |
michael@0 | 114 | public void setProxySelector(ProxySelector prosel) { |
michael@0 | 115 | this.proxySelector = prosel; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | public HttpRoute determineRoute(HttpHost target, |
michael@0 | 119 | HttpRequest request, |
michael@0 | 120 | HttpContext context) |
michael@0 | 121 | throws HttpException { |
michael@0 | 122 | |
michael@0 | 123 | if (request == null) { |
michael@0 | 124 | throw new IllegalStateException |
michael@0 | 125 | ("Request must not be null."); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // If we have a forced route, we can do without a target. |
michael@0 | 129 | HttpRoute route = |
michael@0 | 130 | ConnRouteParams.getForcedRoute(request.getParams()); |
michael@0 | 131 | if (route != null) |
michael@0 | 132 | return route; |
michael@0 | 133 | |
michael@0 | 134 | // If we get here, there is no forced route. |
michael@0 | 135 | // So we need a target to compute a route. |
michael@0 | 136 | |
michael@0 | 137 | if (target == null) { |
michael@0 | 138 | throw new IllegalStateException |
michael@0 | 139 | ("Target host must not be null."); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | final InetAddress local = |
michael@0 | 143 | ConnRouteParams.getLocalAddress(request.getParams()); |
michael@0 | 144 | final HttpHost proxy = determineProxy(target, request, context); |
michael@0 | 145 | |
michael@0 | 146 | final Scheme schm = |
michael@0 | 147 | this.schemeRegistry.getScheme(target.getSchemeName()); |
michael@0 | 148 | // as it is typically used for TLS/SSL, we assume that |
michael@0 | 149 | // a layered scheme implies a secure connection |
michael@0 | 150 | final boolean secure = schm.isLayered(); |
michael@0 | 151 | |
michael@0 | 152 | if (proxy == null) { |
michael@0 | 153 | route = new HttpRoute(target, local, secure); |
michael@0 | 154 | } else { |
michael@0 | 155 | route = new HttpRoute(target, local, proxy, secure); |
michael@0 | 156 | } |
michael@0 | 157 | return route; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Determines a proxy for the given target. |
michael@0 | 162 | * |
michael@0 | 163 | * @param target the planned target, never <code>null</code> |
michael@0 | 164 | * @param request the request to be sent, never <code>null</code> |
michael@0 | 165 | * @param context the context, or <code>null</code> |
michael@0 | 166 | * |
michael@0 | 167 | * @return the proxy to use, or <code>null</code> for a direct route |
michael@0 | 168 | * |
michael@0 | 169 | * @throws HttpException |
michael@0 | 170 | * in case of system proxy settings that cannot be handled |
michael@0 | 171 | */ |
michael@0 | 172 | protected HttpHost determineProxy(HttpHost target, |
michael@0 | 173 | HttpRequest request, |
michael@0 | 174 | HttpContext context) |
michael@0 | 175 | throws HttpException { |
michael@0 | 176 | |
michael@0 | 177 | // the proxy selector can be 'unset', so we better deal with null here |
michael@0 | 178 | ProxySelector psel = this.proxySelector; |
michael@0 | 179 | if (psel == null) |
michael@0 | 180 | psel = ProxySelector.getDefault(); |
michael@0 | 181 | if (psel == null) |
michael@0 | 182 | return null; |
michael@0 | 183 | |
michael@0 | 184 | URI targetURI = null; |
michael@0 | 185 | try { |
michael@0 | 186 | targetURI = new URI(target.toURI()); |
michael@0 | 187 | } catch (URISyntaxException usx) { |
michael@0 | 188 | throw new HttpException |
michael@0 | 189 | ("Cannot convert host to URI: " + target, usx); |
michael@0 | 190 | } |
michael@0 | 191 | List<Proxy> proxies = psel.select(targetURI); |
michael@0 | 192 | |
michael@0 | 193 | Proxy p = chooseProxy(proxies, target, request, context); |
michael@0 | 194 | |
michael@0 | 195 | HttpHost result = null; |
michael@0 | 196 | if (p.type() == Proxy.Type.HTTP) { |
michael@0 | 197 | // convert the socket address to an HttpHost |
michael@0 | 198 | if (!(p.address() instanceof InetSocketAddress)) { |
michael@0 | 199 | throw new HttpException |
michael@0 | 200 | ("Unable to handle non-Inet proxy address: "+p.address()); |
michael@0 | 201 | } |
michael@0 | 202 | final InetSocketAddress isa = (InetSocketAddress) p.address(); |
michael@0 | 203 | // assume default scheme (http) |
michael@0 | 204 | result = new HttpHost(getHost(isa), isa.getPort()); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | return result; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | /** |
michael@0 | 211 | * Obtains a host from an {@link InetSocketAddress}. |
michael@0 | 212 | * |
michael@0 | 213 | * @param isa the socket address |
michael@0 | 214 | * |
michael@0 | 215 | * @return a host string, either as a symbolic name or |
michael@0 | 216 | * as a literal IP address string |
michael@0 | 217 | * <br/> |
michael@0 | 218 | * (TODO: determine format for IPv6 addresses, with or without [brackets]) |
michael@0 | 219 | */ |
michael@0 | 220 | protected String getHost(InetSocketAddress isa) { |
michael@0 | 221 | |
michael@0 | 222 | //@@@ Will this work with literal IPv6 addresses, or do we |
michael@0 | 223 | //@@@ need to wrap these in [] for the string representation? |
michael@0 | 224 | //@@@ Having it in this method at least allows for easy workarounds. |
michael@0 | 225 | return isa.isUnresolved() ? |
michael@0 | 226 | isa.getHostName() : isa.getAddress().getHostAddress(); |
michael@0 | 227 | |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | /** |
michael@0 | 231 | * Chooses a proxy from a list of available proxies. |
michael@0 | 232 | * The default implementation just picks the first non-SOCKS proxy |
michael@0 | 233 | * from the list. If there are only SOCKS proxies, |
michael@0 | 234 | * {@link Proxy#NO_PROXY Proxy.NO_PROXY} is returned. |
michael@0 | 235 | * Derived classes may implement more advanced strategies, |
michael@0 | 236 | * such as proxy rotation if there are multiple options. |
michael@0 | 237 | * |
michael@0 | 238 | * @param proxies the list of proxies to choose from, |
michael@0 | 239 | * never <code>null</code> or empty |
michael@0 | 240 | * @param target the planned target, never <code>null</code> |
michael@0 | 241 | * @param request the request to be sent, never <code>null</code> |
michael@0 | 242 | * @param context the context, or <code>null</code> |
michael@0 | 243 | * |
michael@0 | 244 | * @return a proxy type |
michael@0 | 245 | */ |
michael@0 | 246 | protected Proxy chooseProxy(List<Proxy> proxies, |
michael@0 | 247 | HttpHost target, |
michael@0 | 248 | HttpRequest request, |
michael@0 | 249 | HttpContext context) { |
michael@0 | 250 | |
michael@0 | 251 | if ((proxies == null) || proxies.isEmpty()) { |
michael@0 | 252 | throw new IllegalArgumentException |
michael@0 | 253 | ("Proxy list must not be empty."); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | Proxy result = null; |
michael@0 | 257 | |
michael@0 | 258 | // check the list for one we can use |
michael@0 | 259 | for (int i=0; (result == null) && (i < proxies.size()); i++) { |
michael@0 | 260 | |
michael@0 | 261 | Proxy p = proxies.get(i); |
michael@0 | 262 | switch (p.type()) { |
michael@0 | 263 | |
michael@0 | 264 | case DIRECT: |
michael@0 | 265 | case HTTP: |
michael@0 | 266 | result = p; |
michael@0 | 267 | break; |
michael@0 | 268 | |
michael@0 | 269 | case SOCKS: |
michael@0 | 270 | // SOCKS hosts are not handled on the route level. |
michael@0 | 271 | // The socket may make use of the SOCKS host though. |
michael@0 | 272 | break; |
michael@0 | 273 | } |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | if (result == null) { |
michael@0 | 277 | //@@@ log as warning or info that only a socks proxy is available? |
michael@0 | 278 | // result can only be null if all proxies are socks proxies |
michael@0 | 279 | // socks proxies are not handled on the route planning level |
michael@0 | 280 | result = Proxy.NO_PROXY; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | return result; |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | } |
michael@0 | 287 |