package okhttp3.internal.connection; import com.airbnb.deeplinkdispatch.UrlTreeKt; import com.google.common.net.HttpHeaders; import java.io.IOException; import java.lang.ref.Reference; import java.net.ConnectException; import java.net.Proxy; import java.net.Socket; import java.net.SocketException; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.TimeUnit; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import o.C14953gcr; import o.C14957gcv; import o.C15160gio; import o.InterfaceC15148gic; import o.InterfaceC15149gid; import o.gdZ; import okhttp3.Address; import okhttp3.Call; import okhttp3.CertificatePinner; import okhttp3.Connection; import okhttp3.ConnectionSpec; import okhttp3.EventListener; import okhttp3.Handshake; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; import okhttp3.Route; import okhttp3.internal.Util; import okhttp3.internal.concurrent.TaskRunner; import okhttp3.internal.http.ExchangeCodec; import okhttp3.internal.http.RealInterceptorChain; import okhttp3.internal.http1.Http1ExchangeCodec; import okhttp3.internal.http2.ConnectionShutdownException; import okhttp3.internal.http2.ErrorCode; import okhttp3.internal.http2.Http2Connection; import okhttp3.internal.http2.Http2ExchangeCodec; import okhttp3.internal.http2.Http2Stream; import okhttp3.internal.http2.Settings; import okhttp3.internal.http2.StreamResetException; import okhttp3.internal.platform.Platform; import okhttp3.internal.tls.OkHostnameVerifier; import okhttp3.internal.ws.RealWebSocket; /* loaded from: classes.dex */ public final class RealConnection extends Http2Connection.Listener implements Connection { public static final Companion Companion = new Companion(null); public static final long IDLE_CONNECTION_HEALTHY_NS = 10000000000L; private static final int MAX_TUNNEL_ATTEMPTS = 21; private static final String NPE_THROW_WITH_NULL = "throw with null exception"; private int allocationLimit; private final List> calls; private final RealConnectionPool connectionPool; private Handshake handshake; private Http2Connection http2Connection; private long idleAtNs; private boolean noCoalescedConnections; private boolean noNewExchanges; private Protocol protocol; private Socket rawSocket; private int refusedStreamCount; private final Route route; private int routeFailureCount; private InterfaceC15148gic sink; private Socket socket; private InterfaceC15149gid source; private int successCount; public RealConnection(RealConnectionPool realConnectionPool, Route route) { C14957gcv.e(realConnectionPool, ""); C14957gcv.e(route, ""); this.connectionPool = realConnectionPool; this.route = route; this.allocationLimit = 1; this.calls = new ArrayList(); this.idleAtNs = Long.MAX_VALUE; } public final void noNewExchanges$okhttp() { synchronized (this) { this.noNewExchanges = true; } } public final void noCoalescedConnections$okhttp() { synchronized (this) { this.noCoalescedConnections = true; } } public final void incrementSuccessCount$okhttp() { synchronized (this) { this.successCount++; } } /* JADX WARN: Removed duplicated region for block: B:35:0x0103 */ /* JADX WARN: Removed duplicated region for block: B:38:0x010a */ /* JADX WARN: Removed duplicated region for block: B:41:0x0135 */ /* JADX WARN: Removed duplicated region for block: B:43:0x0140 */ /* JADX WARN: Removed duplicated region for block: B:48:0x0148 A[SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:50:0x013b */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public final void connect(int r17, int r18, int r19, int r20, boolean r21, okhttp3.Call r22, okhttp3.EventListener r23) { /* Method dump skipped, instructions count: 356 To view this dump add '--comments-level debug' option */ throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.connection.RealConnection.connect(int, int, int, int, boolean, okhttp3.Call, okhttp3.EventListener):void"); } private final void connectTunnel(int i, int i2, int i3, Call call, EventListener eventListener) throws IOException { Request createTunnelRequest = createTunnelRequest(); HttpUrl url = createTunnelRequest.url(); int i4 = 0; while (i4 < 21) { i4++; connectSocket(i, i2, call, eventListener); createTunnelRequest = createTunnel(i2, i3, createTunnelRequest, url); if (createTunnelRequest == null) { return; } Socket socket = this.rawSocket; if (socket != null) { Util.closeQuietly(socket); } this.rawSocket = null; this.sink = null; this.source = null; eventListener.connectEnd(call, this.route.socketAddress(), this.route.proxy(), null); } } private final void connectSocket(int i, int i2, Call call, EventListener eventListener) throws IOException { Socket createSocket; Proxy proxy = this.route.proxy(); Address address = this.route.address(); Proxy.Type type = proxy.type(); int i3 = type == null ? -1 : WhenMappings.$EnumSwitchMapping$0[type.ordinal()]; if (i3 == 1 || i3 == 2) { createSocket = address.socketFactory().createSocket(); C14957gcv.e(createSocket); } else { createSocket = new Socket(proxy); } this.rawSocket = createSocket; eventListener.connectStart(call, this.route.socketAddress(), proxy); createSocket.setSoTimeout(i2); try { Platform.Companion.get().connectSocket(createSocket, this.route.socketAddress(), i); try { this.source = C15160gio.c(C15160gio.e(createSocket)); this.sink = C15160gio.c(C15160gio.c(createSocket)); } catch (NullPointerException e) { if (C14957gcv.b((Object) e.getMessage(), (Object) NPE_THROW_WITH_NULL)) { throw new IOException(e); } } } catch (ConnectException e2) { ConnectException connectException = new ConnectException(C14957gcv.c("Failed to connect to ", this.route.socketAddress())); connectException.initCause(e2); throw connectException; } } private final void establishProtocol(ConnectionSpecSelector connectionSpecSelector, int i, Call call, EventListener eventListener) throws IOException { if (this.route.address().sslSocketFactory() == null) { if (this.route.address().protocols().contains(Protocol.H2_PRIOR_KNOWLEDGE)) { this.socket = this.rawSocket; this.protocol = Protocol.H2_PRIOR_KNOWLEDGE; startHttp2(i); return; } else { this.socket = this.rawSocket; this.protocol = Protocol.HTTP_1_1; return; } } eventListener.secureConnectStart(call); connectTls(connectionSpecSelector); eventListener.secureConnectEnd(call, this.handshake); if (this.protocol == Protocol.HTTP_2) { startHttp2(i); } } private final void startHttp2(int i) throws IOException { Socket socket = this.socket; C14957gcv.e(socket); InterfaceC15149gid interfaceC15149gid = this.source; C14957gcv.e(interfaceC15149gid); InterfaceC15148gic interfaceC15148gic = this.sink; C14957gcv.e(interfaceC15148gic); socket.setSoTimeout(0); Http2Connection build = new Http2Connection.Builder(true, TaskRunner.INSTANCE).socket(socket, this.route.address().url().host(), interfaceC15149gid, interfaceC15148gic).listener(this).pingIntervalMillis(i).build(); this.http2Connection = build; this.allocationLimit = Http2Connection.Companion.getDEFAULT_SETTINGS().getMaxConcurrentStreams(); Http2Connection.start$default(build, false, null, 3, null); } private final void connectTls(ConnectionSpecSelector connectionSpecSelector) throws IOException { Address address = this.route.address(); SSLSocketFactory sslSocketFactory = address.sslSocketFactory(); SSLSocket sSLSocket = null; try { C14957gcv.e(sslSocketFactory); Socket createSocket = sslSocketFactory.createSocket(this.rawSocket, address.url().host(), address.url().port(), true); if (createSocket == null) { throw new NullPointerException("null cannot be cast to non-null type javax.net.ssl.SSLSocket"); } SSLSocket sSLSocket2 = (SSLSocket) createSocket; try { ConnectionSpec configureSecureSocket = connectionSpecSelector.configureSecureSocket(sSLSocket2); if (configureSecureSocket.supportsTlsExtensions()) { Platform.Companion.get().configureTlsExtensions(sSLSocket2, address.url().host(), address.protocols()); } sSLSocket2.startHandshake(); SSLSession session = sSLSocket2.getSession(); Handshake.Companion companion = Handshake.Companion; C14957gcv.c(session, ""); Handshake handshake = companion.get(session); HostnameVerifier hostnameVerifier = address.hostnameVerifier(); C14957gcv.e(hostnameVerifier); if (hostnameVerifier.verify(address.url().host(), session)) { CertificatePinner certificatePinner = address.certificatePinner(); C14957gcv.e(certificatePinner); this.handshake = new Handshake(handshake.tlsVersion(), handshake.cipherSuite(), handshake.localCertificates(), new RealConnection$connectTls$1(certificatePinner, handshake, address)); certificatePinner.check$okhttp(address.url().host(), new RealConnection$connectTls$2(this)); String selectedProtocol = configureSecureSocket.supportsTlsExtensions() ? Platform.Companion.get().getSelectedProtocol(sSLSocket2) : null; this.socket = sSLSocket2; this.source = C15160gio.c(C15160gio.e(sSLSocket2)); this.sink = C15160gio.c(C15160gio.c(sSLSocket2)); this.protocol = selectedProtocol != null ? Protocol.Companion.get(selectedProtocol) : Protocol.HTTP_1_1; Platform.Companion.get().afterHandshake(sSLSocket2); return; } List peerCertificates = handshake.peerCertificates(); if (!(!peerCertificates.isEmpty())) { StringBuilder sb = new StringBuilder("Hostname "); sb.append(address.url().host()); sb.append(" not verified (no certificates)"); throw new SSLPeerUnverifiedException(sb.toString()); } X509Certificate x509Certificate = (X509Certificate) peerCertificates.get(0); StringBuilder sb2 = new StringBuilder("\n |Hostname "); sb2.append(address.url().host()); sb2.append(" not verified:\n | certificate: "); sb2.append(CertificatePinner.Companion.pin(x509Certificate)); sb2.append("\n | DN: "); sb2.append((Object) x509Certificate.getSubjectDN().getName()); sb2.append("\n | subjectAltNames: "); sb2.append(OkHostnameVerifier.INSTANCE.allSubjectAltNames(x509Certificate)); sb2.append("\n "); throw new SSLPeerUnverifiedException(gdZ.e(sb2.toString())); } catch (Throwable th) { th = th; sSLSocket = sSLSocket2; if (sSLSocket != null) { Platform.Companion.get().afterHandshake(sSLSocket); } if (sSLSocket != null) { Util.closeQuietly((Socket) sSLSocket); } throw th; } } catch (Throwable th2) { th = th2; } } private final Request createTunnel(int i, int i2, Request request, HttpUrl httpUrl) throws IOException { StringBuilder sb = new StringBuilder("CONNECT "); sb.append(Util.toHostHeader(httpUrl, true)); sb.append(" HTTP/1.1"); String obj = sb.toString(); while (true) { InterfaceC15149gid interfaceC15149gid = this.source; C14957gcv.e(interfaceC15149gid); InterfaceC15148gic interfaceC15148gic = this.sink; C14957gcv.e(interfaceC15148gic); Http1ExchangeCodec http1ExchangeCodec = new Http1ExchangeCodec(null, this, interfaceC15149gid, interfaceC15148gic); interfaceC15149gid.timeout().timeout(i, TimeUnit.MILLISECONDS); interfaceC15148gic.timeout().timeout(i2, TimeUnit.MILLISECONDS); http1ExchangeCodec.writeRequest(request.headers(), obj); http1ExchangeCodec.finishRequest(); Response.Builder readResponseHeaders = http1ExchangeCodec.readResponseHeaders(false); C14957gcv.e(readResponseHeaders); Response build = readResponseHeaders.request(request).build(); http1ExchangeCodec.skipConnectBody(build); int code = build.code(); if (code == 200) { if (interfaceC15149gid.i().c == 0 && interfaceC15148gic.i().c == 0) { return null; } throw new IOException("TLS tunnel buffered too many bytes!"); } if (code == 407) { Request authenticate = this.route.address().proxyAuthenticator().authenticate(this.route, build); if (authenticate == null) { throw new IOException("Failed to authenticate with proxy"); } if (gdZ.a("close", Response.header$default(build, HttpHeaders.CONNECTION, null, 2, null), true)) { return authenticate; } request = authenticate; } else { throw new IOException(C14957gcv.c("Unexpected response code for CONNECT: ", Integer.valueOf(build.code()))); } } } private final Request createTunnelRequest() throws IOException { Request build = new Request.Builder().url(this.route.address().url()).method("CONNECT", null).header(HttpHeaders.HOST, Util.toHostHeader(this.route.address().url(), true)).header("Proxy-Connection", "Keep-Alive").header("User-Agent", Util.userAgent).build(); Request authenticate = this.route.address().proxyAuthenticator().authenticate(this.route, new Response.Builder().request(build).protocol(Protocol.HTTP_1_1).code(407).message("Preemptive Authenticate").body(Util.EMPTY_RESPONSE).sentRequestAtMillis(-1L).receivedResponseAtMillis(-1L).header(HttpHeaders.PROXY_AUTHENTICATE, "OkHttp-Preemptive").build()); return authenticate != null ? authenticate : build; } private final boolean routeMatchesAny(List list) { List list2 = list; if ((list2 instanceof Collection) && list2.isEmpty()) { return false; } for (Route route : list2) { if (route.proxy().type() == Proxy.Type.DIRECT && this.route.proxy().type() == Proxy.Type.DIRECT && C14957gcv.b(this.route.socketAddress(), route.socketAddress())) { return true; } } return false; } private final boolean certificateSupportHost(HttpUrl httpUrl, Handshake handshake) { List peerCertificates = handshake.peerCertificates(); return (peerCertificates.isEmpty() ^ true) && OkHostnameVerifier.INSTANCE.verify(httpUrl.host(), (X509Certificate) peerCertificates.get(0)); } public final ExchangeCodec newCodec$okhttp(OkHttpClient okHttpClient, RealInterceptorChain realInterceptorChain) throws SocketException { C14957gcv.e(okHttpClient, ""); C14957gcv.e(realInterceptorChain, ""); Socket socket = this.socket; C14957gcv.e(socket); InterfaceC15149gid interfaceC15149gid = this.source; C14957gcv.e(interfaceC15149gid); InterfaceC15148gic interfaceC15148gic = this.sink; C14957gcv.e(interfaceC15148gic); Http2Connection http2Connection = this.http2Connection; if (http2Connection != null) { return new Http2ExchangeCodec(okHttpClient, this, realInterceptorChain, http2Connection); } socket.setSoTimeout(realInterceptorChain.readTimeoutMillis()); interfaceC15149gid.timeout().timeout(realInterceptorChain.getReadTimeoutMillis$okhttp(), TimeUnit.MILLISECONDS); interfaceC15148gic.timeout().timeout(realInterceptorChain.getWriteTimeoutMillis$okhttp(), TimeUnit.MILLISECONDS); return new Http1ExchangeCodec(okHttpClient, this, interfaceC15149gid, interfaceC15148gic); } public final RealWebSocket.Streams newWebSocketStreams$okhttp(final Exchange exchange) throws SocketException { C14957gcv.e(exchange, ""); Socket socket = this.socket; C14957gcv.e(socket); final InterfaceC15149gid interfaceC15149gid = this.source; C14957gcv.e(interfaceC15149gid); final InterfaceC15148gic interfaceC15148gic = this.sink; C14957gcv.e(interfaceC15148gic); socket.setSoTimeout(0); noNewExchanges$okhttp(); return new RealWebSocket.Streams(interfaceC15149gid, interfaceC15148gic, exchange) { // from class: okhttp3.internal.connection.RealConnection$newWebSocketStreams$1 final Exchange $exchange; final InterfaceC15148gic $sink; final InterfaceC15149gid $source; /* JADX INFO: Access modifiers changed from: package-private */ /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(true, interfaceC15149gid, interfaceC15148gic); this.$source = interfaceC15149gid; this.$sink = interfaceC15148gic; this.$exchange = exchange; } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() { this.$exchange.bodyComplete(-1L, true, true, null); } }; } public final void cancel() { Socket socket = this.rawSocket; if (socket != null) { Util.closeQuietly(socket); } } @Override // okhttp3.Connection public final Socket socket() { Socket socket = this.socket; C14957gcv.e(socket); return socket; } @Override // okhttp3.internal.http2.Http2Connection.Listener public final void onStream(Http2Stream http2Stream) throws IOException { C14957gcv.e(http2Stream, ""); http2Stream.close(ErrorCode.REFUSED_STREAM, null); } @Override // okhttp3.internal.http2.Http2Connection.Listener public final void onSettings(Http2Connection http2Connection, Settings settings) { synchronized (this) { C14957gcv.e(http2Connection, ""); C14957gcv.e(settings, ""); this.allocationLimit = settings.getMaxConcurrentStreams(); } } public final void connectFailed$okhttp(OkHttpClient okHttpClient, Route route, IOException iOException) { C14957gcv.e(okHttpClient, ""); C14957gcv.e(route, ""); C14957gcv.e(iOException, ""); if (route.proxy().type() != Proxy.Type.DIRECT) { Address address = route.address(); address.proxySelector().connectFailed(address.url().uri(), route.proxy().address(), iOException); } okHttpClient.getRouteDatabase().failed(route); } public final void trackFailure$okhttp(RealCall realCall, IOException iOException) { synchronized (this) { C14957gcv.e(realCall, ""); if (iOException instanceof StreamResetException) { if (((StreamResetException) iOException).errorCode == ErrorCode.REFUSED_STREAM) { int i = this.refusedStreamCount + 1; this.refusedStreamCount = i; if (i > 1) { this.noNewExchanges = true; this.routeFailureCount++; } } else if (((StreamResetException) iOException).errorCode != ErrorCode.CANCEL || !realCall.isCanceled()) { this.noNewExchanges = true; this.routeFailureCount++; } } else if (!isMultiplexed$okhttp() || (iOException instanceof ConnectionShutdownException)) { this.noNewExchanges = true; if (this.successCount == 0) { if (iOException != null) { connectFailed$okhttp(realCall.getClient(), this.route, iOException); } this.routeFailureCount++; } } } } @Override // okhttp3.Connection public final Protocol protocol() { Protocol protocol = this.protocol; C14957gcv.e(protocol); return protocol; } public final String toString() { Object obj; StringBuilder sb = new StringBuilder("Connection{"); sb.append(this.route.address().url().host()); sb.append(':'); sb.append(this.route.address().url().port()); sb.append(", proxy="); sb.append(this.route.proxy()); sb.append(" hostAddress="); sb.append(this.route.socketAddress()); sb.append(" cipherSuite="); Handshake handshake = this.handshake; if (handshake == null || (obj = handshake.cipherSuite()) == null) { obj = "none"; } sb.append(obj); sb.append(" protocol="); sb.append(this.protocol); sb.append(UrlTreeKt.componentParamSuffixChar); return sb.toString(); } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final RealConnection newTestConnection(RealConnectionPool realConnectionPool, Route route, Socket socket, long j) { C14957gcv.e(realConnectionPool, ""); C14957gcv.e(route, ""); C14957gcv.e(socket, ""); RealConnection realConnection = new RealConnection(realConnectionPool, route); realConnection.socket = socket; realConnection.setIdleAtNs$okhttp(j); return realConnection; } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final boolean isEligible$okhttp(Address address, List list) { C14957gcv.e(address, ""); if (!Util.assertionsEnabled || Thread.holdsLock(this)) { if (this.calls.size() >= this.allocationLimit || this.noNewExchanges || !this.route.address().equalsNonHost$okhttp(address)) { return false; } if (C14957gcv.b((Object) address.url().host(), (Object) route().address().url().host())) { return true; } if (this.http2Connection == null || list == null || !routeMatchesAny(list) || address.hostnameVerifier() != OkHostnameVerifier.INSTANCE || !supportsUrl(address.url())) { return false; } try { CertificatePinner certificatePinner = address.certificatePinner(); C14957gcv.e(certificatePinner); String host = address.url().host(); Handshake handshake = handshake(); C14957gcv.e(handshake); certificatePinner.check(host, handshake.peerCertificates()); return true; } catch (SSLPeerUnverifiedException unused) { return false; } } StringBuilder sb = new StringBuilder("Thread "); sb.append((Object) Thread.currentThread().getName()); sb.append(" MUST hold lock on "); sb.append(this); throw new AssertionError(sb.toString()); } private final boolean supportsUrl(HttpUrl httpUrl) { Handshake handshake; if (!Util.assertionsEnabled || Thread.holdsLock(this)) { HttpUrl url = this.route.address().url(); if (httpUrl.port() != url.port()) { return false; } if (C14957gcv.b((Object) httpUrl.host(), (Object) url.host())) { return true; } if (this.noCoalescedConnections || (handshake = this.handshake) == null) { return false; } C14957gcv.e(handshake); return certificateSupportHost(httpUrl, handshake); } StringBuilder sb = new StringBuilder("Thread "); sb.append((Object) Thread.currentThread().getName()); sb.append(" MUST hold lock on "); sb.append(this); throw new AssertionError(sb.toString()); } public final boolean isHealthy(boolean z) { long idleAtNs$okhttp; if (!Util.assertionsEnabled || !Thread.holdsLock(this)) { long nanoTime = System.nanoTime(); Socket socket = this.rawSocket; C14957gcv.e(socket); Socket socket2 = this.socket; C14957gcv.e(socket2); InterfaceC15149gid interfaceC15149gid = this.source; C14957gcv.e(interfaceC15149gid); if (socket.isClosed() || socket2.isClosed() || socket2.isInputShutdown() || socket2.isOutputShutdown()) { return false; } Http2Connection http2Connection = this.http2Connection; if (http2Connection != null) { return http2Connection.isHealthy(nanoTime); } synchronized (this) { idleAtNs$okhttp = getIdleAtNs$okhttp(); } if (nanoTime - idleAtNs$okhttp < IDLE_CONNECTION_HEALTHY_NS || !z) { return true; } return Util.isHealthy(socket2, interfaceC15149gid); } StringBuilder sb = new StringBuilder("Thread "); sb.append((Object) Thread.currentThread().getName()); sb.append(" MUST NOT hold lock on "); sb.append(this); throw new AssertionError(sb.toString()); } public final void setRouteFailureCount$okhttp(int i) { this.routeFailureCount = i; } public final void setNoNewExchanges(boolean z) { this.noNewExchanges = z; } public final void setIdleAtNs$okhttp(long j) { this.idleAtNs = j; } @Override // okhttp3.Connection public final Route route() { return this.route; } public final boolean isMultiplexed$okhttp() { return this.http2Connection != null; } @Override // okhttp3.Connection public final Handshake handshake() { return this.handshake; } public final int getRouteFailureCount$okhttp() { return this.routeFailureCount; } public final boolean getNoNewExchanges() { return this.noNewExchanges; } public final long getIdleAtNs$okhttp() { return this.idleAtNs; } public final RealConnectionPool getConnectionPool() { return this.connectionPool; } /* loaded from: classes.dex */ public final /* synthetic */ class WhenMappings { public static final int[] $EnumSwitchMapping$0; static { int[] iArr = new int[Proxy.Type.values().length]; iArr[Proxy.Type.DIRECT.ordinal()] = 1; iArr[Proxy.Type.HTTP.ordinal()] = 2; $EnumSwitchMapping$0 = iArr; } } public final List> getCalls() { return this.calls; } }