package okhttp3.repackaged.internal.http; import com.google.common.net.HttpHeaders; import java.io.EOFException; import java.io.IOException; import java.net.ProtocolException; import java.util.concurrent.TimeUnit; import o.C15145giV; import o.InterfaceC15142giR; import o.giM; import o.giO; import o.giW; import o.giX; import o.giY; import o.giZ; import okhttp3.repackaged.Headers; import okhttp3.repackaged.Request; import okhttp3.repackaged.Response; import okhttp3.repackaged.ResponseBody; import okhttp3.repackaged.internal.Internal; import okhttp3.repackaged.internal.Util; import okhttp3.repackaged.internal.io.RealConnection; /* loaded from: classes6.dex */ public final class Http1xStream implements HttpStream { private static final int STATE_IDLE = 0; private static final int alA = 1; private static final int alB = 2; private static final int alC = 3; private static final int alD = 4; private static final int alE = 5; private static final int alF = 6; private HttpEngine httpEngine; private final giO sink; private final InterfaceC15142giR source; private int state = 0; private final StreamAllocation streamAllocation; public Http1xStream(StreamAllocation streamAllocation, InterfaceC15142giR interfaceC15142giR, giO gio) { this.streamAllocation = streamAllocation; this.source = interfaceC15142giR; this.sink = gio; } @Override // okhttp3.repackaged.internal.http.HttpStream public final giX createRequestBody(Request request, long j) throws IOException { if ("chunked".equalsIgnoreCase(request.header(HttpHeaders.TRANSFER_ENCODING))) { return newChunkedSink(); } if (j != -1) { return newFixedLengthSink(j); } throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!"); } @Override // okhttp3.repackaged.internal.http.HttpStream public final void cancel() { RealConnection connection = this.streamAllocation.connection(); if (connection != null) { connection.cancel(); } } @Override // okhttp3.repackaged.internal.http.HttpStream public final void writeRequestHeaders(Request request) throws IOException { this.httpEngine.writingRequestHeaders(); writeRequest(request.headers(), RequestLine.a(request, this.httpEngine.getConnection().route().proxy().type())); } @Override // okhttp3.repackaged.internal.http.HttpStream public final Response.Builder readResponseHeaders() throws IOException { return readResponse(); } @Override // okhttp3.repackaged.internal.http.HttpStream public final ResponseBody openResponseBody(Response response) throws IOException { return new RealResponseBody(response.headers(), C15145giV.a(l(response))); } private giZ l(Response response) throws IOException { if (!HttpEngine.hasBody(response)) { return newFixedLengthSource(0L); } if ("chunked".equalsIgnoreCase(response.header(HttpHeaders.TRANSFER_ENCODING))) { return newChunkedSource(this.httpEngine); } long contentLength = OkHeaders.contentLength(response); if (contentLength != -1) { return newFixedLengthSource(contentLength); } return newUnknownLengthSource(); } @Override // okhttp3.repackaged.internal.http.HttpStream public final void finishRequest() throws IOException { this.sink.flush(); } public final void writeRequest(Headers headers, String str) throws IOException { if (this.state != 0) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } this.sink.c(str).c("\r\n"); int size = headers.size(); for (int i = 0; i < size; i++) { this.sink.c(headers.name(i)).c(": ").c(headers.value(i)).c("\r\n"); } this.sink.c("\r\n"); this.state = 1; } public final Response.Builder readResponse() throws IOException { StatusLine parse; Response.Builder headers; int i = this.state; if (i != 1 && i != 3) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } do { try { parse = StatusLine.parse(this.source.p()); headers = new Response.Builder().protocol(parse.protocol).code(parse.code).message(parse.message).headers(readHeaders()); } catch (EOFException e2) { StringBuilder sb2 = new StringBuilder("unexpected end of stream on "); sb2.append(this.streamAllocation); IOException iOException = new IOException(sb2.toString()); iOException.initCause(e2); throw iOException; } } while (parse.code == 100); this.state = 4; return headers; } public final Headers readHeaders() throws IOException { Headers.Builder builder = new Headers.Builder(); while (true) { String p = this.source.p(); if (p.length() != 0) { Internal.instance.addLenient(builder, p); } else { return builder.build(); } } } public final giX newChunkedSink() { if (this.state != 1) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } this.state = 2; return new b(); } public final giX newFixedLengthSink(long j) { if (this.state != 1) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } this.state = 2; return new d(j); } @Override // okhttp3.repackaged.internal.http.HttpStream public final void writeRequestBody(RetryableSink retryableSink) throws IOException { if (this.state != 1) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } this.state = 3; retryableSink.writeToSocket(this.sink); } public final giZ newFixedLengthSource(long j) throws IOException { if (this.state != 4) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } this.state = 5; return new e(this, j); } public final giZ newChunkedSource(HttpEngine httpEngine) throws IOException { if (this.state != 4) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } this.state = 5; return new c(this, httpEngine); } public final giZ newUnknownLengthSource() throws IOException { if (this.state != 4) { StringBuilder sb = new StringBuilder("state: "); sb.append(this.state); throw new IllegalStateException(sb.toString()); } StreamAllocation streamAllocation = this.streamAllocation; if (streamAllocation == null) { throw new IllegalStateException("streamAllocation == null"); } this.state = 5; streamAllocation.noNewStreams(); return new f(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class d implements giX { private final giW alG; final Http1xStream alH; private long alL; private boolean closed; private d(Http1xStream http1xStream, long j) { this.alH = http1xStream; this.alG = new giW(http1xStream.sink.timeout()); this.alL = j; } @Override // o.giX, java.io.Flushable public final void flush() throws IOException { if (this.closed) { return; } this.alH.sink.flush(); } @Override // o.giX, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { if (this.closed) { return; } this.closed = true; if (this.alL <= 0) { this.alH.a(this.alG); this.alH.state = 3; return; } throw new ProtocolException("unexpected end of stream"); } @Override // o.giX public final void write(giM gim, long j) throws IOException { if (this.closed) { throw new IllegalStateException("closed"); } Util.checkOffsetAndCount(gim.a, 0L, j); if (j <= this.alL) { this.alH.sink.write(gim, j); this.alL -= j; } else { StringBuilder sb = new StringBuilder("expected "); sb.append(this.alL); sb.append(" bytes but received "); sb.append(j); throw new ProtocolException(sb.toString()); } } @Override // o.giX public final giY timeout() { return this.alG; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class b implements giX { private final giW alG; final Http1xStream alH; private boolean closed; private b(Http1xStream http1xStream) { this.alH = http1xStream; this.alG = new giW(http1xStream.sink.timeout()); } @Override // o.giX public final void write(giM gim, long j) throws IOException { if (this.closed) { throw new IllegalStateException("closed"); } if (j == 0) { return; } this.alH.sink.k(j); this.alH.sink.c("\r\n"); this.alH.sink.write(gim, j); this.alH.sink.c("\r\n"); } @Override // o.giX, java.io.Flushable public final void flush() throws IOException { synchronized (this) { if (this.closed) { return; } this.alH.sink.flush(); } } @Override // o.giX, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { synchronized (this) { if (this.closed) { return; } this.closed = true; this.alH.sink.c("0\r\n\r\n"); this.alH.a(this.alG); this.alH.state = 3; } } @Override // o.giX public final giY timeout() { return this.alG; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public abstract class a implements giZ { protected final giW alG; final Http1xStream alH; protected boolean closed; private a(Http1xStream http1xStream) { this.alH = http1xStream; this.alG = new giW(http1xStream.source.timeout()); } protected final void B(boolean z) throws IOException { if (this.alH.state == 6) { return; } if (this.alH.state == 5) { this.alH.a(this.alG); this.alH.state = 6; if (this.alH.streamAllocation != null) { this.alH.streamAllocation.streamFinished(!z, this.alH); return; } return; } StringBuilder sb = new StringBuilder("state: "); sb.append(this.alH.state); throw new IllegalStateException(sb.toString()); } @Override // o.giZ public giY timeout() { return this.alG; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public class e extends a { final Http1xStream alH; private long alL; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public e(Http1xStream http1xStream, long j) throws IOException { super(); this.alH = http1xStream; this.alL = j; if (j == 0) { B(true); } } @Override // o.giZ public long read(giM gim, long j) throws IOException { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: ".concat(String.valueOf(j))); } if (this.closed) { throw new IllegalStateException("closed"); } if (this.alL == 0) { return -1L; } long read = this.alH.source.read(gim, Math.min(this.alL, j)); if (read == -1) { B(false); throw new ProtocolException("unexpected end of stream"); } long j2 = this.alL - read; this.alL = j2; if (j2 == 0) { B(true); } return read; } @Override // o.giZ, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { if (this.closed) { return; } if (this.alL != 0 && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) { B(false); } this.closed = true; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public class c extends a { private static final long alI = -1; final Http1xStream alH; private long alJ; private boolean alK; private final HttpEngine httpEngine; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ c(Http1xStream http1xStream, HttpEngine httpEngine) throws IOException { super(); this.alH = http1xStream; this.alJ = -1L; this.alK = true; this.httpEngine = httpEngine; } @Override // o.giZ public long read(giM gim, long j) throws IOException { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: ".concat(String.valueOf(j))); } if (this.closed) { throw new IllegalStateException("closed"); } if (!this.alK) { return -1L; } long j2 = this.alJ; if (j2 == 0 || j2 == -1) { uX(); if (!this.alK) { return -1L; } } long read = this.alH.source.read(gim, Math.min(j, this.alJ)); if (read != -1) { this.alJ -= read; return read; } B(false); throw new ProtocolException("unexpected end of stream"); } private void uX() throws IOException { if (this.alJ != -1) { this.alH.source.p(); } try { this.alJ = this.alH.source.l(); String trim = this.alH.source.p().trim(); if (this.alJ < 0 || !(trim.isEmpty() || trim.startsWith(";"))) { StringBuilder sb = new StringBuilder("expected chunk size and optional extensions but was \""); sb.append(this.alJ); sb.append(trim); sb.append("\""); throw new ProtocolException(sb.toString()); } if (this.alJ == 0) { this.alK = false; this.httpEngine.receiveHeaders(this.alH.readHeaders()); B(true); } } catch (NumberFormatException e) { throw new ProtocolException(e.getMessage()); } } @Override // o.giZ, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { if (this.closed) { return; } if (this.alK && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) { B(false); } this.closed = true; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public class f extends a { final Http1xStream alH; private boolean alM; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ private f(Http1xStream http1xStream) { super(); this.alH = http1xStream; } @Override // o.giZ public long read(giM gim, long j) throws IOException { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: ".concat(String.valueOf(j))); } if (this.closed) { throw new IllegalStateException("closed"); } if (this.alM) { return -1L; } long read = this.alH.source.read(gim, j); if (read != -1) { return read; } this.alM = true; B(true); return -1L; } @Override // o.giZ, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { if (this.closed) { return; } if (!this.alM) { B(false); } this.closed = true; } } /* JADX INFO: Access modifiers changed from: private */ public void a(giW giw) { giY giy = giw.b; giY giy2 = giY.NONE; if (giy2 != null) { giw.b = giy2; giy.clearDeadline(); giy.clearTimeout(); return; } throw new IllegalArgumentException("delegate == null"); } @Override // okhttp3.repackaged.internal.http.HttpStream public final void setHttpEngine(HttpEngine httpEngine) { this.httpEngine = httpEngine; } public final boolean isClosed() { return this.state == 6; } }