jimu-decompiled/sources/okhttp3/internal/http2/Hpack.java
2025-05-13 19:24:51 +02:00

488 lines
21 KiB
Java

package okhttp3.internal.http2;
import com.liulishuo.filedownloader.model.FileDownloadModel;
import com.ubt.jimu.base.entities.Constant;
import com.ubt.jimu.base.http.ApiConstants;
import com.ubt.jimu.unity.bluetooth.MyUnityListener;
import com.ubtrobot.jimu.robotapi.PeripheralType;
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 okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSource;
import okio.ByteString;
import okio.Okio;
import okio.Source;
/* loaded from: classes2.dex */
final class Hpack {
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;
static final Header[] STATIC_HEADER_TABLE = {new Header(Header.TARGET_AUTHORITY, ""), new Header(Header.TARGET_METHOD, "GET"), new Header(Header.TARGET_METHOD, "POST"), 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, "500"), new Header("accept-charset", ""), new Header("accept-encoding", "gzip, deflate"), new Header("accept-language", ""), new Header("accept-ranges", ""), new Header("accept", ""), new Header("access-control-allow-origin", ""), new Header("age", ""), new Header("allow", ""), new Header(ApiConstants.AUTHORIZATION, ""), new Header("cache-control", ""), new Header("content-disposition", ""), new Header("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(FileDownloadModel.ETAG, ""), new Header("expect", ""), new Header("expires", ""), new Header(MyUnityListener.CALLER, ""), 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(Constant.Community.SOURCE_LINK_TYPE, ""), 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", "")};
static final Map<ByteString, Integer> NAME_TO_FIRST_INDEX = nameToFirstIndex();
static final class Reader {
Header[] dynamicTable;
int dynamicTableByteCount;
int headerCount;
private final List<Header> headerList;
private final int headerTableSizeSetting;
private int maxDynamicTableByteCount;
int nextHeaderIndex;
private final BufferedSource source;
Reader(int i, Source source) {
this(i, i, source);
}
private void adjustDynamicTableByteCount() {
int i = this.maxDynamicTableByteCount;
int i2 = this.dynamicTableByteCount;
if (i < i2) {
if (i == 0) {
clearDynamicTable();
} else {
evictToRecoverBytes(i2 - i);
}
}
}
private void clearDynamicTable() {
Arrays.fill(this.dynamicTable, (Object) null);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
private int dynamicTableIndex(int i) {
return this.nextHeaderIndex + 1 + i;
}
private int evictToRecoverBytes(int i) {
int i2 = 0;
if (i > 0) {
int length = this.dynamicTable.length;
while (true) {
length--;
if (length < this.nextHeaderIndex || i <= 0) {
break;
}
Header[] headerArr = this.dynamicTable;
i -= headerArr[length].hpackSize;
this.dynamicTableByteCount -= headerArr[length].hpackSize;
this.headerCount--;
i2++;
}
Header[] headerArr2 = this.dynamicTable;
int i3 = this.nextHeaderIndex;
System.arraycopy(headerArr2, i3 + 1, headerArr2, i3 + 1 + i2, this.headerCount);
this.nextHeaderIndex += i2;
}
return i2;
}
private ByteString getName(int i) throws IOException {
if (isStaticHeader(i)) {
return Hpack.STATIC_HEADER_TABLE[i].name;
}
int dynamicTableIndex = dynamicTableIndex(i - Hpack.STATIC_HEADER_TABLE.length);
if (dynamicTableIndex >= 0) {
Header[] headerArr = this.dynamicTable;
if (dynamicTableIndex < headerArr.length) {
return headerArr[dynamicTableIndex].name;
}
}
throw new IOException("Header index too large " + (i + 1));
}
private void insertIntoDynamicTable(int i, Header header) {
this.headerList.add(header);
int i2 = header.hpackSize;
if (i != -1) {
i2 -= this.dynamicTable[dynamicTableIndex(i)].hpackSize;
}
int i3 = this.maxDynamicTableByteCount;
if (i2 > i3) {
clearDynamicTable();
return;
}
int evictToRecoverBytes = evictToRecoverBytes((this.dynamicTableByteCount + i2) - i3);
if (i == -1) {
int i4 = this.headerCount + 1;
Header[] headerArr = this.dynamicTable;
if (i4 > headerArr.length) {
Header[] headerArr2 = new Header[headerArr.length * 2];
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 boolean isStaticHeader(int i) {
return i >= 0 && i <= Hpack.STATIC_HEADER_TABLE.length - 1;
}
private int readByte() throws IOException {
return this.source.readByte() & 255;
}
private void readIndexedHeader(int i) throws IOException {
if (isStaticHeader(i)) {
this.headerList.add(Hpack.STATIC_HEADER_TABLE[i]);
return;
}
int dynamicTableIndex = dynamicTableIndex(i - Hpack.STATIC_HEADER_TABLE.length);
if (dynamicTableIndex >= 0) {
Header[] headerArr = this.dynamicTable;
if (dynamicTableIndex < headerArr.length) {
this.headerList.add(headerArr[dynamicTableIndex]);
return;
}
}
throw new IOException("Header index too large " + (i + 1));
}
private void readLiteralHeaderWithIncrementalIndexingIndexedName(int i) throws IOException {
insertIntoDynamicTable(-1, new Header(getName(i), readByteString()));
}
private void readLiteralHeaderWithIncrementalIndexingNewName() throws IOException {
insertIntoDynamicTable(-1, new Header(Hpack.checkLowercase(readByteString()), readByteString()));
}
private void readLiteralHeaderWithoutIndexingIndexedName(int i) throws IOException {
this.headerList.add(new Header(getName(i), readByteString()));
}
private void readLiteralHeaderWithoutIndexingNewName() throws IOException {
this.headerList.add(new Header(Hpack.checkLowercase(readByteString()), readByteString()));
}
public List<Header> getAndResetHeaderList() {
ArrayList arrayList = new ArrayList(this.headerList);
this.headerList.clear();
return arrayList;
}
int maxDynamicTableByteCount() {
return this.maxDynamicTableByteCount;
}
ByteString readByteString() throws IOException {
int readByte = readByte();
boolean z = (readByte & PeripheralType.SERVO) == 128;
int readInt = readInt(readByte, Hpack.PREFIX_7_BITS);
return z ? ByteString.of(Huffman.get().decode(this.source.readByteArray(readInt))) : this.source.readByteString(readInt);
}
void readHeaders() throws IOException {
while (!this.source.exhausted()) {
int readByte = this.source.readByte() & 255;
if (readByte == 128) {
throw new IOException("index == 0");
}
if ((readByte & PeripheralType.SERVO) == 128) {
readIndexedHeader(readInt(readByte, Hpack.PREFIX_7_BITS) - 1);
} else if (readByte == 64) {
readLiteralHeaderWithIncrementalIndexingNewName();
} else if ((readByte & 64) == 64) {
readLiteralHeaderWithIncrementalIndexingIndexedName(readInt(readByte, 63) - 1);
} else if ((readByte & 32) == 32) {
this.maxDynamicTableByteCount = readInt(readByte, 31);
int i = this.maxDynamicTableByteCount;
if (i < 0 || i > this.headerTableSizeSetting) {
throw new IOException("Invalid dynamic table size update " + this.maxDynamicTableByteCount);
}
adjustDynamicTableByteCount();
} else if (readByte == 16 || readByte == 0) {
readLiteralHeaderWithoutIndexingNewName();
} else {
readLiteralHeaderWithoutIndexingIndexedName(readInt(readByte, 15) - 1);
}
}
}
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 & PeripheralType.SERVO) == 0) {
return i2 + (readByte << i4);
}
i2 += (readByte & Hpack.PREFIX_7_BITS) << i4;
i4 += 7;
}
}
Reader(int i, int i2, Source source) {
this.headerList = new ArrayList();
this.dynamicTable = new Header[8];
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
this.headerTableSizeSetting = i;
this.maxDynamicTableByteCount = i2;
this.source = Okio.buffer(source);
}
}
static final class Writer {
private static final int SETTINGS_HEADER_TABLE_SIZE = 4096;
private static final int SETTINGS_HEADER_TABLE_SIZE_LIMIT = 16384;
Header[] dynamicTable;
int dynamicTableByteCount;
private boolean emitDynamicTableSizeUpdate;
int headerCount;
int headerTableSizeSetting;
int maxDynamicTableByteCount;
int nextHeaderIndex;
private final Buffer out;
private int smallestHeaderTableSizeSetting;
private final boolean useCompression;
Writer(Buffer buffer) {
this(4096, true, buffer);
}
private void adjustDynamicTableByteCount() {
int i = this.maxDynamicTableByteCount;
int i2 = this.dynamicTableByteCount;
if (i < i2) {
if (i == 0) {
clearDynamicTable();
} else {
evictToRecoverBytes(i2 - i);
}
}
}
private void clearDynamicTable() {
Arrays.fill(this.dynamicTable, (Object) null);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
private int evictToRecoverBytes(int i) {
int i2 = 0;
if (i > 0) {
int length = this.dynamicTable.length;
while (true) {
length--;
if (length < this.nextHeaderIndex || i <= 0) {
break;
}
Header[] headerArr = this.dynamicTable;
i -= headerArr[length].hpackSize;
this.dynamicTableByteCount -= headerArr[length].hpackSize;
this.headerCount--;
i2++;
}
Header[] headerArr2 = this.dynamicTable;
int i3 = this.nextHeaderIndex;
System.arraycopy(headerArr2, i3 + 1, headerArr2, i3 + 1 + i2, this.headerCount);
Header[] headerArr3 = this.dynamicTable;
int i4 = this.nextHeaderIndex;
Arrays.fill(headerArr3, i4 + 1, i4 + 1 + i2, (Object) null);
this.nextHeaderIndex += i2;
}
return i2;
}
private 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 + 1;
Header[] headerArr = this.dynamicTable;
if (i3 > headerArr.length) {
Header[] headerArr2 = new Header[headerArr.length * 2];
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;
}
void setHeaderTableSizeSetting(int i) {
this.headerTableSizeSetting = i;
int min = Math.min(i, SETTINGS_HEADER_TABLE_SIZE_LIMIT);
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();
}
void writeByteString(ByteString byteString) throws IOException {
if (!this.useCompression || Huffman.get().encodedLength(byteString) >= byteString.size()) {
writeInt(byteString.size(), Hpack.PREFIX_7_BITS, 0);
this.out.write(byteString);
return;
}
Buffer buffer = new Buffer();
Huffman.get().encode(byteString, buffer);
ByteString readByteString = buffer.readByteString();
writeInt(readByteString.size(), Hpack.PREFIX_7_BITS, PeripheralType.SERVO);
this.out.write(readByteString);
}
void writeHeaders(List<Header> list) throws IOException {
int i;
int i2;
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);
ByteString asciiLowercase = header.name.toAsciiLowercase();
ByteString byteString = header.value;
Integer num = Hpack.NAME_TO_FIRST_INDEX.get(asciiLowercase);
if (num != null) {
i = num.intValue() + 1;
if (i > 1 && i < 8) {
if (Util.equal(Hpack.STATIC_HEADER_TABLE[i - 1].value, byteString)) {
i2 = i;
} else if (Util.equal(Hpack.STATIC_HEADER_TABLE[i].value, byteString)) {
i2 = i;
i++;
}
}
i2 = i;
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;
}
if (Util.equal(this.dynamicTable[i5].name, asciiLowercase)) {
if (Util.equal(this.dynamicTable[i5].value, byteString)) {
i = Hpack.STATIC_HEADER_TABLE.length + (i5 - this.nextHeaderIndex);
break;
} else if (i2 == -1) {
i2 = (i5 - this.nextHeaderIndex) + Hpack.STATIC_HEADER_TABLE.length;
}
}
i5++;
}
}
if (i != -1) {
writeInt(i, Hpack.PREFIX_7_BITS, PeripheralType.SERVO);
} else if (i2 == -1) {
this.out.writeByte(64);
writeByteString(asciiLowercase);
writeByteString(byteString);
insertIntoDynamicTable(header);
} else if (!asciiLowercase.startsWith(Header.PSEUDO_PREFIX) || Header.TARGET_AUTHORITY.equals(asciiLowercase)) {
writeInt(i2, 63, 64);
writeByteString(byteString);
insertIntoDynamicTable(header);
} else {
writeInt(i2, 15, 0);
writeByteString(byteString);
}
}
}
void writeInt(int i, int i2, int i3) {
if (i < i2) {
this.out.writeByte(i | i3);
return;
}
this.out.writeByte(i3 | i2);
int i4 = i - i2;
while (i4 >= 128) {
this.out.writeByte(128 | (i4 & Hpack.PREFIX_7_BITS));
i4 >>>= 7;
}
this.out.writeByte(i4);
}
Writer(int i, boolean z, Buffer buffer) {
this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
this.dynamicTable = new Header[8];
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
this.headerTableSizeSetting = i;
this.maxDynamicTableByteCount = i;
this.useCompression = z;
this.out = buffer;
}
}
private Hpack() {
}
static ByteString checkLowercase(ByteString byteString) throws IOException {
int size = byteString.size();
for (int i = 0; i < size; i++) {
byte b = byteString.getByte(i);
if (b >= 65 && b <= 90) {
throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + byteString.utf8());
}
}
return byteString;
}
private static Map<ByteString, Integer> nameToFirstIndex() {
LinkedHashMap linkedHashMap = new LinkedHashMap(STATIC_HEADER_TABLE.length);
int i = 0;
while (true) {
Header[] headerArr = STATIC_HEADER_TABLE;
if (i >= headerArr.length) {
return Collections.unmodifiableMap(linkedHashMap);
}
if (!linkedHashMap.containsKey(headerArr[i].name)) {
linkedHashMap.put(STATIC_HEADER_TABLE[i].name, Integer.valueOf(i));
}
i++;
}
}
}