Hpack.java
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package com.squareup.okhttp.internal.framed;
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 okio.Buffer;
import okio.BufferedSource;
import okio.ByteString;
import okio.Okio;
import okio.Source;
final class Hpack
{
private static final Map<ByteString, Integer> NAME_TO_FIRST_INDEX = nameToFirstIndex();
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 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("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("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", "") };
private static ByteString checkLowercase(ByteString paramByteString)
throws IOException
{
int i = 0;
int j = paramByteString.size();
while (i < j)
{
int k = paramByteString.getByte(i);
if ((k >= 65) && (k <= 90)) {
throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + paramByteString.utf8());
}
i += 1;
}
return paramByteString;
}
private static Map<ByteString, Integer> nameToFirstIndex()
{
LinkedHashMap localLinkedHashMap = new LinkedHashMap(STATIC_HEADER_TABLE.length);
int i = 0;
while (i < STATIC_HEADER_TABLE.length)
{
if (!localLinkedHashMap.containsKey(STATIC_HEADER_TABLE[i].name)) {
localLinkedHashMap.put(STATIC_HEADER_TABLE[i].name, Integer.valueOf(i));
}
i += 1;
}
return Collections.unmodifiableMap(localLinkedHashMap);
}
static final class Reader
{
Header[] dynamicTable = new Header[8];
int dynamicTableByteCount = 0;
int headerCount = 0;
private final List<Header> headerList = new ArrayList();
private int headerTableSizeSetting;
private int maxDynamicTableByteCount;
int nextHeaderIndex = this.dynamicTable.length - 1;
private final BufferedSource source;
Reader(int paramInt, Source paramSource)
{
this.headerTableSizeSetting = paramInt;
this.maxDynamicTableByteCount = paramInt;
this.source = Okio.buffer(paramSource);
}
private void adjustDynamicTableByteCount()
{
if (this.maxDynamicTableByteCount < this.dynamicTableByteCount)
{
if (this.maxDynamicTableByteCount == 0) {
clearDynamicTable();
}
}
else {
return;
}
evictToRecoverBytes(this.dynamicTableByteCount - this.maxDynamicTableByteCount);
}
private void clearDynamicTable()
{
this.headerList.clear();
Arrays.fill(this.dynamicTable, null);
this.nextHeaderIndex = (this.dynamicTable.length - 1);
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
private int dynamicTableIndex(int paramInt)
{
return this.nextHeaderIndex + 1 + paramInt;
}
private int evictToRecoverBytes(int paramInt)
{
int i = 0;
int k = 0;
if (paramInt > 0)
{
i = this.dynamicTable.length - 1;
int j = paramInt;
paramInt = k;
while ((i >= this.nextHeaderIndex) && (j > 0))
{
j -= this.dynamicTable[i].hpackSize;
this.dynamicTableByteCount -= this.dynamicTable[i].hpackSize;
this.headerCount -= 1;
paramInt += 1;
i -= 1;
}
System.arraycopy(this.dynamicTable, this.nextHeaderIndex + 1, this.dynamicTable, this.nextHeaderIndex + 1 + paramInt, this.headerCount);
this.nextHeaderIndex += paramInt;
i = paramInt;
}
return i;
}
private ByteString getName(int paramInt)
{
if (isStaticHeader(paramInt)) {
return Hpack.STATIC_HEADER_TABLE[paramInt].name;
}
return this.dynamicTable[dynamicTableIndex(paramInt - Hpack.STATIC_HEADER_TABLE.length)].name;
}
private void insertIntoDynamicTable(int paramInt, Header paramHeader)
{
this.headerList.add(paramHeader);
int j = paramHeader.hpackSize;
int i = j;
if (paramInt != -1) {
i = j - this.dynamicTable[dynamicTableIndex(paramInt)].hpackSize;
}
if (i > this.maxDynamicTableByteCount)
{
clearDynamicTable();
return;
}
j = evictToRecoverBytes(this.dynamicTableByteCount + i - this.maxDynamicTableByteCount);
if (paramInt == -1)
{
if (this.headerCount + 1 > this.dynamicTable.length)
{
Header[] arrayOfHeader = new Header[this.dynamicTable.length * 2];
System.arraycopy(this.dynamicTable, 0, arrayOfHeader, this.dynamicTable.length, this.dynamicTable.length);
this.nextHeaderIndex = (this.dynamicTable.length - 1);
this.dynamicTable = arrayOfHeader;
}
paramInt = this.nextHeaderIndex;
this.nextHeaderIndex = (paramInt - 1);
this.dynamicTable[paramInt] = paramHeader;
this.headerCount += 1;
}
for (;;)
{
this.dynamicTableByteCount = (i + this.dynamicTableByteCount);
return;
int k = dynamicTableIndex(paramInt);
this.dynamicTable[(j + k + paramInt)] = paramHeader;
}
}
private boolean isStaticHeader(int paramInt)
{
return (paramInt >= 0) && (paramInt <= Hpack.STATIC_HEADER_TABLE.length - 1);
}
private int readByte()
throws IOException
{
return this.source.readByte() & 0xFF;
}
private void readIndexedHeader(int paramInt)
throws IOException
{
if (isStaticHeader(paramInt))
{
Header localHeader = Hpack.STATIC_HEADER_TABLE[paramInt];
this.headerList.add(localHeader);
return;
}
int i = dynamicTableIndex(paramInt - Hpack.STATIC_HEADER_TABLE.length);
if ((i < 0) || (i > this.dynamicTable.length - 1)) {
throw new IOException("Header index too large " + (paramInt + 1));
}
this.headerList.add(this.dynamicTable[i]);
}
private void readLiteralHeaderWithIncrementalIndexingIndexedName(int paramInt)
throws IOException
{
insertIntoDynamicTable(-1, new Header(getName(paramInt), readByteString()));
}
private void readLiteralHeaderWithIncrementalIndexingNewName()
throws IOException
{
insertIntoDynamicTable(-1, new Header(Hpack.checkLowercase(readByteString()), readByteString()));
}
private void readLiteralHeaderWithoutIndexingIndexedName(int paramInt)
throws IOException
{
ByteString localByteString1 = getName(paramInt);
ByteString localByteString2 = readByteString();
this.headerList.add(new Header(localByteString1, localByteString2));
}
private void readLiteralHeaderWithoutIndexingNewName()
throws IOException
{
ByteString localByteString1 = Hpack.checkLowercase(readByteString());
ByteString localByteString2 = readByteString();
this.headerList.add(new Header(localByteString1, localByteString2));
}
public final List<Header> getAndResetHeaderList()
{
ArrayList localArrayList = new ArrayList(this.headerList);
this.headerList.clear();
return localArrayList;
}
final void headerTableSizeSetting(int paramInt)
{
this.headerTableSizeSetting = paramInt;
this.maxDynamicTableByteCount = paramInt;
adjustDynamicTableByteCount();
}
final int maxDynamicTableByteCount()
{
return this.maxDynamicTableByteCount;
}
final ByteString readByteString()
throws IOException
{
int j = readByte();
if ((j & 0x80) == 128) {}
for (int i = 1;; i = 0)
{
j = readInt(j, 127);
if (i == 0) {
break;
}
return ByteString.of(Huffman.get().decode(this.source.readByteArray(j)));
}
return this.source.readByteString(j);
}
final void readHeaders()
throws IOException
{
while (!this.source.exhausted())
{
int i = this.source.readByte() & 0xFF;
if (i == 128) {
throw new IOException("index == 0");
}
if ((i & 0x80) == 128)
{
readIndexedHeader(readInt(i, 127) - 1);
}
else if (i == 64)
{
readLiteralHeaderWithIncrementalIndexingNewName();
}
else if ((i & 0x40) == 64)
{
readLiteralHeaderWithIncrementalIndexingIndexedName(readInt(i, 63) - 1);
}
else if ((i & 0x20) == 32)
{
this.maxDynamicTableByteCount = readInt(i, 31);
if ((this.maxDynamicTableByteCount < 0) || (this.maxDynamicTableByteCount > this.headerTableSizeSetting)) {
throw new IOException("Invalid dynamic table size update " + this.maxDynamicTableByteCount);
}
adjustDynamicTableByteCount();
}
else if ((i == 16) || (i == 0))
{
readLiteralHeaderWithoutIndexingNewName();
}
else
{
readLiteralHeaderWithoutIndexingIndexedName(readInt(i, 15) - 1);
}
}
}
final int readInt(int paramInt1, int paramInt2)
throws IOException
{
paramInt1 &= paramInt2;
if (paramInt1 < paramInt2) {
return paramInt1;
}
paramInt1 = 0;
int i;
for (;;)
{
i = readByte();
if ((i & 0x80) == 0) {
break;
}
paramInt2 += ((i & 0x7F) << paramInt1);
paramInt1 += 7;
}
return (i << paramInt1) + paramInt2;
}
}
static final class Writer
{
private final Buffer out;
Writer(Buffer paramBuffer)
{
this.out = paramBuffer;
}
final void writeByteString(ByteString paramByteString)
throws IOException
{
writeInt(paramByteString.size(), 127, 0);
this.out.write(paramByteString);
}
final void writeHeaders(List<Header> paramList)
throws IOException
{
int j = paramList.size();
int i = 0;
if (i < j)
{
ByteString localByteString = ((Header)paramList.get(i)).name.toAsciiLowercase();
Integer localInteger = (Integer)Hpack.NAME_TO_FIRST_INDEX.get(localByteString);
if (localInteger != null)
{
writeInt(localInteger.intValue() + 1, 15, 0);
writeByteString(((Header)paramList.get(i)).value);
}
for (;;)
{
i += 1;
break;
this.out.writeByte(0);
writeByteString(localByteString);
writeByteString(((Header)paramList.get(i)).value);
}
}
}
final void writeInt(int paramInt1, int paramInt2, int paramInt3)
throws IOException
{
if (paramInt1 < paramInt2)
{
this.out.writeByte(paramInt3 | paramInt1);
return;
}
this.out.writeByte(paramInt3 | paramInt2);
paramInt1 -= paramInt2;
while (paramInt1 >= 128)
{
this.out.writeByte(paramInt1 & 0x7F | 0x80);
paramInt1 >>>= 7;
}
this.out.writeByte(paramInt1);
}
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/com/squareup/okhttp/internal/framed/Hpack.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/