237 lines
8.4 KiB
Java
237 lines
8.4 KiB
Java
|
package okhttp3.internal.connection;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.net.InetAddress;
|
||
|
import java.net.InetSocketAddress;
|
||
|
import java.net.Proxy;
|
||
|
import java.net.SocketAddress;
|
||
|
import java.net.SocketException;
|
||
|
import java.net.URI;
|
||
|
import java.net.UnknownHostException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Collections;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.List;
|
||
|
import java.util.NoSuchElementException;
|
||
|
import o.C14851gaI;
|
||
|
import o.C14881gav;
|
||
|
import o.C14953gcr;
|
||
|
import o.C14957gcv;
|
||
|
import okhttp3.Address;
|
||
|
import okhttp3.Call;
|
||
|
import okhttp3.EventListener;
|
||
|
import okhttp3.HttpUrl;
|
||
|
import okhttp3.Route;
|
||
|
import okhttp3.internal.Util;
|
||
|
|
||
|
/* loaded from: classes.dex */
|
||
|
public final class RouteSelector {
|
||
|
public static final Companion Companion = new Companion(null);
|
||
|
private final Address address;
|
||
|
private final Call call;
|
||
|
private final EventListener eventListener;
|
||
|
private List<? extends InetSocketAddress> inetSocketAddresses;
|
||
|
private int nextProxyIndex;
|
||
|
private final List<Route> postponedRoutes;
|
||
|
private List<? extends Proxy> proxies;
|
||
|
private final RouteDatabase routeDatabase;
|
||
|
|
||
|
public RouteSelector(Address address, RouteDatabase routeDatabase, Call call, EventListener eventListener) {
|
||
|
C14957gcv.e(address, "");
|
||
|
C14957gcv.e(routeDatabase, "");
|
||
|
C14957gcv.e(call, "");
|
||
|
C14957gcv.e(eventListener, "");
|
||
|
this.address = address;
|
||
|
this.routeDatabase = routeDatabase;
|
||
|
this.call = call;
|
||
|
this.eventListener = eventListener;
|
||
|
this.proxies = C14851gaI.c;
|
||
|
this.inetSocketAddresses = C14851gaI.c;
|
||
|
this.postponedRoutes = new ArrayList();
|
||
|
resetNextProxy(address.url(), address.proxy());
|
||
|
}
|
||
|
|
||
|
public final boolean hasNext() {
|
||
|
return hasNextProxy() || (this.postponedRoutes.isEmpty() ^ true);
|
||
|
}
|
||
|
|
||
|
public final Selection next() throws IOException {
|
||
|
if (!hasNext()) {
|
||
|
throw new NoSuchElementException();
|
||
|
}
|
||
|
ArrayList arrayList = new ArrayList();
|
||
|
while (hasNextProxy()) {
|
||
|
Proxy nextProxy = nextProxy();
|
||
|
Iterator<? extends InetSocketAddress> it = this.inetSocketAddresses.iterator();
|
||
|
while (it.hasNext()) {
|
||
|
Route route = new Route(this.address, nextProxy, it.next());
|
||
|
if (this.routeDatabase.shouldPostpone(route)) {
|
||
|
this.postponedRoutes.add(route);
|
||
|
} else {
|
||
|
arrayList.add(route);
|
||
|
}
|
||
|
}
|
||
|
if (!arrayList.isEmpty()) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (arrayList.isEmpty()) {
|
||
|
C14881gav.d(arrayList, this.postponedRoutes);
|
||
|
this.postponedRoutes.clear();
|
||
|
}
|
||
|
return new Selection(arrayList);
|
||
|
}
|
||
|
|
||
|
private final void resetNextProxy(HttpUrl httpUrl, Proxy proxy) {
|
||
|
this.eventListener.proxySelectStart(this.call, httpUrl);
|
||
|
List<Proxy> resetNextProxy$selectProxies = resetNextProxy$selectProxies(proxy, httpUrl, this);
|
||
|
this.proxies = resetNextProxy$selectProxies;
|
||
|
this.nextProxyIndex = 0;
|
||
|
this.eventListener.proxySelectEnd(this.call, httpUrl, resetNextProxy$selectProxies);
|
||
|
}
|
||
|
|
||
|
private final boolean hasNextProxy() {
|
||
|
return this.nextProxyIndex < this.proxies.size();
|
||
|
}
|
||
|
|
||
|
private final Proxy nextProxy() throws IOException {
|
||
|
if (!hasNextProxy()) {
|
||
|
StringBuilder sb = new StringBuilder("No route to ");
|
||
|
sb.append(this.address.url().host());
|
||
|
sb.append("; exhausted proxy configurations: ");
|
||
|
sb.append(this.proxies);
|
||
|
throw new SocketException(sb.toString());
|
||
|
}
|
||
|
List<? extends Proxy> list = this.proxies;
|
||
|
int i = this.nextProxyIndex;
|
||
|
this.nextProxyIndex = i + 1;
|
||
|
Proxy proxy = list.get(i);
|
||
|
resetNextInetSocketAddress(proxy);
|
||
|
return proxy;
|
||
|
}
|
||
|
|
||
|
private final void resetNextInetSocketAddress(Proxy proxy) throws IOException {
|
||
|
String host;
|
||
|
int port;
|
||
|
List<InetAddress> lookup;
|
||
|
ArrayList arrayList = new ArrayList();
|
||
|
this.inetSocketAddresses = arrayList;
|
||
|
if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
|
||
|
host = this.address.url().host();
|
||
|
port = this.address.url().port();
|
||
|
} else {
|
||
|
SocketAddress address = proxy.address();
|
||
|
if (!(address instanceof InetSocketAddress)) {
|
||
|
throw new IllegalArgumentException(C14957gcv.c("Proxy.address() is not an InetSocketAddress: ", address.getClass()).toString());
|
||
|
}
|
||
|
Companion companion = Companion;
|
||
|
C14957gcv.c(address, "");
|
||
|
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
|
||
|
host = companion.getSocketHost(inetSocketAddress);
|
||
|
port = inetSocketAddress.getPort();
|
||
|
}
|
||
|
if (port <= 0 || port >= 65536) {
|
||
|
StringBuilder sb = new StringBuilder("No route to ");
|
||
|
sb.append(host);
|
||
|
sb.append(':');
|
||
|
sb.append(port);
|
||
|
sb.append("; port is out of range");
|
||
|
throw new SocketException(sb.toString());
|
||
|
}
|
||
|
if (proxy.type() == Proxy.Type.SOCKS) {
|
||
|
arrayList.add(InetSocketAddress.createUnresolved(host, port));
|
||
|
return;
|
||
|
}
|
||
|
if (!Util.canParseAsIpAddress(host)) {
|
||
|
this.eventListener.dnsStart(this.call, host);
|
||
|
lookup = this.address.dns().lookup(host);
|
||
|
if (lookup.isEmpty()) {
|
||
|
StringBuilder sb2 = new StringBuilder();
|
||
|
sb2.append(this.address.dns());
|
||
|
sb2.append(" returned no addresses for ");
|
||
|
sb2.append(host);
|
||
|
throw new UnknownHostException(sb2.toString());
|
||
|
}
|
||
|
this.eventListener.dnsEnd(this.call, host, lookup);
|
||
|
} else {
|
||
|
lookup = Collections.singletonList(InetAddress.getByName(host));
|
||
|
C14957gcv.c(lookup, "");
|
||
|
}
|
||
|
Iterator<InetAddress> it = lookup.iterator();
|
||
|
while (it.hasNext()) {
|
||
|
arrayList.add(new InetSocketAddress(it.next(), port));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes.dex */
|
||
|
public static final class Selection {
|
||
|
private int nextRouteIndex;
|
||
|
private final List<Route> routes;
|
||
|
|
||
|
public Selection(List<Route> list) {
|
||
|
C14957gcv.e(list, "");
|
||
|
this.routes = list;
|
||
|
}
|
||
|
|
||
|
public final boolean hasNext() {
|
||
|
return this.nextRouteIndex < this.routes.size();
|
||
|
}
|
||
|
|
||
|
public final Route next() {
|
||
|
if (!hasNext()) {
|
||
|
throw new NoSuchElementException();
|
||
|
}
|
||
|
List<Route> list = this.routes;
|
||
|
int i = this.nextRouteIndex;
|
||
|
this.nextRouteIndex = i + 1;
|
||
|
return list.get(i);
|
||
|
}
|
||
|
|
||
|
public final List<Route> getRoutes() {
|
||
|
return this.routes;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes.dex */
|
||
|
public static final class Companion {
|
||
|
private Companion() {
|
||
|
}
|
||
|
|
||
|
public final String getSocketHost(InetSocketAddress inetSocketAddress) {
|
||
|
C14957gcv.e(inetSocketAddress, "");
|
||
|
InetAddress address = inetSocketAddress.getAddress();
|
||
|
if (address == null) {
|
||
|
String hostName = inetSocketAddress.getHostName();
|
||
|
C14957gcv.c((Object) hostName, "");
|
||
|
return hostName;
|
||
|
}
|
||
|
String hostAddress = address.getHostAddress();
|
||
|
C14957gcv.c((Object) hostAddress, "");
|
||
|
return hostAddress;
|
||
|
}
|
||
|
|
||
|
public /* synthetic */ Companion(C14953gcr c14953gcr) {
|
||
|
this();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static final List<Proxy> resetNextProxy$selectProxies(Proxy proxy, HttpUrl httpUrl, RouteSelector routeSelector) {
|
||
|
if (proxy == null) {
|
||
|
URI uri = httpUrl.uri();
|
||
|
if (uri.getHost() == null) {
|
||
|
return Util.immutableListOf(Proxy.NO_PROXY);
|
||
|
}
|
||
|
List<Proxy> select = routeSelector.address.proxySelector().select(uri);
|
||
|
List<Proxy> list = select;
|
||
|
if (list == null || list.isEmpty()) {
|
||
|
return Util.immutableListOf(Proxy.NO_PROXY);
|
||
|
}
|
||
|
C14957gcv.c(select, "");
|
||
|
return Util.toImmutableList(select);
|
||
|
}
|
||
|
List<Proxy> singletonList = Collections.singletonList(proxy);
|
||
|
C14957gcv.c(singletonList, "");
|
||
|
return singletonList;
|
||
|
}
|
||
|
}
|