package io.netty.handler.codec.http;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.CombinedChannelDuplexHandler;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.handler.codec.http.HttpClientUpgradeHandler;
import io.netty.util.ReferenceCountUtil;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicLong;

/* JADX INFO: loaded from: classes.dex */
public final class HttpClientCodec extends CombinedChannelDuplexHandler<HttpResponseDecoder, HttpRequestEncoder> implements HttpClientUpgradeHandler.SourceCodec {
    private boolean done;
    private final boolean failOnMissingResponse;
    private final boolean parseHttpAfterConnectRequest;
    private final Queue<HttpMethod> queue;
    private final AtomicLong requestResponseCounter;

    public final class Decoder extends HttpResponseDecoder {
        public Decoder(int i2, int i3, int i4, boolean z2) {
            super(i2, i3, i4, z2);
        }

        public Decoder(int i2, int i3, int i4, boolean z2, int i5) {
            super(i2, i3, i4, z2, i5);
        }

        private void decrement(Object obj) {
            if (obj != null && (obj instanceof LastHttpContent)) {
                HttpClientCodec.this.requestResponseCounter.decrementAndGet();
            }
        }

        @Override // io.netty.handler.codec.ByteToMessageDecoder, io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
        public void channelInactive(ChannelHandlerContext channelHandlerContext) {
            super.channelInactive(channelHandlerContext);
            if (HttpClientCodec.this.failOnMissingResponse) {
                long j2 = HttpClientCodec.this.requestResponseCounter.get();
                if (j2 > 0) {
                    channelHandlerContext.fireExceptionCaught((Throwable) new PrematureChannelClosureException(a.r("channel gone inactive with ", j2, " missing response(s)")));
                }
            }
        }

        @Override // io.netty.handler.codec.http.HttpObjectDecoder, io.netty.handler.codec.ByteToMessageDecoder
        public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
            if (HttpClientCodec.this.done) {
                int iActualReadableBytes = actualReadableBytes();
                if (iActualReadableBytes == 0) {
                    return;
                }
                list.add(byteBuf.readBytes(iActualReadableBytes));
                return;
            }
            super.decode(channelHandlerContext, byteBuf, list);
            if (HttpClientCodec.this.failOnMissingResponse) {
                int size = list.size();
                for (int size2 = list.size(); size2 < size; size2++) {
                    decrement(list.get(size2));
                }
            }
        }

        @Override // io.netty.handler.codec.http.HttpObjectDecoder
        public boolean isContentAlwaysEmpty(HttpMessage httpMessage) {
            int iCode = ((HttpResponse) httpMessage).status().code();
            if (iCode == 100 || iCode == 101) {
                return super.isContentAlwaysEmpty(httpMessage);
            }
            HttpMethod httpMethod = (HttpMethod) HttpClientCodec.this.queue.poll();
            char cCharAt = httpMethod.name().charAt(0);
            if (cCharAt != 'C') {
                if (cCharAt == 'H' && HttpMethod.HEAD.equals(httpMethod)) {
                    return true;
                }
            } else if (iCode == 200 && HttpMethod.CONNECT.equals(httpMethod)) {
                if (!HttpClientCodec.this.parseHttpAfterConnectRequest) {
                    HttpClientCodec.this.done = true;
                    HttpClientCodec.this.queue.clear();
                }
                return true;
            }
            return super.isContentAlwaysEmpty(httpMessage);
        }
    }

    public final class Encoder extends HttpRequestEncoder {
        public boolean upgraded;

        private Encoder() {
        }

        @Override // io.netty.handler.codec.http.HttpObjectEncoder, io.netty.handler.codec.MessageToMessageEncoder
        public void encode(ChannelHandlerContext channelHandlerContext, Object obj, List<Object> list) {
            if (this.upgraded) {
                list.add(ReferenceCountUtil.retain(obj));
                return;
            }
            if ((obj instanceof HttpRequest) && !HttpClientCodec.this.done) {
                HttpClientCodec.this.queue.offer(((HttpRequest) obj).method());
            }
            super.encode(channelHandlerContext, obj, list);
            if (HttpClientCodec.this.failOnMissingResponse && !HttpClientCodec.this.done && (obj instanceof LastHttpContent)) {
                HttpClientCodec.this.requestResponseCounter.incrementAndGet();
            }
        }
    }

    public HttpClientCodec() {
        this(4096, 8192, 8192, false);
    }

    public HttpClientCodec(int i2, int i3, int i4) {
        this(i2, i3, i4, false);
    }

    public HttpClientCodec(int i2, int i3, int i4, boolean z2) {
        this(i2, i3, i4, z2, true);
    }

    public HttpClientCodec(int i2, int i3, int i4, boolean z2, boolean z3) {
        this(i2, i3, i4, z2, z3, false);
    }

    public HttpClientCodec(int i2, int i3, int i4, boolean z2, boolean z3, int i5) {
        this(i2, i3, i4, z2, z3, i5, false);
    }

    public HttpClientCodec(int i2, int i3, int i4, boolean z2, boolean z3, int i5, boolean z4) {
        this.queue = new ArrayDeque();
        this.requestResponseCounter = new AtomicLong();
        init(new Decoder(i2, i3, i4, z3, i5), new Encoder());
        this.parseHttpAfterConnectRequest = z4;
        this.failOnMissingResponse = z2;
    }

    public HttpClientCodec(int i2, int i3, int i4, boolean z2, boolean z3, boolean z4) {
        this.queue = new ArrayDeque();
        this.requestResponseCounter = new AtomicLong();
        init(new Decoder(i2, i3, i4, z3), new Encoder());
        this.failOnMissingResponse = z2;
        this.parseHttpAfterConnectRequest = z4;
    }

    public boolean isSingleDecode() {
        return inboundHandler().isSingleDecode();
    }

    @Override // io.netty.handler.codec.http.HttpClientUpgradeHandler.SourceCodec
    public void prepareUpgradeFrom(ChannelHandlerContext channelHandlerContext) {
        ((Encoder) outboundHandler()).upgraded = true;
    }

    public void setSingleDecode(boolean z2) {
        inboundHandler().setSingleDecode(z2);
    }

    @Override // io.netty.handler.codec.http.HttpClientUpgradeHandler.SourceCodec
    public void upgradeFrom(ChannelHandlerContext channelHandlerContext) {
        channelHandlerContext.pipeline().remove(this);
    }
}
