/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/ProxySelectorRoutePlanner.java

https://github.com/rillian/firefox · Java · 279 lines · 119 code · 38 blank · 122 comment · 18 complexity · 401206df4cd78ffd8f3150e53a5b2f0e MD5 · raw file

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