package io.netty.handler.codec.http2;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.http2.Http2FrameWriter;
import io.netty.handler.codec.http2.Http2HeadersEncoder;
import io.netty.util.collection.CharObjectMap;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;

/* JADX INFO: loaded from: classes.dex */
public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSizePolicy, Http2FrameWriter.Configuration {
    private static final String STREAM_DEPENDENCY = "Stream Dependency";
    private static final String STREAM_ID = "Stream ID";
    private static final ByteBuf ZERO_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer(DnsRecord.CLASS_ANY).writeZero(DnsRecord.CLASS_ANY)).asReadOnly();
    private final Http2HeadersEncoder headersEncoder;
    private int maxFrameSize;

    public DefaultHttp2FrameWriter() {
        this(new DefaultHttp2HeadersEncoder());
    }

    public DefaultHttp2FrameWriter(Http2HeadersEncoder.SensitivityDetector sensitivityDetector) {
        this(new DefaultHttp2HeadersEncoder(sensitivityDetector));
    }

    public DefaultHttp2FrameWriter(Http2HeadersEncoder.SensitivityDetector sensitivityDetector, boolean z2) {
        this(new DefaultHttp2HeadersEncoder(sensitivityDetector, z2));
    }

    public DefaultHttp2FrameWriter(Http2HeadersEncoder http2HeadersEncoder) {
        this.headersEncoder = http2HeadersEncoder;
        this.maxFrameSize = 16384;
    }

    private static int paddingBytes(int i2) {
        return i2 - 1;
    }

    private static void verifyErrorCode(long j2) {
        if (j2 < 0 || j2 > 4294967295L) {
            throw new IllegalArgumentException(a.q("Invalid errorCode: ", j2));
        }
    }

    private static void verifyPingPayload(ByteBuf byteBuf) {
        if (byteBuf == null || byteBuf.readableBytes() != 8) {
            throw new IllegalArgumentException("Opaque data must be 8 bytes");
        }
    }

    private static void verifyStreamId(int i2, String str) {
        if (i2 <= 0) {
            throw new IllegalArgumentException(a.t(str, " must be > 0"));
        }
    }

    private static void verifyStreamOrConnectionId(int i2, String str) {
        if (i2 < 0) {
            throw new IllegalArgumentException(a.t(str, " must be >= 0"));
        }
    }

    private static void verifyWeight(short s2) {
        if (s2 < 1 || s2 > 256) {
            throw new IllegalArgumentException(a.l("Invalid weight: ", s2));
        }
    }

    private static void verifyWindowSizeIncrement(int i2) {
        if (i2 < 0) {
            throw new IllegalArgumentException("WindowSizeIncrement must be >= 0");
        }
    }

    private ChannelFuture writeContinuationFrames(ChannelHandlerContext channelHandlerContext, int i2, ByteBuf byteBuf, int i3, Http2CodecUtil.SimpleChannelPromiseAggregator simpleChannelPromiseAggregator) {
        Http2Flags http2FlagsPaddingPresent = new Http2Flags().paddingPresent(i3 > 0);
        int i4 = this.maxFrameSize - i3;
        if (i4 <= 0) {
            return simpleChannelPromiseAggregator.setFailure((Throwable) new IllegalArgumentException(a.y(a.G("Padding [", i3, "] is too large for max frame size ["), this.maxFrameSize, "]")));
        }
        if (byteBuf.isReadable()) {
            int iMin = Math.min(byteBuf.readableBytes(), i4) + i3;
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(10);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, iMin, (byte) 9, http2FlagsPaddingPresent, i2);
            writePaddingLength(byteBufBuffer, i3);
            do {
                int iMin2 = Math.min(byteBuf.readableBytes(), i4);
                ByteBuf retainedSlice = byteBuf.readRetainedSlice(iMin2);
                int i5 = iMin2 + i3;
                if (byteBuf.isReadable()) {
                    channelHandlerContext.write(byteBufBuffer.retain(), simpleChannelPromiseAggregator.newPromise());
                } else {
                    http2FlagsPaddingPresent = http2FlagsPaddingPresent.endOfHeaders(true);
                    byteBufBuffer.release();
                    byteBufBuffer = channelHandlerContext.alloc().buffer(10);
                    Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, i5, (byte) 9, http2FlagsPaddingPresent, i2);
                    writePaddingLength(byteBufBuffer, i3);
                    channelHandlerContext.write(byteBufBuffer, simpleChannelPromiseAggregator.newPromise());
                }
                channelHandlerContext.write(retainedSlice, simpleChannelPromiseAggregator.newPromise());
                if (paddingBytes(i3) > 0) {
                    channelHandlerContext.write(ZERO_BUFFER.slice(0, paddingBytes(i3)), simpleChannelPromiseAggregator.newPromise());
                }
            } while (byteBuf.isReadable());
        }
        return simpleChannelPromiseAggregator;
    }

    private ChannelFuture writeHeadersInternal(ChannelHandlerContext channelHandlerContext, int i2, Http2Headers http2Headers, int i3, boolean z2, boolean z3, int i4, short s2, boolean z4, ChannelPromise channelPromise) {
        int i5 = i4;
        Http2CodecUtil.SimpleChannelPromiseAggregator simpleChannelPromiseAggregator = new Http2CodecUtil.SimpleChannelPromiseAggregator(channelPromise, channelHandlerContext.channel(), channelHandlerContext.executor());
        ByteBuf byteBufBuffer = null;
        try {
            try {
                try {
                    verifyStreamId(i2, STREAM_ID);
                    if (z3) {
                        verifyStreamOrConnectionId(i5, STREAM_DEPENDENCY);
                        Http2CodecUtil.verifyPadding(i3);
                        verifyWeight(s2);
                    }
                    byteBufBuffer = channelHandlerContext.alloc().buffer();
                    this.headersEncoder.encodeHeaders(i2, http2Headers, byteBufBuffer);
                    Http2Flags http2FlagsPaddingPresent = new Http2Flags().endOfStream(z2).priorityPresent(z3).paddingPresent(i3 > 0);
                    int numPriorityBytes = http2FlagsPaddingPresent.getNumPriorityBytes() + i3;
                    ByteBuf retainedSlice = byteBufBuffer.readRetainedSlice(Math.min(byteBufBuffer.readableBytes(), this.maxFrameSize - numPriorityBytes));
                    http2FlagsPaddingPresent.endOfHeaders(!byteBufBuffer.isReadable());
                    int i6 = retainedSlice.readableBytes() + numPriorityBytes;
                    ByteBuf byteBufBuffer2 = channelHandlerContext.alloc().buffer(15);
                    Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer2, i6, (byte) 1, http2FlagsPaddingPresent, i2);
                    writePaddingLength(byteBufBuffer2, i3);
                    if (z3) {
                        if (z4) {
                            i5 = (int) (((long) i5) | 2147483648L);
                        }
                        byteBufBuffer2.writeInt(i5);
                        byteBufBuffer2.writeByte(s2 - 1);
                    }
                    channelHandlerContext.write(byteBufBuffer2, simpleChannelPromiseAggregator.newPromise());
                    channelHandlerContext.write(retainedSlice, simpleChannelPromiseAggregator.newPromise());
                    if (paddingBytes(i3) > 0) {
                        channelHandlerContext.write(ZERO_BUFFER.slice(0, paddingBytes(i3)), simpleChannelPromiseAggregator.newPromise());
                    }
                    if (!http2FlagsPaddingPresent.endOfHeaders()) {
                        writeContinuationFrames(channelHandlerContext, i2, byteBufBuffer, i3, simpleChannelPromiseAggregator);
                    }
                } catch (Throwable th) {
                    simpleChannelPromiseAggregator.setFailure(th);
                    simpleChannelPromiseAggregator.doneAllocatingPromises();
                    PlatformDependent.throwException(th);
                    if (byteBufBuffer != null) {
                    }
                    return simpleChannelPromiseAggregator.doneAllocatingPromises();
                }
            } catch (Http2Exception e2) {
                simpleChannelPromiseAggregator.setFailure((Throwable) e2);
                if (byteBufBuffer != null) {
                }
                return simpleChannelPromiseAggregator.doneAllocatingPromises();
            }
            byteBufBuffer.release();
            return simpleChannelPromiseAggregator.doneAllocatingPromises();
        } catch (Throwable th2) {
            if (byteBufBuffer != null) {
                byteBufBuffer.release();
            }
            throw th2;
        }
    }

    private static void writePaddingLength(ByteBuf byteBuf, int i2) {
        if (i2 > 0) {
            byteBuf.writeByte(i2 - 1);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public Http2FrameWriter.Configuration configuration() {
        return this;
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter.Configuration
    public Http2FrameSizePolicy frameSizePolicy() {
        return this;
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter.Configuration
    public Http2HeadersEncoder.Configuration headersConfiguration() {
        return this.headersEncoder.configuration();
    }

    @Override // io.netty.handler.codec.http2.Http2FrameSizePolicy
    public int maxFrameSize() {
        return this.maxFrameSize;
    }

    @Override // io.netty.handler.codec.http2.Http2FrameSizePolicy
    public void maxFrameSize(int i2) throws Http2Exception {
        if (!Http2CodecUtil.isMaxFrameSizeValid(i2)) {
            throw Http2Exception.connectionError(Http2Error.FRAME_SIZE_ERROR, "Invalid MAX_FRAME_SIZE specified in sent settings: %d", Integer.valueOf(i2));
        }
        this.maxFrameSize = i2;
    }

    /* JADX WARN: Removed duplicated region for block: B:39:0x00f8  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x00fe  */
    /* JADX WARN: Removed duplicated region for block: B:43:0x00ff  */
    /* JADX WARN: Removed duplicated region for block: B:46:0x0114  */
    /* JADX WARN: Removed duplicated region for block: B:55:0x0139 A[Catch: all -> 0x0153, TRY_LEAVE, TryCatch #1 {all -> 0x0153, blocks: (B:34:0x00d1, B:40:0x00f9, B:44:0x0100, B:47:0x0116, B:52:0x0128, B:53:0x0133, B:55:0x0139), top: B:78:0x00d1 }] */
    /* JADX WARN: Removed duplicated region for block: B:67:0x0160  */
    /* JADX WARN: Removed duplicated region for block: B:81:0x0165 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    @Override // io.netty.handler.codec.http2.Http2DataWriter
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public io.netty.channel.ChannelFuture writeData(io.netty.channel.ChannelHandlerContext r17, int r18, io.netty.buffer.ByteBuf r19, int r20, boolean r21, io.netty.channel.ChannelPromise r22) {
        /*
            Method dump skipped, instruction units count: 377
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.http2.DefaultHttp2FrameWriter.writeData(io.netty.channel.ChannelHandlerContext, int, io.netty.buffer.ByteBuf, int, boolean, io.netty.channel.ChannelPromise):io.netty.channel.ChannelFuture");
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeFrame(ChannelHandlerContext channelHandlerContext, byte b2, int i2, Http2Flags http2Flags, ByteBuf byteBuf, ChannelPromise channelPromise) {
        Http2CodecUtil.SimpleChannelPromiseAggregator simpleChannelPromiseAggregator = new Http2CodecUtil.SimpleChannelPromiseAggregator(channelPromise, channelHandlerContext.channel(), channelHandlerContext.executor());
        try {
            verifyStreamOrConnectionId(i2, STREAM_ID);
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(9);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, byteBuf.readableBytes(), b2, http2Flags, i2);
            channelHandlerContext.write(byteBufBuffer, simpleChannelPromiseAggregator.newPromise());
            try {
                channelHandlerContext.write(byteBuf, simpleChannelPromiseAggregator.newPromise());
            } catch (Throwable th) {
                simpleChannelPromiseAggregator.setFailure(th);
            }
            return simpleChannelPromiseAggregator.doneAllocatingPromises();
        } catch (Throwable th2) {
            try {
                byteBuf.release();
                return simpleChannelPromiseAggregator;
            } finally {
                simpleChannelPromiseAggregator.setFailure(th2);
                simpleChannelPromiseAggregator.doneAllocatingPromises();
            }
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeGoAway(ChannelHandlerContext channelHandlerContext, int i2, long j2, ByteBuf byteBuf, ChannelPromise channelPromise) {
        Http2CodecUtil.SimpleChannelPromiseAggregator simpleChannelPromiseAggregator = new Http2CodecUtil.SimpleChannelPromiseAggregator(channelPromise, channelHandlerContext.channel(), channelHandlerContext.executor());
        try {
            verifyStreamOrConnectionId(i2, "Last Stream ID");
            verifyErrorCode(j2);
            int i3 = byteBuf.readableBytes() + 8;
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(17);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, i3, (byte) 7, new Http2Flags(), 0);
            byteBufBuffer.writeInt(i2);
            byteBufBuffer.writeInt((int) j2);
            channelHandlerContext.write(byteBufBuffer, simpleChannelPromiseAggregator.newPromise());
            try {
                channelHandlerContext.write(byteBuf, simpleChannelPromiseAggregator.newPromise());
            } catch (Throwable th) {
                simpleChannelPromiseAggregator.setFailure(th);
            }
            return simpleChannelPromiseAggregator.doneAllocatingPromises();
        } catch (Throwable th2) {
            try {
                byteBuf.release();
                return simpleChannelPromiseAggregator;
            } finally {
                simpleChannelPromiseAggregator.setFailure(th2);
                simpleChannelPromiseAggregator.doneAllocatingPromises();
            }
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeHeaders(ChannelHandlerContext channelHandlerContext, int i2, Http2Headers http2Headers, int i3, short s2, boolean z2, int i4, boolean z3, ChannelPromise channelPromise) {
        return writeHeadersInternal(channelHandlerContext, i2, http2Headers, i4, z3, true, i3, s2, z2, channelPromise);
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeHeaders(ChannelHandlerContext channelHandlerContext, int i2, Http2Headers http2Headers, int i3, boolean z2, ChannelPromise channelPromise) {
        return writeHeadersInternal(channelHandlerContext, i2, http2Headers, i3, z2, false, 0, (short) 0, false, channelPromise);
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writePing(ChannelHandlerContext channelHandlerContext, boolean z2, long j2, ChannelPromise channelPromise) {
        Http2Flags http2FlagsAck = z2 ? new Http2Flags().ack(true) : new Http2Flags();
        ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(17);
        Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, 8, (byte) 6, http2FlagsAck, 0);
        byteBufBuffer.writeLong(j2);
        return channelHandlerContext.write(byteBufBuffer, channelPromise);
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writePriority(ChannelHandlerContext channelHandlerContext, int i2, int i3, short s2, boolean z2, ChannelPromise channelPromise) {
        try {
            verifyStreamId(i2, STREAM_ID);
            verifyStreamId(i3, STREAM_DEPENDENCY);
            verifyWeight(s2);
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(14);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, 5, (byte) 2, new Http2Flags(), i2);
            if (z2) {
                i3 = (int) (((long) i3) | 2147483648L);
            }
            byteBufBuffer.writeInt(i3);
            byteBufBuffer.writeByte(s2 - 1);
            return channelHandlerContext.write(byteBufBuffer, channelPromise);
        } catch (Throwable th) {
            return channelPromise.setFailure(th);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writePushPromise(ChannelHandlerContext channelHandlerContext, int i2, int i3, Http2Headers http2Headers, int i4, ChannelPromise channelPromise) {
        Http2CodecUtil.SimpleChannelPromiseAggregator simpleChannelPromiseAggregator = new Http2CodecUtil.SimpleChannelPromiseAggregator(channelPromise, channelHandlerContext.channel(), channelHandlerContext.executor());
        ByteBuf byteBufBuffer = null;
        try {
            try {
                verifyStreamId(i2, STREAM_ID);
                verifyStreamId(i3, "Promised Stream ID");
                Http2CodecUtil.verifyPadding(i4);
                byteBufBuffer = channelHandlerContext.alloc().buffer();
                this.headersEncoder.encodeHeaders(i2, http2Headers, byteBufBuffer);
                boolean z2 = true;
                Http2Flags http2FlagsPaddingPresent = new Http2Flags().paddingPresent(i4 > 0);
                int i5 = i4 + 4;
                ByteBuf retainedSlice = byteBufBuffer.readRetainedSlice(Math.min(byteBufBuffer.readableBytes(), this.maxFrameSize - i5));
                if (byteBufBuffer.isReadable()) {
                    z2 = false;
                }
                http2FlagsPaddingPresent.endOfHeaders(z2);
                int i6 = retainedSlice.readableBytes() + i5;
                ByteBuf byteBufBuffer2 = channelHandlerContext.alloc().buffer(14);
                Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer2, i6, (byte) 5, http2FlagsPaddingPresent, i2);
                writePaddingLength(byteBufBuffer2, i4);
                byteBufBuffer2.writeInt(i3);
                channelHandlerContext.write(byteBufBuffer2, simpleChannelPromiseAggregator.newPromise());
                channelHandlerContext.write(retainedSlice, simpleChannelPromiseAggregator.newPromise());
                if (paddingBytes(i4) > 0) {
                    channelHandlerContext.write(ZERO_BUFFER.slice(0, paddingBytes(i4)), simpleChannelPromiseAggregator.newPromise());
                }
                if (!http2FlagsPaddingPresent.endOfHeaders()) {
                    writeContinuationFrames(channelHandlerContext, i2, byteBufBuffer, i4, simpleChannelPromiseAggregator);
                }
            } finally {
                if (byteBufBuffer != null) {
                    byteBufBuffer.release();
                }
            }
        } catch (Http2Exception e2) {
            simpleChannelPromiseAggregator.setFailure((Throwable) e2);
            return simpleChannelPromiseAggregator.doneAllocatingPromises();
        } catch (Throwable th) {
            simpleChannelPromiseAggregator.setFailure(th);
            simpleChannelPromiseAggregator.doneAllocatingPromises();
            PlatformDependent.throwException(th);
            if (byteBufBuffer != null) {
            }
            return simpleChannelPromiseAggregator.doneAllocatingPromises();
        }
        return simpleChannelPromiseAggregator.doneAllocatingPromises();
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeRstStream(ChannelHandlerContext channelHandlerContext, int i2, long j2, ChannelPromise channelPromise) {
        try {
            verifyStreamId(i2, STREAM_ID);
            verifyErrorCode(j2);
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(13);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, 4, (byte) 3, new Http2Flags(), i2);
            byteBufBuffer.writeInt((int) j2);
            return channelHandlerContext.write(byteBufBuffer, channelPromise);
        } catch (Throwable th) {
            return channelPromise.setFailure(th);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeSettings(ChannelHandlerContext channelHandlerContext, Http2Settings http2Settings, ChannelPromise channelPromise) {
        try {
            ObjectUtil.checkNotNull(http2Settings, "settings");
            int size = http2Settings.size() * 6;
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer((http2Settings.size() * 6) + 9);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, size, (byte) 4, new Http2Flags(), 0);
            for (CharObjectMap.PrimitiveEntry<Long> primitiveEntry : http2Settings.entries()) {
                byteBufBuffer.writeChar(primitiveEntry.key());
                byteBufBuffer.writeInt(primitiveEntry.value().intValue());
            }
            return channelHandlerContext.write(byteBufBuffer, channelPromise);
        } catch (Throwable th) {
            return channelPromise.setFailure(th);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeSettingsAck(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        try {
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(9);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, 0, (byte) 4, new Http2Flags().ack(true), 0);
            return channelHandlerContext.write(byteBufBuffer, channelPromise);
        } catch (Throwable th) {
            return channelPromise.setFailure(th);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2FrameWriter
    public ChannelFuture writeWindowUpdate(ChannelHandlerContext channelHandlerContext, int i2, int i3, ChannelPromise channelPromise) {
        try {
            verifyStreamOrConnectionId(i2, STREAM_ID);
            verifyWindowSizeIncrement(i3);
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(13);
            Http2CodecUtil.writeFrameHeaderInternal(byteBufBuffer, 4, (byte) 8, new Http2Flags(), i2);
            byteBufBuffer.writeInt(i3);
            return channelHandlerContext.write(byteBufBuffer, channelPromise);
        } catch (Throwable th) {
            return channelPromise.setFailure(th);
        }
    }
}
