package okhttp3.internal.connection; import com.facebook.internal.security.CertificateUtil; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.SocketAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; import okhttp3.Address; import okhttp3.Call; import okhttp3.EventListener; import okhttp3.HttpUrl; import okhttp3.Route; import okhttp3.internal.Util; /* loaded from: classes5.dex */ public final class RouteSelector { public final Address address; public final Call call; public final EventListener eventListener; public int nextProxyIndex; public final RouteDatabase routeDatabase; public List proxies = Collections.emptyList(); public List inetSocketAddresses = Collections.emptyList(); public final List postponedRoutes = new ArrayList(); public RouteSelector(Address address, RouteDatabase routeDatabase, Call call, EventListener eventListener) { this.address = address; this.routeDatabase = routeDatabase; this.call = call; this.eventListener = eventListener; resetNextProxy(address.url(), address.proxy()); } public boolean hasNext() { return hasNextProxy() || !this.postponedRoutes.isEmpty(); } public Selection next() { if (!hasNext()) { throw new NoSuchElementException(); } ArrayList arrayList = new ArrayList(); while (hasNextProxy()) { Proxy nextProxy = nextProxy(); int size = this.inetSocketAddresses.size(); for (int i = 0; i < size; i++) { Route route = new Route(this.address, nextProxy, (InetSocketAddress) this.inetSocketAddresses.get(i)); if (this.routeDatabase.shouldPostpone(route)) { this.postponedRoutes.add(route); } else { arrayList.add(route); } } if (!arrayList.isEmpty()) { break; } } if (arrayList.isEmpty()) { arrayList.addAll(this.postponedRoutes); this.postponedRoutes.clear(); } return new Selection(arrayList); } public final void resetNextProxy(HttpUrl httpUrl, Proxy proxy) { if (proxy != null) { this.proxies = Collections.singletonList(proxy); } else { List select = this.address.proxySelector().select(httpUrl.uri()); this.proxies = (select == null || select.isEmpty()) ? Util.immutableList(Proxy.NO_PROXY) : Util.immutableList(select); } this.nextProxyIndex = 0; } public final boolean hasNextProxy() { return this.nextProxyIndex < this.proxies.size(); } public final Proxy nextProxy() { if (!hasNextProxy()) { throw new SocketException("No route to " + this.address.url().host() + "; exhausted proxy configurations: " + this.proxies); } List list = this.proxies; int i = this.nextProxyIndex; this.nextProxyIndex = i + 1; Proxy proxy = (Proxy) list.get(i); resetNextInetSocketAddress(proxy); return proxy; } public final void resetNextInetSocketAddress(Proxy proxy) { String host; int port; this.inetSocketAddresses = new 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("Proxy.address() is not an InetSocketAddress: " + address.getClass()); } InetSocketAddress inetSocketAddress = (InetSocketAddress) address; host = getHostString(inetSocketAddress); port = inetSocketAddress.getPort(); } if (port < 1 || port > 65535) { throw new SocketException("No route to " + host + CertificateUtil.DELIMITER + port + "; port is out of range"); } if (proxy.type() == Proxy.Type.SOCKS) { this.inetSocketAddresses.add(InetSocketAddress.createUnresolved(host, port)); return; } this.eventListener.dnsStart(this.call, host); List lookup = this.address.dns().lookup(host); if (lookup.isEmpty()) { throw new UnknownHostException(this.address.dns() + " returned no addresses for " + host); } this.eventListener.dnsEnd(this.call, host, lookup); int size = lookup.size(); for (int i = 0; i < size; i++) { this.inetSocketAddresses.add(new InetSocketAddress((InetAddress) lookup.get(i), port)); } } public static String getHostString(InetSocketAddress inetSocketAddress) { InetAddress address = inetSocketAddress.getAddress(); if (address == null) { return inetSocketAddress.getHostName(); } return address.getHostAddress(); } public static final class Selection { public int nextRouteIndex = 0; public final List routes; public Selection(List list) { this.routes = list; } public boolean hasNext() { return this.nextRouteIndex < this.routes.size(); } public Route next() { if (!hasNext()) { throw new NoSuchElementException(); } List list = this.routes; int i = this.nextRouteIndex; this.nextRouteIndex = i + 1; return (Route) list.get(i); } public List getAll() { return new ArrayList(this.routes); } } }