package okhttp3.internal.connection; import com.google.common.net.HttpHeaders; import java.io.IOException; import java.net.ProtocolException; import java.net.SocketException; import o.AbstractC15151gif; import o.AbstractC15154gii; import o.C14957gcv; import o.C15111ghZ; import o.C15160gio; import o.InterfaceC15168giw; import o.giD; import okhttp3.EventListener; import okhttp3.Headers; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okhttp3.internal.http.ExchangeCodec; import okhttp3.internal.http.RealResponseBody; import okhttp3.internal.ws.RealWebSocket; /* loaded from: classes.dex */ public final class Exchange { private final RealCall call; private final ExchangeCodec codec; private final RealConnection connection; private final EventListener eventListener; private final ExchangeFinder finder; private boolean hasFailure; private boolean isDuplex; public Exchange(RealCall realCall, EventListener eventListener, ExchangeFinder exchangeFinder, ExchangeCodec exchangeCodec) { C14957gcv.e(realCall, ""); C14957gcv.e(eventListener, ""); C14957gcv.e(exchangeFinder, ""); C14957gcv.e(exchangeCodec, ""); this.call = realCall; this.eventListener = eventListener; this.finder = exchangeFinder; this.codec = exchangeCodec; this.connection = exchangeCodec.getConnection(); } public final boolean isCoalescedConnection$okhttp() { return !C14957gcv.b((Object) this.finder.getAddress$okhttp().url().host(), (Object) this.connection.route().address().url().host()); } public final void writeRequestHeaders(Request request) throws IOException { C14957gcv.e(request, ""); try { this.eventListener.requestHeadersStart(this.call); this.codec.writeRequestHeaders(request); this.eventListener.requestHeadersEnd(this.call, request); } catch (IOException e) { this.eventListener.requestFailed(this.call, e); trackFailure(e); throw e; } } public final InterfaceC15168giw createRequestBody(Request request, boolean z) throws IOException { C14957gcv.e(request, ""); this.isDuplex = z; RequestBody body = request.body(); C14957gcv.e(body); long contentLength = body.contentLength(); this.eventListener.requestBodyStart(this.call); return new RequestBodySink(this, this.codec.createRequestBody(request, contentLength), contentLength); } public final void flushRequest() throws IOException { try { this.codec.flushRequest(); } catch (IOException e) { this.eventListener.requestFailed(this.call, e); trackFailure(e); throw e; } } public final void finishRequest() throws IOException { try { this.codec.finishRequest(); } catch (IOException e) { this.eventListener.requestFailed(this.call, e); trackFailure(e); throw e; } } public final void responseHeadersStart() { this.eventListener.responseHeadersStart(this.call); } public final Response.Builder readResponseHeaders(boolean z) throws IOException { try { Response.Builder readResponseHeaders = this.codec.readResponseHeaders(z); if (readResponseHeaders != null) { readResponseHeaders.initExchange$okhttp(this); } return readResponseHeaders; } catch (IOException e) { this.eventListener.responseFailed(this.call, e); trackFailure(e); throw e; } } public final void responseHeadersEnd(Response response) { C14957gcv.e(response, ""); this.eventListener.responseHeadersEnd(this.call, response); } public final ResponseBody openResponseBody(Response response) throws IOException { C14957gcv.e(response, ""); try { String header$default = Response.header$default(response, HttpHeaders.CONTENT_TYPE, null, 2, null); long reportedContentLength = this.codec.reportedContentLength(response); return new RealResponseBody(header$default, reportedContentLength, C15160gio.c(new ResponseBodySource(this, this.codec.openResponseBodySource(response), reportedContentLength))); } catch (IOException e) { this.eventListener.responseFailed(this.call, e); trackFailure(e); throw e; } } public final Headers trailers() throws IOException { return this.codec.trailers(); } public final RealWebSocket.Streams newWebSocketStreams() throws SocketException { this.call.timeoutEarlyExit(); return this.codec.getConnection().newWebSocketStreams$okhttp(this); } public final void webSocketUpgradeFailed() { bodyComplete(-1L, true, true, null); } public final void noNewExchangesOnConnection() { this.codec.getConnection().noNewExchanges$okhttp(); } public final void cancel() { this.codec.cancel(); } public final void detachWithViolence() { this.codec.cancel(); this.call.messageDone$okhttp(this, true, true, null); } private final void trackFailure(IOException iOException) { this.hasFailure = true; this.finder.trackFailure(iOException); this.codec.getConnection().trackFailure$okhttp(this.call, iOException); } public final E bodyComplete(long j, boolean z, boolean z2, E e) { if (e != null) { trackFailure(e); } if (z2) { if (e != null) { this.eventListener.requestFailed(this.call, e); } else { this.eventListener.requestBodyEnd(this.call, j); } } if (z) { if (e != null) { this.eventListener.responseFailed(this.call, e); } else { this.eventListener.responseBodyEnd(this.call, j); } } return (E) this.call.messageDone$okhttp(this, z2, z, e); } public final void noRequestBody() { this.call.messageDone$okhttp(this, true, false, null); } /* loaded from: classes.dex */ final class RequestBodySink extends AbstractC15151gif { private long bytesReceived; private boolean closed; private boolean completed; private final long contentLength; final Exchange this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public RequestBodySink(Exchange exchange, InterfaceC15168giw interfaceC15168giw, long j) { super(interfaceC15168giw); C14957gcv.e(exchange, ""); C14957gcv.e(interfaceC15168giw, ""); this.this$0 = exchange; this.contentLength = j; } @Override // o.AbstractC15151gif, o.InterfaceC15168giw public final void write(C15111ghZ c15111ghZ, long j) throws IOException { C14957gcv.e(c15111ghZ, ""); if (!(!this.closed)) { throw new IllegalStateException("closed".toString()); } long j2 = this.contentLength; if (j2 != -1 && this.bytesReceived + j > j2) { StringBuilder sb = new StringBuilder("expected "); sb.append(this.contentLength); sb.append(" bytes but received "); sb.append(this.bytesReceived + j); throw new ProtocolException(sb.toString()); } try { super.write(c15111ghZ, j); this.bytesReceived += j; } catch (IOException e) { throw complete(e); } } @Override // o.AbstractC15151gif, o.InterfaceC15168giw, java.io.Flushable public final void flush() throws IOException { try { super.flush(); } catch (IOException e) { throw complete(e); } } @Override // o.AbstractC15151gif, o.InterfaceC15168giw, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { if (this.closed) { return; } this.closed = true; long j = this.contentLength; if (j != -1 && this.bytesReceived != j) { throw new ProtocolException("unexpected end of stream"); } try { super.close(); complete(null); } catch (IOException e) { throw complete(e); } } private final E complete(E e) { if (this.completed) { return e; } this.completed = true; return (E) this.this$0.bodyComplete(this.bytesReceived, false, true, e); } } /* loaded from: classes.dex */ public final class ResponseBodySource extends AbstractC15154gii { private long bytesReceived; private boolean closed; private boolean completed; private final long contentLength; private boolean invokeStartEvent; final Exchange this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public ResponseBodySource(Exchange exchange, giD gid, long j) { super(gid); C14957gcv.e(exchange, ""); C14957gcv.e(gid, ""); this.this$0 = exchange; this.contentLength = j; this.invokeStartEvent = true; if (j == 0) { complete(null); } } @Override // o.AbstractC15154gii, o.giD public final long read(C15111ghZ c15111ghZ, long j) throws IOException { C14957gcv.e(c15111ghZ, ""); if (!(!this.closed)) { throw new IllegalStateException("closed".toString()); } try { long read = delegate().read(c15111ghZ, j); if (this.invokeStartEvent) { this.invokeStartEvent = false; this.this$0.getEventListener$okhttp().responseBodyStart(this.this$0.getCall$okhttp()); } if (read == -1) { complete(null); return -1L; } long j2 = this.bytesReceived + read; long j3 = this.contentLength; if (j3 != -1 && j2 > j3) { StringBuilder sb = new StringBuilder("expected "); sb.append(this.contentLength); sb.append(" bytes but received "); sb.append(j2); throw new ProtocolException(sb.toString()); } this.bytesReceived = j2; if (j2 == j3) { complete(null); } return read; } catch (IOException e) { throw complete(e); } } @Override // o.AbstractC15154gii, o.giD, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { if (this.closed) { return; } this.closed = true; try { super.close(); complete(null); } catch (IOException e) { throw complete(e); } } public final E complete(E e) { if (this.completed) { return e; } this.completed = true; if (e == null && this.invokeStartEvent) { this.invokeStartEvent = false; this.this$0.getEventListener$okhttp().responseBodyStart(this.this$0.getCall$okhttp()); } return (E) this.this$0.bodyComplete(this.bytesReceived, true, false, e); } } public final boolean isDuplex$okhttp() { return this.isDuplex; } public final boolean getHasFailure$okhttp() { return this.hasFailure; } public final ExchangeFinder getFinder$okhttp() { return this.finder; } public final EventListener getEventListener$okhttp() { return this.eventListener; } public final RealConnection getConnection$okhttp() { return this.connection; } public final RealCall getCall$okhttp() { return this.call; } }