what-the-bank/sources/okhttp3/internal/http2/Hpack.java

573 lines
24 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.http2;
import io.beid.beidk.definitions.LivenessStatus;
import io.beid.beidk.definitions.SCBHeader;
import io.grpc.internal.GrpcUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import o.C14881gav;
import o.C14953gcr;
import o.C14957gcv;
import o.C15111ghZ;
import o.C15155gij;
import o.C15160gio;
import o.InterfaceC15149gid;
import o.gdP;
import o.giD;
import okhttp3.internal.Util;
/* loaded from: classes.dex */
public final class Hpack {
public static final Hpack INSTANCE;
private static final Map<C15155gij, Integer> NAME_TO_FIRST_INDEX;
private static final int PREFIX_4_BITS = 15;
private static final int PREFIX_5_BITS = 31;
private static final int PREFIX_6_BITS = 63;
private static final int PREFIX_7_BITS = 127;
private static final int SETTINGS_HEADER_TABLE_SIZE = 4096;
private static final int SETTINGS_HEADER_TABLE_SIZE_LIMIT = 16384;
private static final Header[] STATIC_HEADER_TABLE;
private Hpack() {
}
static {
Hpack hpack = new Hpack();
INSTANCE = hpack;
STATIC_HEADER_TABLE = new Header[]{new Header(Header.TARGET_AUTHORITY, ""), new Header(Header.TARGET_METHOD, "GET"), new Header(Header.TARGET_METHOD, GrpcUtil.HTTP_METHOD), new Header(Header.TARGET_PATH, "/"), new Header(Header.TARGET_PATH, "/index.html"), new Header(Header.TARGET_SCHEME, "http"), new Header(Header.TARGET_SCHEME, "https"), new Header(Header.RESPONSE_STATUS, "200"), new Header(Header.RESPONSE_STATUS, "204"), new Header(Header.RESPONSE_STATUS, "206"), new Header(Header.RESPONSE_STATUS, "304"), new Header(Header.RESPONSE_STATUS, "400"), new Header(Header.RESPONSE_STATUS, "404"), new Header(Header.RESPONSE_STATUS, LivenessStatus.ERROR_UNKNOW), new Header("accept-charset", ""), new Header(GrpcUtil.CONTENT_ACCEPT_ENCODING, "gzip, deflate"), new Header(SCBHeader.SCB_REQ_HEADER_ACCEPT_LANGUAGE, ""), new Header("accept-ranges", ""), new Header("accept", ""), new Header("access-control-allow-origin", ""), new Header("age", ""), new Header("allow", ""), new Header("authorization", ""), new Header("cache-control", ""), new Header("content-disposition", ""), new Header(GrpcUtil.CONTENT_ENCODING, ""), new Header("content-language", ""), new Header("content-length", ""), new Header("content-location", ""), new Header("content-range", ""), new Header("content-type", ""), new Header("cookie", ""), new Header("date", ""), new Header("etag", ""), new Header("expect", ""), new Header("expires", ""), new Header("from", ""), new Header("host", ""), new Header("if-match", ""), new Header("if-modified-since", ""), new Header("if-none-match", ""), new Header("if-range", ""), new Header("if-unmodified-since", ""), new Header("last-modified", ""), new Header("link", ""), new Header("location", ""), new Header("max-forwards", ""), new Header("proxy-authenticate", ""), new Header("proxy-authorization", ""), new Header("range", ""), new Header("referer", ""), new Header("refresh", ""), new Header("retry-after", ""), new Header("server", ""), new Header("set-cookie", ""), new Header("strict-transport-security", ""), new Header("transfer-encoding", ""), new Header("user-agent", ""), new Header("vary", ""), new Header("via", ""), new Header("www-authenticate", "")};
NAME_TO_FIRST_INDEX = hpack.nameToFirstIndex();
}
/* loaded from: classes.dex */
public static final class Reader {
public Header[] dynamicTable;
public int dynamicTableByteCount;
public int headerCount;
private final List<Header> headerList;
private final int headerTableSizeSetting;
private int maxDynamicTableByteCount;
private int nextHeaderIndex;
private final InterfaceC15149gid source;
public Reader(giD gid, int i, int i2) {
C14957gcv.e(gid, "");
this.headerTableSizeSetting = i;
this.maxDynamicTableByteCount = i2;
this.headerList = new ArrayList();
this.source = C15160gio.c(gid);
this.dynamicTable = new Header[8];
this.nextHeaderIndex = 7;
}
public /* synthetic */ Reader(giD gid, int i, int i2, int i3, C14953gcr c14953gcr) {
this(gid, i, (i3 & 4) != 0 ? i : i2);
}
public final List<Header> getAndResetHeaderList() {
List<Header> d = C14881gav.d((Iterable) this.headerList);
this.headerList.clear();
return d;
}
private final void adjustDynamicTableByteCount() {
int i = this.maxDynamicTableByteCount;
int i2 = this.dynamicTableByteCount;
if (i < i2) {
if (i == 0) {
clearDynamicTable();
} else {
evictToRecoverBytes(i2 - i);
}
}
}
private final int evictToRecoverBytes(int i) {
int i2;
int i3 = 0;
if (i > 0) {
int length = this.dynamicTable.length;
while (true) {
length--;
i2 = this.nextHeaderIndex;
if (length < i2 || i <= 0) {
break;
}
Header header = this.dynamicTable[length];
C14957gcv.e(header);
i -= header.hpackSize;
this.dynamicTableByteCount -= header.hpackSize;
this.headerCount--;
i3++;
}
Header[] headerArr = this.dynamicTable;
int i4 = i2 + 1;
System.arraycopy(headerArr, i4, headerArr, i4 + i3, this.headerCount);
this.nextHeaderIndex += i3;
}
return i3;
}
public final void readHeaders() throws IOException {
while (!this.source.j()) {
int and = Util.and(this.source.f(), 255);
if (and == 128) {
throw new IOException("index == 0");
}
if ((and & 128) == 128) {
readIndexedHeader(readInt(and, 127) - 1);
} else if (and == 64) {
readLiteralHeaderWithIncrementalIndexingNewName();
} else if ((and & 64) == 64) {
readLiteralHeaderWithIncrementalIndexingIndexedName(readInt(and, 63) - 1);
} else if ((and & 32) == 32) {
int readInt = readInt(and, 31);
this.maxDynamicTableByteCount = readInt;
if (readInt < 0 || readInt > this.headerTableSizeSetting) {
throw new IOException(C14957gcv.c("Invalid dynamic table size update ", Integer.valueOf(this.maxDynamicTableByteCount)));
}
adjustDynamicTableByteCount();
} else if (and == 16 || and == 0) {
readLiteralHeaderWithoutIndexingNewName();
} else {
readLiteralHeaderWithoutIndexingIndexedName(readInt(and, 15) - 1);
}
}
}
private final void readIndexedHeader(int i) throws IOException {
if (isStaticHeader(i)) {
this.headerList.add(Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[i]);
return;
}
int dynamicTableIndex = dynamicTableIndex(i - Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length);
if (dynamicTableIndex >= 0) {
Header[] headerArr = this.dynamicTable;
if (dynamicTableIndex < headerArr.length) {
List<Header> list = this.headerList;
Header header = headerArr[dynamicTableIndex];
C14957gcv.e(header);
list.add(header);
return;
}
}
throw new IOException(C14957gcv.c("Header index too large ", Integer.valueOf(i + 1)));
}
private final void readLiteralHeaderWithoutIndexingIndexedName(int i) throws IOException {
this.headerList.add(new Header(getName(i), readByteString()));
}
private final void readLiteralHeaderWithoutIndexingNewName() throws IOException {
this.headerList.add(new Header(Hpack.INSTANCE.checkLowercase(readByteString()), readByteString()));
}
private final void readLiteralHeaderWithIncrementalIndexingIndexedName(int i) throws IOException {
insertIntoDynamicTable(-1, new Header(getName(i), readByteString()));
}
private final void readLiteralHeaderWithIncrementalIndexingNewName() throws IOException {
insertIntoDynamicTable(-1, new Header(Hpack.INSTANCE.checkLowercase(readByteString()), readByteString()));
}
private final C15155gij getName(int i) throws IOException {
if (isStaticHeader(i)) {
return Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[i].name;
}
int dynamicTableIndex = dynamicTableIndex(i - Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length);
if (dynamicTableIndex >= 0) {
Header[] headerArr = this.dynamicTable;
if (dynamicTableIndex < headerArr.length) {
Header header = headerArr[dynamicTableIndex];
C14957gcv.e(header);
return header.name;
}
}
throw new IOException(C14957gcv.c("Header index too large ", Integer.valueOf(i + 1)));
}
private final boolean isStaticHeader(int i) {
return i >= 0 && i <= Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length - 1;
}
private final void insertIntoDynamicTable(int i, Header header) {
this.headerList.add(header);
int i2 = header.hpackSize;
if (i != -1) {
Header header2 = this.dynamicTable[dynamicTableIndex(i)];
C14957gcv.e(header2);
i2 -= header2.hpackSize;
}
int i3 = this.maxDynamicTableByteCount;
if (i2 > i3) {
clearDynamicTable();
return;
}
int evictToRecoverBytes = evictToRecoverBytes((this.dynamicTableByteCount + i2) - i3);
if (i == -1) {
int i4 = this.headerCount;
Header[] headerArr = this.dynamicTable;
if (i4 + 1 > headerArr.length) {
Header[] headerArr2 = new Header[headerArr.length << 1];
System.arraycopy(headerArr, 0, headerArr2, headerArr.length, headerArr.length);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.dynamicTable = headerArr2;
}
int i5 = this.nextHeaderIndex;
this.nextHeaderIndex = i5 - 1;
this.dynamicTable[i5] = header;
this.headerCount++;
} else {
this.dynamicTable[i + dynamicTableIndex(i) + evictToRecoverBytes] = header;
}
this.dynamicTableByteCount += i2;
}
private final int readByte() throws IOException {
return Util.and(this.source.f(), 255);
}
public final int readInt(int i, int i2) throws IOException {
int i3 = i & i2;
if (i3 < i2) {
return i3;
}
int i4 = 0;
while (true) {
int readByte = readByte();
if ((readByte & 128) == 0) {
return i2 + (readByte << i4);
}
i2 += (readByte & 127) << i4;
i4 += 7;
}
}
public final C15155gij readByteString() throws IOException {
int readByte = readByte();
boolean z = (readByte & 128) == 128;
long readInt = readInt(readByte, 127);
if (z) {
C15111ghZ c15111ghZ = new C15111ghZ();
Huffman.INSTANCE.decode(this.source, readInt, c15111ghZ);
return c15111ghZ.c(c15111ghZ.c);
}
return this.source.c(readInt);
}
private final void clearDynamicTable() {
Header[] headerArr = this.dynamicTable;
int length = headerArr.length;
C14957gcv.e(headerArr, "");
Arrays.fill(headerArr, 0, length, (Object) null);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
public final int maxDynamicTableByteCount() {
return this.maxDynamicTableByteCount;
}
private final int dynamicTableIndex(int i) {
return this.nextHeaderIndex + 1 + i;
}
/* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
public Reader(giD gid, int i) {
this(gid, i, 0, 4, null);
C14957gcv.e(gid, "");
}
}
private final Map<C15155gij, Integer> nameToFirstIndex() {
Header[] headerArr = STATIC_HEADER_TABLE;
LinkedHashMap linkedHashMap = new LinkedHashMap(headerArr.length);
int length = headerArr.length;
for (int i = 0; i < length; i++) {
Header[] headerArr2 = STATIC_HEADER_TABLE;
if (!linkedHashMap.containsKey(headerArr2[i].name)) {
linkedHashMap.put(headerArr2[i].name, Integer.valueOf(i));
}
}
Map<C15155gij, Integer> unmodifiableMap = Collections.unmodifiableMap(linkedHashMap);
C14957gcv.c(unmodifiableMap, "");
return unmodifiableMap;
}
/* loaded from: classes.dex */
public static final class Writer {
public Header[] dynamicTable;
public int dynamicTableByteCount;
private boolean emitDynamicTableSizeUpdate;
public int headerCount;
public int headerTableSizeSetting;
public int maxDynamicTableByteCount;
private int nextHeaderIndex;
private final C15111ghZ out;
private int smallestHeaderTableSizeSetting;
private final boolean useCompression;
public Writer(int i, boolean z, C15111ghZ c15111ghZ) {
C14957gcv.e(c15111ghZ, "");
this.headerTableSizeSetting = i;
this.useCompression = z;
this.out = c15111ghZ;
this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
this.maxDynamicTableByteCount = i;
this.dynamicTable = new Header[8];
this.nextHeaderIndex = 7;
}
public /* synthetic */ Writer(int i, boolean z, C15111ghZ c15111ghZ, int i2, C14953gcr c14953gcr) {
this((i2 & 1) != 0 ? 4096 : i, (i2 & 2) != 0 ? true : z, c15111ghZ);
}
private final int evictToRecoverBytes(int i) {
int i2;
int i3 = 0;
if (i > 0) {
int length = this.dynamicTable.length;
while (true) {
length--;
i2 = this.nextHeaderIndex;
if (length < i2 || i <= 0) {
break;
}
Header header = this.dynamicTable[length];
C14957gcv.e(header);
i -= header.hpackSize;
int i4 = this.dynamicTableByteCount;
Header header2 = this.dynamicTable[length];
C14957gcv.e(header2);
this.dynamicTableByteCount = i4 - header2.hpackSize;
this.headerCount--;
i3++;
}
Header[] headerArr = this.dynamicTable;
int i5 = i2 + 1;
System.arraycopy(headerArr, i5, headerArr, i5 + i3, this.headerCount);
Header[] headerArr2 = this.dynamicTable;
int i6 = this.nextHeaderIndex + 1;
Arrays.fill(headerArr2, i6, i6 + i3, (Object) null);
this.nextHeaderIndex += i3;
}
return i3;
}
private final void insertIntoDynamicTable(Header header) {
int i = header.hpackSize;
int i2 = this.maxDynamicTableByteCount;
if (i > i2) {
clearDynamicTable();
return;
}
evictToRecoverBytes((this.dynamicTableByteCount + i) - i2);
int i3 = this.headerCount;
Header[] headerArr = this.dynamicTable;
if (i3 + 1 > headerArr.length) {
Header[] headerArr2 = new Header[headerArr.length << 1];
System.arraycopy(headerArr, 0, headerArr2, headerArr.length, headerArr.length);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.dynamicTable = headerArr2;
}
int i4 = this.nextHeaderIndex;
this.nextHeaderIndex = i4 - 1;
this.dynamicTable[i4] = header;
this.headerCount++;
this.dynamicTableByteCount += i;
}
public final void writeHeaders(List<Header> list) throws IOException {
int i;
int i2;
C14957gcv.e(list, "");
if (this.emitDynamicTableSizeUpdate) {
int i3 = this.smallestHeaderTableSizeSetting;
if (i3 < this.maxDynamicTableByteCount) {
writeInt(i3, 31, 32);
}
this.emitDynamicTableSizeUpdate = false;
this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
writeInt(this.maxDynamicTableByteCount, 31, 32);
}
int size = list.size();
for (int i4 = 0; i4 < size; i4++) {
Header header = list.get(i4);
C15155gij g = header.name.g();
C15155gij c15155gij = header.value;
Integer num = Hpack.INSTANCE.getNAME_TO_FIRST_INDEX().get(g);
if (num != null) {
int intValue = num.intValue();
i2 = intValue + 1;
if (2 <= i2 && i2 < 8) {
if (C14957gcv.b(Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[intValue].value, c15155gij)) {
i = i2;
} else if (C14957gcv.b(Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[i2].value, c15155gij)) {
i = intValue + 2;
}
}
i = -1;
} else {
i = -1;
i2 = -1;
}
if (i == -1) {
int i5 = this.nextHeaderIndex + 1;
int length = this.dynamicTable.length;
while (true) {
if (i5 >= length) {
break;
}
Header header2 = this.dynamicTable[i5];
C14957gcv.e(header2);
if (C14957gcv.b(header2.name, g)) {
Header header3 = this.dynamicTable[i5];
C14957gcv.e(header3);
if (C14957gcv.b(header3.value, c15155gij)) {
i = Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length + (i5 - this.nextHeaderIndex);
break;
} else if (i2 == -1) {
i2 = Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length + (i5 - this.nextHeaderIndex);
}
}
i5++;
}
}
if (i != -1) {
writeInt(i, 127, 128);
} else if (i2 == -1) {
this.out.b(64);
writeByteString(g);
writeByteString(c15155gij);
insertIntoDynamicTable(header);
} else {
C15155gij c15155gij2 = Header.PSEUDO_PREFIX;
C14957gcv.e(c15155gij2, "");
if (g.d(c15155gij2, 0, c15155gij2.a()) && !C14957gcv.b(Header.TARGET_AUTHORITY, g)) {
writeInt(i2, 15, 0);
writeByteString(c15155gij);
} else {
writeInt(i2, 63, 64);
writeByteString(c15155gij);
insertIntoDynamicTable(header);
}
}
}
}
public final void writeInt(int i, int i2, int i3) {
if (i < i2) {
this.out.b(i | i3);
return;
}
this.out.b(i3 | i2);
int i4 = i - i2;
while (i4 >= 128) {
this.out.b(128 | (i4 & 127));
i4 >>>= 7;
}
this.out.b(i4);
}
public final void writeByteString(C15155gij c15155gij) throws IOException {
C14957gcv.e(c15155gij, "");
if (this.useCompression && Huffman.INSTANCE.encodedLength(c15155gij) < c15155gij.a()) {
C15111ghZ c15111ghZ = new C15111ghZ();
Huffman.INSTANCE.encode(c15155gij, c15111ghZ);
C15155gij c = c15111ghZ.c(c15111ghZ.c);
writeInt(c.a(), 127, 128);
C15111ghZ c15111ghZ2 = this.out;
C14957gcv.e(c, "");
c.a(c15111ghZ2, c.a());
return;
}
writeInt(c15155gij.a(), 127, 0);
C15111ghZ c15111ghZ3 = this.out;
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ3, c15155gij.a());
}
public final void resizeHeaderTable(int i) {
this.headerTableSizeSetting = i;
int min = Math.min(i, 16384);
int i2 = this.maxDynamicTableByteCount;
if (i2 == min) {
return;
}
if (min < i2) {
this.smallestHeaderTableSizeSetting = Math.min(this.smallestHeaderTableSizeSetting, min);
}
this.emitDynamicTableSizeUpdate = true;
this.maxDynamicTableByteCount = min;
adjustDynamicTableByteCount();
}
private final void adjustDynamicTableByteCount() {
int i = this.maxDynamicTableByteCount;
int i2 = this.dynamicTableByteCount;
if (i < i2) {
if (i == 0) {
clearDynamicTable();
} else {
evictToRecoverBytes(i2 - i);
}
}
}
private final void clearDynamicTable() {
Header[] headerArr = this.dynamicTable;
int length = headerArr.length;
C14957gcv.e(headerArr, "");
Arrays.fill(headerArr, 0, length, (Object) null);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
/* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
public Writer(C15111ghZ c15111ghZ) {
this(0, false, c15111ghZ, 3, null);
C14957gcv.e(c15111ghZ, "");
}
/* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
public Writer(int i, C15111ghZ c15111ghZ) {
this(i, false, c15111ghZ, 2, null);
C14957gcv.e(c15111ghZ, "");
}
}
public final C15155gij checkLowercase(C15155gij c15155gij) throws IOException {
C14957gcv.e(c15155gij, "");
int a = c15155gij.a();
for (int i = 0; i < a; i++) {
byte c = c15155gij.c(i);
if (65 <= c && c <= 90) {
String str = c15155gij.d;
if (str == null) {
byte[] b = c15155gij.b();
C14957gcv.e(b, "");
String str2 = new String(b, gdP.a);
c15155gij.d = str2;
str = str2;
}
throw new IOException(C14957gcv.c("PROTOCOL_ERROR response malformed: mixed case name: ", (Object) str));
}
}
return c15155gij;
}
public final Header[] getSTATIC_HEADER_TABLE() {
return STATIC_HEADER_TABLE;
}
public final Map<C15155gij, Integer> getNAME_TO_FIRST_INDEX() {
return NAME_TO_FIRST_INDEX;
}
}