49765
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package jdk.internal.net.http.hpack;
|
|
26 |
|
|
27 |
import jdk.internal.net.http.hpack.HPACK.Logger;
|
|
28 |
|
50681
|
29 |
import java.util.List;
|
49765
|
30 |
import java.util.NoSuchElementException;
|
|
31 |
|
49944
|
32 |
import static jdk.internal.net.http.common.Utils.pow2Size;
|
49765
|
33 |
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA;
|
|
34 |
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NORMAL;
|
|
35 |
import static java.lang.String.format;
|
|
36 |
|
|
37 |
/*
|
|
38 |
* A header table consists of two tables, the static table and the dynamic
|
|
39 |
* table. Following the vocabulary of RFC 7541, the length of the header table
|
|
40 |
* is the total number of entries in both the static and the dynamic tables.
|
|
41 |
* The size of the table is the sum of the sizes of its dynamic table's entries.
|
|
42 |
*/
|
|
43 |
class SimpleHeaderTable {
|
|
44 |
|
50681
|
45 |
/* An immutable list of static header fields */
|
|
46 |
protected static final List<HeaderField> staticTable = List.of(
|
|
47 |
new HeaderField(""), // A dummy to make the list index 1-based, instead of 0-based
|
49765
|
48 |
new HeaderField(":authority"),
|
|
49 |
new HeaderField(":method", "GET"),
|
|
50 |
new HeaderField(":method", "POST"),
|
|
51 |
new HeaderField(":path", "/"),
|
|
52 |
new HeaderField(":path", "/index.html"),
|
|
53 |
new HeaderField(":scheme", "http"),
|
|
54 |
new HeaderField(":scheme", "https"),
|
|
55 |
new HeaderField(":status", "200"),
|
|
56 |
new HeaderField(":status", "204"),
|
|
57 |
new HeaderField(":status", "206"),
|
|
58 |
new HeaderField(":status", "304"),
|
|
59 |
new HeaderField(":status", "400"),
|
|
60 |
new HeaderField(":status", "404"),
|
|
61 |
new HeaderField(":status", "500"),
|
|
62 |
new HeaderField("accept-charset"),
|
|
63 |
new HeaderField("accept-encoding", "gzip, deflate"),
|
|
64 |
new HeaderField("accept-language"),
|
|
65 |
new HeaderField("accept-ranges"),
|
|
66 |
new HeaderField("accept"),
|
|
67 |
new HeaderField("access-control-allow-origin"),
|
|
68 |
new HeaderField("age"),
|
|
69 |
new HeaderField("allow"),
|
|
70 |
new HeaderField("authorization"),
|
|
71 |
new HeaderField("cache-control"),
|
|
72 |
new HeaderField("content-disposition"),
|
|
73 |
new HeaderField("content-encoding"),
|
|
74 |
new HeaderField("content-language"),
|
|
75 |
new HeaderField("content-length"),
|
|
76 |
new HeaderField("content-location"),
|
|
77 |
new HeaderField("content-range"),
|
|
78 |
new HeaderField("content-type"),
|
|
79 |
new HeaderField("cookie"),
|
|
80 |
new HeaderField("date"),
|
|
81 |
new HeaderField("etag"),
|
|
82 |
new HeaderField("expect"),
|
|
83 |
new HeaderField("expires"),
|
|
84 |
new HeaderField("from"),
|
|
85 |
new HeaderField("host"),
|
|
86 |
new HeaderField("if-match"),
|
|
87 |
new HeaderField("if-modified-since"),
|
|
88 |
new HeaderField("if-none-match"),
|
|
89 |
new HeaderField("if-range"),
|
|
90 |
new HeaderField("if-unmodified-since"),
|
|
91 |
new HeaderField("last-modified"),
|
|
92 |
new HeaderField("link"),
|
|
93 |
new HeaderField("location"),
|
|
94 |
new HeaderField("max-forwards"),
|
|
95 |
new HeaderField("proxy-authenticate"),
|
|
96 |
new HeaderField("proxy-authorization"),
|
|
97 |
new HeaderField("range"),
|
|
98 |
new HeaderField("referer"),
|
|
99 |
new HeaderField("refresh"),
|
|
100 |
new HeaderField("retry-after"),
|
|
101 |
new HeaderField("server"),
|
|
102 |
new HeaderField("set-cookie"),
|
|
103 |
new HeaderField("strict-transport-security"),
|
|
104 |
new HeaderField("transfer-encoding"),
|
|
105 |
new HeaderField("user-agent"),
|
|
106 |
new HeaderField("vary"),
|
|
107 |
new HeaderField("via"),
|
50681
|
108 |
new HeaderField("www-authenticate"));
|
49765
|
109 |
|
50681
|
110 |
protected static final int STATIC_TABLE_LENGTH = staticTable.size() - 1;
|
49765
|
111 |
protected static final int ENTRY_SIZE = 32;
|
|
112 |
|
|
113 |
private final Logger logger;
|
|
114 |
|
|
115 |
private int maxSize;
|
|
116 |
private int size;
|
|
117 |
|
|
118 |
public SimpleHeaderTable(int maxSize, Logger logger) {
|
|
119 |
this.logger = logger;
|
|
120 |
setMaxSize(maxSize);
|
|
121 |
}
|
|
122 |
|
|
123 |
public int size() {
|
|
124 |
return size;
|
|
125 |
}
|
|
126 |
|
|
127 |
public int maxSize() {
|
|
128 |
return maxSize;
|
|
129 |
}
|
|
130 |
|
|
131 |
public int length() {
|
|
132 |
return STATIC_TABLE_LENGTH + buffer.size;
|
|
133 |
}
|
|
134 |
|
|
135 |
HeaderField get(int index) {
|
|
136 |
checkIndex(index);
|
|
137 |
if (index <= STATIC_TABLE_LENGTH) {
|
50681
|
138 |
return staticTable.get(index);
|
49765
|
139 |
} else {
|
|
140 |
return buffer.get(index - STATIC_TABLE_LENGTH - 1);
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
void put(CharSequence name, CharSequence value) {
|
|
145 |
// Invoking toString() will possibly allocate Strings. But that's
|
|
146 |
// unavoidable at this stage. If a CharSequence is going to be stored in
|
|
147 |
// the table, it must not be mutable (e.g. for the sake of hashing).
|
|
148 |
put(new HeaderField(name.toString(), value.toString()));
|
|
149 |
}
|
|
150 |
|
|
151 |
private void put(HeaderField h) {
|
|
152 |
if (logger.isLoggable(NORMAL)) {
|
|
153 |
logger.log(NORMAL, () -> format("adding ('%s', '%s')",
|
|
154 |
h.name, h.value));
|
|
155 |
}
|
|
156 |
int entrySize = sizeOf(h);
|
|
157 |
if (logger.isLoggable(EXTRA)) {
|
|
158 |
logger.log(EXTRA, () -> format("size of ('%s', '%s') is %s",
|
|
159 |
h.name, h.value, entrySize));
|
|
160 |
}
|
|
161 |
while (entrySize > maxSize - size && size != 0) {
|
|
162 |
if (logger.isLoggable(EXTRA)) {
|
|
163 |
logger.log(EXTRA, () -> format("insufficient space %s, must evict entry",
|
|
164 |
(maxSize - size)));
|
|
165 |
}
|
|
166 |
evictEntry();
|
|
167 |
}
|
|
168 |
if (entrySize > maxSize - size) {
|
|
169 |
if (logger.isLoggable(EXTRA)) {
|
|
170 |
logger.log(EXTRA, () -> format("not adding ('%s, '%s'), too big",
|
|
171 |
h.name, h.value));
|
|
172 |
}
|
|
173 |
return;
|
|
174 |
}
|
|
175 |
size += entrySize;
|
|
176 |
add(h);
|
|
177 |
if (logger.isLoggable(EXTRA)) {
|
|
178 |
logger.log(EXTRA, () -> format("('%s, '%s') added", h.name, h.value));
|
|
179 |
logger.log(EXTRA, this::toString);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
void setMaxSize(int maxSize) {
|
|
184 |
if (maxSize < 0) {
|
|
185 |
throw new IllegalArgumentException(
|
|
186 |
"maxSize >= 0: maxSize=" + maxSize);
|
|
187 |
}
|
|
188 |
while (maxSize < size && size != 0) {
|
|
189 |
evictEntry();
|
|
190 |
}
|
|
191 |
this.maxSize = maxSize;
|
|
192 |
// A header table cannot accommodate more entries than this
|
|
193 |
int upperBound = maxSize / ENTRY_SIZE;
|
|
194 |
buffer.resize(upperBound);
|
|
195 |
}
|
|
196 |
|
|
197 |
HeaderField evictEntry() {
|
|
198 |
HeaderField f = remove();
|
|
199 |
int s = sizeOf(f);
|
|
200 |
this.size -= s;
|
|
201 |
if (logger.isLoggable(EXTRA)) {
|
|
202 |
logger.log(EXTRA, () -> format("evicted entry ('%s', '%s') of size %s",
|
|
203 |
f.name, f.value, s));
|
|
204 |
logger.log(EXTRA, this::toString);
|
|
205 |
}
|
|
206 |
return f;
|
|
207 |
}
|
|
208 |
|
|
209 |
@Override
|
|
210 |
public String toString() {
|
|
211 |
double used = maxSize == 0 ? 0 : 100 * (((double) size) / maxSize);
|
|
212 |
return format("dynamic length: %d, full length: %s, used space: %s/%s (%.1f%%)",
|
|
213 |
buffer.size, length(), size, maxSize, used);
|
|
214 |
}
|
|
215 |
|
|
216 |
private int checkIndex(int index) {
|
|
217 |
int len = length();
|
|
218 |
if (index < 1 || index > len) {
|
|
219 |
throw new IndexOutOfBoundsException(
|
|
220 |
format("1 <= index <= length(): index=%s, length()=%s",
|
|
221 |
index, len));
|
|
222 |
}
|
|
223 |
return index;
|
|
224 |
}
|
|
225 |
|
|
226 |
int sizeOf(HeaderField f) {
|
|
227 |
return f.name.length() + f.value.length() + ENTRY_SIZE;
|
|
228 |
}
|
|
229 |
|
|
230 |
//
|
|
231 |
// Diagnostic information in the form used in the RFC 7541
|
|
232 |
//
|
|
233 |
String getStateString() {
|
|
234 |
if (size == 0) {
|
|
235 |
return "empty.";
|
|
236 |
}
|
|
237 |
|
|
238 |
StringBuilder b = new StringBuilder();
|
|
239 |
for (int i = 1, size = buffer.size; i <= size; i++) {
|
|
240 |
HeaderField e = buffer.get(i - 1);
|
|
241 |
b.append(format("[%3d] (s = %3d) %s: %s\n", i,
|
|
242 |
sizeOf(e), e.name, e.value));
|
|
243 |
}
|
|
244 |
b.append(format(" Table size:%4s", this.size));
|
|
245 |
return b.toString();
|
|
246 |
}
|
|
247 |
|
|
248 |
// Convert to a Value Object (JDK-8046159)?
|
|
249 |
protected static final class HeaderField {
|
|
250 |
|
|
251 |
final String name;
|
|
252 |
final String value;
|
|
253 |
|
|
254 |
public HeaderField(String name) {
|
|
255 |
this(name, "");
|
|
256 |
}
|
|
257 |
|
|
258 |
public HeaderField(String name, String value) {
|
|
259 |
this.name = name;
|
|
260 |
this.value = value;
|
|
261 |
}
|
|
262 |
|
|
263 |
@Override
|
|
264 |
public String toString() {
|
|
265 |
return value.isEmpty() ? name : name + ": " + value;
|
|
266 |
}
|
|
267 |
}
|
|
268 |
|
|
269 |
private final CircularBuffer<HeaderField> buffer = new CircularBuffer<>(0);
|
|
270 |
|
|
271 |
protected void add(HeaderField f) {
|
|
272 |
buffer.add(f);
|
|
273 |
}
|
|
274 |
|
|
275 |
protected HeaderField remove() {
|
|
276 |
return buffer.remove();
|
|
277 |
}
|
|
278 |
|
|
279 |
// head
|
|
280 |
// v
|
|
281 |
// [ ][ ][A][B][C][D][ ][ ][ ]
|
|
282 |
// ^
|
|
283 |
// tail
|
|
284 |
//
|
|
285 |
// |<- size ->| (4)
|
|
286 |
// |<------ capacity ------->| (9)
|
|
287 |
//
|
|
288 |
static final class CircularBuffer<E> {
|
|
289 |
|
|
290 |
int tail, head, size, capacity;
|
|
291 |
Object[] elements;
|
|
292 |
|
|
293 |
CircularBuffer(int capacity) {
|
49944
|
294 |
this.capacity = pow2Size(capacity);
|
|
295 |
elements = new Object[this.capacity];
|
49765
|
296 |
}
|
|
297 |
|
|
298 |
void add(E elem) {
|
|
299 |
if (size == capacity) {
|
|
300 |
throw new IllegalStateException(
|
|
301 |
format("No room for '%s': capacity=%s", elem, capacity));
|
|
302 |
}
|
|
303 |
elements[head] = elem;
|
49944
|
304 |
head = (head + 1) & (capacity - 1);
|
49765
|
305 |
size++;
|
|
306 |
}
|
|
307 |
|
|
308 |
@SuppressWarnings("unchecked")
|
|
309 |
E remove() {
|
|
310 |
if (size == 0) {
|
|
311 |
throw new NoSuchElementException("Empty");
|
|
312 |
}
|
|
313 |
E elem = (E) elements[tail];
|
|
314 |
elements[tail] = null;
|
49944
|
315 |
tail = (tail + 1) & (capacity - 1);
|
49765
|
316 |
size--;
|
|
317 |
return elem;
|
|
318 |
}
|
|
319 |
|
|
320 |
@SuppressWarnings("unchecked")
|
|
321 |
E get(int index) {
|
|
322 |
if (index < 0 || index >= size) {
|
|
323 |
throw new IndexOutOfBoundsException(
|
|
324 |
format("0 <= index <= capacity: index=%s, capacity=%s",
|
|
325 |
index, capacity));
|
|
326 |
}
|
49944
|
327 |
int idx = (tail + (size - index - 1)) & (capacity - 1);
|
49765
|
328 |
return (E) elements[idx];
|
|
329 |
}
|
|
330 |
|
|
331 |
public void resize(int newCapacity) {
|
|
332 |
if (newCapacity < size) {
|
|
333 |
throw new IllegalStateException(
|
|
334 |
format("newCapacity >= size: newCapacity=%s, size=%s",
|
|
335 |
newCapacity, size));
|
|
336 |
}
|
|
337 |
|
49944
|
338 |
int capacity = pow2Size(newCapacity);
|
|
339 |
Object[] newElements = new Object[capacity];
|
49765
|
340 |
|
|
341 |
if (tail < head || size == 0) {
|
|
342 |
System.arraycopy(elements, tail, newElements, 0, size);
|
|
343 |
} else {
|
|
344 |
System.arraycopy(elements, tail, newElements, 0, elements.length - tail);
|
|
345 |
System.arraycopy(elements, 0, newElements, elements.length - tail, head);
|
|
346 |
}
|
|
347 |
|
|
348 |
elements = newElements;
|
|
349 |
tail = 0;
|
|
350 |
head = size;
|
49944
|
351 |
this.capacity = capacity;
|
49765
|
352 |
}
|
|
353 |
}
|
|
354 |
}
|