package okhttp3; import java.io.IOException; import java.util.ArrayList; import okhttp3.internal.NamedRunnable; import okhttp3.internal.cache.CacheInterceptor; import okhttp3.internal.connection.ConnectInterceptor; import okhttp3.internal.connection.StreamAllocation; import okhttp3.internal.http.BridgeInterceptor; import okhttp3.internal.http.CallServerInterceptor; import okhttp3.internal.http.RealInterceptorChain; import okhttp3.internal.http.RetryAndFollowUpInterceptor; import okhttp3.internal.platform.Platform; /* loaded from: classes2.dex */ final class RealCall implements Call { final OkHttpClient client; private EventListener eventListener; private boolean executed; final boolean forWebSocket; final Request originalRequest; final RetryAndFollowUpInterceptor retryAndFollowUpInterceptor; final class AsyncCall extends NamedRunnable { private final Callback responseCallback; AsyncCall(Callback callback) { super("OkHttp %s", RealCall.this.redactedUrl()); this.responseCallback = callback; } @Override // okhttp3.internal.NamedRunnable protected void execute() { IOException e; boolean z = true; try { try { Response responseWithInterceptorChain = RealCall.this.getResponseWithInterceptorChain(); try { if (RealCall.this.retryAndFollowUpInterceptor.isCanceled()) { this.responseCallback.onFailure(RealCall.this, new IOException("Canceled")); } else { this.responseCallback.onResponse(RealCall.this, responseWithInterceptorChain); } } catch (IOException e2) { e = e2; if (z) { Platform.get().log(4, "Callback failure for " + RealCall.this.toLoggableString(), e); } else { RealCall.this.eventListener.callFailed(RealCall.this, e); this.responseCallback.onFailure(RealCall.this, e); } } } finally { RealCall.this.client.dispatcher().finished(this); } } catch (IOException e3) { e = e3; z = false; } } RealCall get() { return RealCall.this; } String host() { return RealCall.this.originalRequest.url().host(); } Request request() { return RealCall.this.originalRequest; } } private RealCall(OkHttpClient okHttpClient, Request request, boolean z) { this.client = okHttpClient; this.originalRequest = request; this.forWebSocket = z; this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(okHttpClient, z); } private void captureCallStackTrace() { this.retryAndFollowUpInterceptor.setCallStackTrace(Platform.get().getStackTraceForCloseable("response.body().close()")); } static RealCall newRealCall(OkHttpClient okHttpClient, Request request, boolean z) { RealCall realCall = new RealCall(okHttpClient, request, z); realCall.eventListener = okHttpClient.eventListenerFactory().create(realCall); return realCall; } @Override // okhttp3.Call public void cancel() { this.retryAndFollowUpInterceptor.cancel(); } @Override // okhttp3.Call public void enqueue(Callback callback) { synchronized (this) { if (this.executed) { throw new IllegalStateException("Already Executed"); } this.executed = true; } captureCallStackTrace(); this.eventListener.callStart(this); this.client.dispatcher().enqueue(new AsyncCall(callback)); } @Override // okhttp3.Call public Response execute() throws IOException { synchronized (this) { if (this.executed) { throw new IllegalStateException("Already Executed"); } this.executed = true; } captureCallStackTrace(); this.eventListener.callStart(this); try { try { this.client.dispatcher().executed(this); Response responseWithInterceptorChain = getResponseWithInterceptorChain(); if (responseWithInterceptorChain != null) { return responseWithInterceptorChain; } throw new IOException("Canceled"); } catch (IOException e) { this.eventListener.callFailed(this, e); throw e; } } finally { this.client.dispatcher().finished(this); } } Response getResponseWithInterceptorChain() throws IOException { ArrayList arrayList = new ArrayList(); arrayList.addAll(this.client.interceptors()); arrayList.add(this.retryAndFollowUpInterceptor); arrayList.add(new BridgeInterceptor(this.client.cookieJar())); arrayList.add(new CacheInterceptor(this.client.internalCache())); arrayList.add(new ConnectInterceptor(this.client)); if (!this.forWebSocket) { arrayList.addAll(this.client.networkInterceptors()); } arrayList.add(new CallServerInterceptor(this.forWebSocket)); return new RealInterceptorChain(arrayList, null, null, null, 0, this.originalRequest, this, this.eventListener, this.client.connectTimeoutMillis(), this.client.readTimeoutMillis(), this.client.writeTimeoutMillis()).proceed(this.originalRequest); } @Override // okhttp3.Call public boolean isCanceled() { return this.retryAndFollowUpInterceptor.isCanceled(); } @Override // okhttp3.Call public synchronized boolean isExecuted() { return this.executed; } String redactedUrl() { return this.originalRequest.url().redact(); } @Override // okhttp3.Call public Request request() { return this.originalRequest; } StreamAllocation streamAllocation() { return this.retryAndFollowUpInterceptor.streamAllocation(); } String toLoggableString() { StringBuilder sb = new StringBuilder(); sb.append(isCanceled() ? "canceled " : ""); sb.append(this.forWebSocket ? "web socket" : "call"); sb.append(" to "); sb.append(redactedUrl()); return sb.toString(); } @Override // okhttp3.Call public RealCall clone() { return newRealCall(this.client, this.originalRequest, this.forWebSocket); } }