Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | |
michael@0 | 33 | import ch.boye.httpclientandroidlib.annotation.ThreadSafe; |
michael@0 | 34 | |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HttpException; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.HttpHost; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.HttpRequest; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.protocol.HttpContext; |
michael@0 | 39 | |
michael@0 | 40 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoutePlanner; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.conn.scheme.Scheme; |
michael@0 | 43 | import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; |
michael@0 | 44 | |
michael@0 | 45 | import ch.boye.httpclientandroidlib.conn.params.ConnRouteParams; |
michael@0 | 46 | |
michael@0 | 47 | /** |
michael@0 | 48 | * Default implementation of an {@link HttpRoutePlanner}. This implementation |
michael@0 | 49 | * is based on {@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames parameters}. |
michael@0 | 50 | * It will not make use of any Java system properties, nor of system or |
michael@0 | 51 | * browser proxy settings. |
michael@0 | 52 | * <p> |
michael@0 | 53 | * The following parameters can be used to customize the behavior of this |
michael@0 | 54 | * class: |
michael@0 | 55 | * <ul> |
michael@0 | 56 | * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li> |
michael@0 | 57 | * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li> |
michael@0 | 58 | * <li>{@link ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames#FORCED_ROUTE}</li> |
michael@0 | 59 | * </ul> |
michael@0 | 60 | * |
michael@0 | 61 | * @since 4.0 |
michael@0 | 62 | */ |
michael@0 | 63 | @ThreadSafe |
michael@0 | 64 | public class DefaultHttpRoutePlanner implements HttpRoutePlanner { |
michael@0 | 65 | |
michael@0 | 66 | /** The scheme registry. */ |
michael@0 | 67 | protected final SchemeRegistry schemeRegistry; // class is @ThreadSafe |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * Creates a new default route planner. |
michael@0 | 71 | * |
michael@0 | 72 | * @param schreg the scheme registry |
michael@0 | 73 | */ |
michael@0 | 74 | public DefaultHttpRoutePlanner(SchemeRegistry schreg) { |
michael@0 | 75 | if (schreg == null) { |
michael@0 | 76 | throw new IllegalArgumentException |
michael@0 | 77 | ("SchemeRegistry must not be null."); |
michael@0 | 78 | } |
michael@0 | 79 | schemeRegistry = schreg; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | public HttpRoute determineRoute(HttpHost target, |
michael@0 | 83 | HttpRequest request, |
michael@0 | 84 | HttpContext context) |
michael@0 | 85 | throws HttpException { |
michael@0 | 86 | |
michael@0 | 87 | if (request == null) { |
michael@0 | 88 | throw new IllegalStateException |
michael@0 | 89 | ("Request must not be null."); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // If we have a forced route, we can do without a target. |
michael@0 | 93 | HttpRoute route = |
michael@0 | 94 | ConnRouteParams.getForcedRoute(request.getParams()); |
michael@0 | 95 | if (route != null) |
michael@0 | 96 | return route; |
michael@0 | 97 | |
michael@0 | 98 | // If we get here, there is no forced route. |
michael@0 | 99 | // So we need a target to compute a route. |
michael@0 | 100 | |
michael@0 | 101 | if (target == null) { |
michael@0 | 102 | throw new IllegalStateException |
michael@0 | 103 | ("Target host must not be null."); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | final InetAddress local = |
michael@0 | 107 | ConnRouteParams.getLocalAddress(request.getParams()); |
michael@0 | 108 | final HttpHost proxy = |
michael@0 | 109 | ConnRouteParams.getDefaultProxy(request.getParams()); |
michael@0 | 110 | |
michael@0 | 111 | final Scheme schm; |
michael@0 | 112 | try { |
michael@0 | 113 | schm = schemeRegistry.getScheme(target.getSchemeName()); |
michael@0 | 114 | } catch (IllegalStateException ex) { |
michael@0 | 115 | throw new HttpException(ex.getMessage()); |
michael@0 | 116 | } |
michael@0 | 117 | // as it is typically used for TLS/SSL, we assume that |
michael@0 | 118 | // a layered scheme implies a secure connection |
michael@0 | 119 | final boolean secure = schm.isLayered(); |
michael@0 | 120 | |
michael@0 | 121 | if (proxy == null) { |
michael@0 | 122 | route = new HttpRoute(target, local, secure); |
michael@0 | 123 | } else { |
michael@0 | 124 | route = new HttpRoute(target, local, proxy, secure); |
michael@0 | 125 | } |
michael@0 | 126 | return route; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | } |