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.embedded.EmbeddedChannel;
import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.AsciiString;
import io.netty.util.ReferenceCountUtil;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObject> {
    public static final String IDENTITY = HttpHeaderValues.IDENTITY.toString();
    private boolean continueResponse;
    public ChannelHandlerContext ctx;
    private EmbeddedChannel decoder;

    private void cleanup() {
        EmbeddedChannel embeddedChannel = this.decoder;
        if (embeddedChannel != null) {
            embeddedChannel.finishAndReleaseAll();
            this.decoder = null;
        }
    }

    private void cleanupSafely(ChannelHandlerContext channelHandlerContext) {
        try {
            cleanup();
        } catch (Throwable th) {
            channelHandlerContext.fireExceptionCaught(th);
        }
    }

    private void decode(ByteBuf byteBuf, List<Object> list) {
        this.decoder.writeInbound(byteBuf.retain());
        fetchDecoderOutput(list);
    }

    private void decodeContent(HttpContent httpContent, List<Object> list) {
        decode(httpContent.content(), list);
        if (httpContent instanceof LastHttpContent) {
            finishDecode(list);
            HttpHeaders httpHeadersTrailingHeaders = ((LastHttpContent) httpContent).trailingHeaders();
            if (httpHeadersTrailingHeaders.isEmpty()) {
                list.add(LastHttpContent.EMPTY_LAST_CONTENT);
            } else {
                list.add(new ComposedLastHttpContent(httpHeadersTrailingHeaders));
            }
        }
    }

    private void fetchDecoderOutput(List<Object> list) {
        while (true) {
            ByteBuf byteBuf = (ByteBuf) this.decoder.readInbound();
            if (byteBuf == null) {
                return;
            }
            if (byteBuf.isReadable()) {
                list.add(new DefaultHttpContent(byteBuf));
            } else {
                byteBuf.release();
            }
        }
    }

    private void finishDecode(List<Object> list) {
        if (this.decoder.finish()) {
            fetchDecoderOutput(list);
        }
        this.decoder = null;
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
        cleanupSafely(channelHandlerContext);
        super.channelInactive(channelHandlerContext);
    }

    /* JADX INFO: renamed from: decode, reason: avoid collision after fix types in other method */
    public void decode2(ChannelHandlerContext channelHandlerContext, HttpObject httpObject, List<Object> list) {
        HttpMessage defaultHttpResponse;
        boolean z2;
        if ((httpObject instanceof HttpResponse) && ((HttpResponse) httpObject).status().code() == 100) {
            if (!(httpObject instanceof LastHttpContent)) {
                z2 = true;
                this.continueResponse = z2;
            }
        } else {
            if (!this.continueResponse) {
                if (httpObject instanceof HttpMessage) {
                    cleanup();
                    HttpMessage httpMessage = (HttpMessage) httpObject;
                    HttpHeaders httpHeadersHeaders = httpMessage.headers();
                    AsciiString asciiString = HttpHeaderNames.CONTENT_ENCODING;
                    String str = httpHeadersHeaders.get(asciiString);
                    String strTrim = str != null ? str.trim() : IDENTITY;
                    EmbeddedChannel embeddedChannelNewContentDecoder = newContentDecoder(strTrim);
                    this.decoder = embeddedChannelNewContentDecoder;
                    if (embeddedChannelNewContentDecoder == null) {
                        if (httpMessage instanceof HttpContent) {
                            ((HttpContent) httpMessage).retain();
                        }
                        list.add(httpMessage);
                        return;
                    }
                    AsciiString asciiString2 = HttpHeaderNames.CONTENT_LENGTH;
                    if (httpHeadersHeaders.contains(asciiString2)) {
                        httpHeadersHeaders.remove(asciiString2);
                        httpHeadersHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
                    }
                    String targetContentEncoding = getTargetContentEncoding(strTrim);
                    if (HttpHeaderValues.IDENTITY.contentEquals(targetContentEncoding)) {
                        httpHeadersHeaders.remove(asciiString);
                    } else {
                        httpHeadersHeaders.set(asciiString, targetContentEncoding);
                    }
                    if (httpMessage instanceof HttpContent) {
                        if (httpMessage instanceof HttpRequest) {
                            HttpRequest httpRequest = (HttpRequest) httpMessage;
                            defaultHttpResponse = new DefaultHttpRequest(httpRequest.protocolVersion(), httpRequest.method(), httpRequest.uri());
                        } else {
                            if (!(httpMessage instanceof HttpResponse)) {
                                StringBuilder sbF = a.F("Object of class ");
                                sbF.append(httpMessage.getClass().getName());
                                sbF.append(" is not a HttpRequest or HttpResponse");
                                throw new CodecException(sbF.toString());
                            }
                            HttpResponse httpResponse = (HttpResponse) httpMessage;
                            defaultHttpResponse = new DefaultHttpResponse(httpResponse.protocolVersion(), httpResponse.status());
                        }
                        defaultHttpResponse.headers().set(httpMessage.headers());
                        defaultHttpResponse.setDecoderResult(httpMessage.decoderResult());
                        list.add(defaultHttpResponse);
                    } else {
                        list.add(httpMessage);
                    }
                }
                if (httpObject instanceof HttpContent) {
                    HttpContent httpContent = (HttpContent) httpObject;
                    if (this.decoder == null) {
                        list.add(httpContent.retain());
                        return;
                    } else {
                        decodeContent(httpContent, list);
                        return;
                    }
                }
                return;
            }
            if (httpObject instanceof LastHttpContent) {
                z2 = false;
                this.continueResponse = z2;
            }
        }
        list.add(ReferenceCountUtil.retain(httpObject));
    }

    @Override // io.netty.handler.codec.MessageToMessageDecoder
    public /* bridge */ /* synthetic */ void decode(ChannelHandlerContext channelHandlerContext, HttpObject httpObject, List list) {
        decode2(channelHandlerContext, httpObject, (List<Object>) list);
    }

    public String getTargetContentEncoding(String str) {
        return IDENTITY;
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) {
        this.ctx = channelHandlerContext;
        super.handlerAdded(channelHandlerContext);
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) {
        cleanupSafely(channelHandlerContext);
        super.handlerRemoved(channelHandlerContext);
    }

    public abstract EmbeddedChannel newContentDecoder(String str);
}
