package okhttp3.repackaged.internal.http; 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.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; import okhttp3.repackaged.Address; import okhttp3.repackaged.HttpUrl; import okhttp3.repackaged.Route; import okhttp3.repackaged.internal.RouteDatabase; /* loaded from: classes6.dex */ public final class RouteSelector { private final Address address; private final RouteDatabase aho; private int amB; private Proxy amw; private InetSocketAddress amx; private int amz; private List amy = Collections.emptyList(); private List amA = Collections.emptyList(); private final List amC = new ArrayList(); public RouteSelector(Address address, RouteDatabase routeDatabase) { this.address = address; this.aho = routeDatabase; a(address.url(), address.proxy()); } public final boolean hasNext() { return ve() || vc() || vg(); } public final Route next() throws IOException { if (!ve()) { if (!vc()) { if (!vg()) { throw new NoSuchElementException(); } return vh(); } this.amw = vd(); } InetSocketAddress vf = vf(); this.amx = vf; Route route = new Route(this.address, this.amw, vf); if (!this.aho.shouldPostpone(route)) { return route; } this.amC.add(route); return next(); } public final void connectFailed(Route route, IOException iOException) { if (route.proxy().type() != Proxy.Type.DIRECT && this.address.proxySelector() != null) { this.address.proxySelector().connectFailed(this.address.url().uri(), route.proxy().address(), iOException); } this.aho.failed(route); } private void a(HttpUrl httpUrl, Proxy proxy) { if (proxy != null) { this.amy = Collections.singletonList(proxy); } else { this.amy = new ArrayList(); List select = this.address.proxySelector().select(httpUrl.uri()); if (select != null) { this.amy.addAll(select); } this.amy.removeAll(Collections.singleton(Proxy.NO_PROXY)); this.amy.add(Proxy.NO_PROXY); } this.amz = 0; } private boolean vc() { return this.amz < this.amy.size(); } private Proxy vd() throws IOException { if (!vc()) { StringBuilder sb = new StringBuilder("No route to "); sb.append(this.address.url().host()); sb.append("; exhausted proxy configurations: "); sb.append(this.amy); throw new SocketException(sb.toString()); } List list = this.amy; int i = this.amz; this.amz = i + 1; Proxy proxy = list.get(i); a(proxy); return proxy; } private void a(Proxy proxy) throws IOException { String host; int port; this.amA = 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)) { StringBuilder sb = new StringBuilder("Proxy.address() is not an InetSocketAddress: "); sb.append(address.getClass()); throw new IllegalArgumentException(sb.toString()); } InetSocketAddress inetSocketAddress = (InetSocketAddress) address; host = getHostString(inetSocketAddress); port = inetSocketAddress.getPort(); } if (port <= 0 || port > 65535) { StringBuilder sb2 = new StringBuilder("No route to "); sb2.append(host); sb2.append(":"); sb2.append(port); sb2.append("; port is out of range"); throw new SocketException(sb2.toString()); } if (proxy.type() == Proxy.Type.SOCKS) { this.amA.add(InetSocketAddress.createUnresolved(host, port)); } else { List lookup = this.address.dns().lookup(host); int size = lookup.size(); for (int i = 0; i < size; i++) { this.amA.add(new InetSocketAddress(lookup.get(i), port)); } } this.amB = 0; } static String getHostString(InetSocketAddress inetSocketAddress) { InetAddress address = inetSocketAddress.getAddress(); if (address == null) { return inetSocketAddress.getHostName(); } return address.getHostAddress(); } private boolean ve() { return this.amB < this.amA.size(); } private InetSocketAddress vf() throws IOException { if (!ve()) { StringBuilder sb = new StringBuilder("No route to "); sb.append(this.address.url().host()); sb.append("; exhausted inet socket addresses: "); sb.append(this.amA); throw new SocketException(sb.toString()); } List list = this.amA; int i = this.amB; this.amB = i + 1; return list.get(i); } private boolean vg() { return !this.amC.isEmpty(); } private Route vh() { return this.amC.remove(0); } }