48083
|
1 |
/*
|
49765
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
|
48083
|
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 |
*/
|
49765
|
25 |
package jdk.internal.net.http.hpack;
|
48083
|
26 |
|
49765
|
27 |
import jdk.internal.net.http.common.Utils;
|
|
28 |
import jdk.internal.net.http.hpack.HPACK.Logger.Level;
|
48083
|
29 |
|
50681
|
30 |
import java.nio.ByteBuffer;
|
48083
|
31 |
import java.security.AccessController;
|
|
32 |
import java.security.PrivilegedAction;
|
|
33 |
import java.util.Map;
|
|
34 |
import java.util.ResourceBundle;
|
|
35 |
import java.util.function.Supplier;
|
|
36 |
|
|
37 |
import static java.lang.String.format;
|
|
38 |
import static java.util.stream.Collectors.joining;
|
49765
|
39 |
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.EXTRA;
|
|
40 |
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NONE;
|
|
41 |
import static jdk.internal.net.http.hpack.HPACK.Logger.Level.NORMAL;
|
48083
|
42 |
|
|
43 |
/**
|
|
44 |
* Internal utilities and stuff.
|
|
45 |
*/
|
|
46 |
public final class HPACK {
|
|
47 |
|
|
48 |
private static final RootLogger LOGGER;
|
|
49 |
private static final Map<String, Level> logLevels =
|
|
50 |
Map.of("NORMAL", NORMAL, "EXTRA", EXTRA);
|
|
51 |
|
|
52 |
static {
|
|
53 |
String PROPERTY = "jdk.internal.httpclient.hpack.log.level";
|
|
54 |
|
|
55 |
String value = AccessController.doPrivileged(
|
|
56 |
(PrivilegedAction<String>) () -> System.getProperty(PROPERTY));
|
|
57 |
|
|
58 |
if (value == null) {
|
|
59 |
LOGGER = new RootLogger(NONE);
|
|
60 |
} else {
|
|
61 |
String upperCasedValue = value.toUpperCase();
|
|
62 |
Level l = logLevels.get(upperCasedValue);
|
|
63 |
if (l == null) {
|
|
64 |
LOGGER = new RootLogger(NONE);
|
|
65 |
LOGGER.log(System.Logger.Level.INFO,
|
|
66 |
() -> format("%s value '%s' not recognized (use %s); logging disabled",
|
|
67 |
PROPERTY, value, logLevels.keySet().stream().collect(joining(", "))));
|
|
68 |
} else {
|
|
69 |
LOGGER = new RootLogger(l);
|
|
70 |
LOGGER.log(System.Logger.Level.DEBUG,
|
|
71 |
() -> format("logging level %s", l));
|
|
72 |
}
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
public static Logger getLogger() {
|
|
77 |
return LOGGER;
|
|
78 |
}
|
|
79 |
|
|
80 |
private HPACK() { }
|
|
81 |
|
|
82 |
/**
|
|
83 |
* The purpose of this logger is to provide means of diagnosing issues _in
|
|
84 |
* the HPACK implementation_. It's not a general purpose logger.
|
|
85 |
*/
|
|
86 |
// implements System.Logger to make it possible to skip this class
|
|
87 |
// when looking for the Caller.
|
|
88 |
public static class Logger implements System.Logger {
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Log detail level.
|
|
92 |
*/
|
|
93 |
public enum Level {
|
|
94 |
|
|
95 |
NONE(0, System.Logger.Level.OFF),
|
|
96 |
NORMAL(1, System.Logger.Level.DEBUG),
|
|
97 |
EXTRA(2, System.Logger.Level.TRACE);
|
|
98 |
|
|
99 |
private final int level;
|
|
100 |
final System.Logger.Level systemLevel;
|
|
101 |
|
|
102 |
Level(int i, System.Logger.Level system) {
|
|
103 |
level = i;
|
|
104 |
systemLevel = system;
|
|
105 |
}
|
|
106 |
|
|
107 |
public final boolean implies(Level other) {
|
|
108 |
return this.level >= other.level;
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
private final String name;
|
|
113 |
private final Level level;
|
|
114 |
private final String path;
|
|
115 |
private final System.Logger logger;
|
|
116 |
|
|
117 |
private Logger(String path, String name, Level level) {
|
|
118 |
this(path, name, level, null);
|
|
119 |
}
|
|
120 |
|
|
121 |
private Logger(String p, String name, Level level, System.Logger logger) {
|
|
122 |
this.path = p;
|
|
123 |
this.name = name;
|
|
124 |
this.level = level;
|
|
125 |
this.logger = Utils.getHpackLogger(path::toString, level.systemLevel);
|
|
126 |
}
|
|
127 |
|
|
128 |
public final String getName() {
|
|
129 |
return name;
|
|
130 |
}
|
|
131 |
|
|
132 |
@Override
|
|
133 |
public boolean isLoggable(System.Logger.Level level) {
|
|
134 |
return logger.isLoggable(level);
|
|
135 |
}
|
|
136 |
|
|
137 |
@Override
|
|
138 |
public void log(System.Logger.Level level, ResourceBundle bundle, String msg, Throwable thrown) {
|
|
139 |
logger.log(level, bundle, msg,thrown);
|
|
140 |
}
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public void log(System.Logger.Level level, ResourceBundle bundle, String format, Object... params) {
|
|
144 |
logger.log(level, bundle, format, params);
|
|
145 |
}
|
|
146 |
|
|
147 |
/*
|
|
148 |
* Usual performance trick for logging, reducing performance overhead in
|
|
149 |
* the case where logging with the specified level is a NOP.
|
|
150 |
*/
|
|
151 |
|
|
152 |
public boolean isLoggable(Level level) {
|
|
153 |
return this.level.implies(level);
|
|
154 |
}
|
|
155 |
|
|
156 |
public void log(Level level, Supplier<String> s) {
|
|
157 |
if (this.level.implies(level)) {
|
|
158 |
logger.log(level.systemLevel, s);
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
public Logger subLogger(String name) {
|
|
163 |
return new Logger(path + "/" + name, name, level);
|
|
164 |
}
|
|
165 |
|
|
166 |
}
|
|
167 |
|
|
168 |
private static final class RootLogger extends Logger {
|
|
169 |
|
|
170 |
protected RootLogger(Level level) {
|
|
171 |
super("hpack", "hpack", level);
|
|
172 |
}
|
|
173 |
|
|
174 |
}
|
50681
|
175 |
|
|
176 |
// -- low-level utilities --
|
|
177 |
|
|
178 |
@FunctionalInterface
|
|
179 |
interface BufferUpdateConsumer {
|
|
180 |
void accept(long data, int len);
|
|
181 |
}
|
|
182 |
|
|
183 |
@SuppressWarnings("fallthrough")
|
|
184 |
public static int read(ByteBuffer source,
|
|
185 |
long buffer,
|
|
186 |
int bufferLen,
|
|
187 |
BufferUpdateConsumer consumer)
|
|
188 |
{
|
|
189 |
// read as much as possible (up to 8 bytes)
|
|
190 |
int nBytes = Math.min((64 - bufferLen) >> 3, source.remaining());
|
|
191 |
switch (nBytes) {
|
|
192 |
case 0:
|
|
193 |
break;
|
|
194 |
case 3:
|
|
195 |
buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
|
|
196 |
bufferLen += 8;
|
|
197 |
case 2:
|
|
198 |
buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
|
|
199 |
bufferLen += 8;
|
|
200 |
case 1:
|
|
201 |
buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
|
|
202 |
bufferLen += 8;
|
|
203 |
consumer.accept(buffer, bufferLen);
|
|
204 |
break;
|
|
205 |
case 7:
|
|
206 |
buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
|
|
207 |
bufferLen += 8;
|
|
208 |
case 6:
|
|
209 |
buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
|
|
210 |
bufferLen += 8;
|
|
211 |
case 5:
|
|
212 |
buffer |= ((source.get() & 0x00000000000000ffL) << (56 - bufferLen));
|
|
213 |
bufferLen += 8;
|
|
214 |
case 4:
|
|
215 |
buffer |= ((source.getInt() & 0x00000000ffffffffL) << (32 - bufferLen));
|
|
216 |
bufferLen += 32;
|
|
217 |
consumer.accept(buffer, bufferLen);
|
|
218 |
break;
|
|
219 |
case 8:
|
|
220 |
buffer = source.getLong();
|
|
221 |
bufferLen = 64;
|
|
222 |
consumer.accept(buffer, bufferLen);
|
|
223 |
break;
|
|
224 |
default:
|
|
225 |
throw new InternalError(String.valueOf(nBytes));
|
|
226 |
}
|
|
227 |
return nBytes;
|
|
228 |
}
|
|
229 |
|
|
230 |
// The number of bytes that can be written at once
|
|
231 |
// (calculating in bytes, not bits, since
|
|
232 |
// destination.remaining() * 8 might overflow)
|
|
233 |
@SuppressWarnings("fallthrough")
|
|
234 |
public static int write(long buffer,
|
|
235 |
int bufferLen,
|
|
236 |
BufferUpdateConsumer consumer,
|
|
237 |
ByteBuffer destination)
|
|
238 |
{
|
|
239 |
int nBytes = Math.min(bufferLen >> 3, destination.remaining());
|
|
240 |
switch (nBytes) {
|
|
241 |
case 0:
|
|
242 |
break;
|
|
243 |
case 3:
|
|
244 |
destination.put((byte) (buffer >>> 56));
|
|
245 |
buffer <<= 8;
|
|
246 |
bufferLen -= 8;
|
|
247 |
case 2:
|
|
248 |
destination.put((byte) (buffer >>> 56));
|
|
249 |
buffer <<= 8;
|
|
250 |
bufferLen -= 8;
|
|
251 |
case 1:
|
|
252 |
destination.put((byte) (buffer >>> 56));
|
|
253 |
buffer <<= 8;
|
|
254 |
bufferLen -= 8;
|
|
255 |
consumer.accept(buffer, bufferLen);
|
|
256 |
break;
|
|
257 |
case 7:
|
|
258 |
destination.put((byte) (buffer >>> 56));
|
|
259 |
buffer <<= 8;
|
|
260 |
bufferLen -= 8;
|
|
261 |
case 6:
|
|
262 |
destination.put((byte) (buffer >>> 56));
|
|
263 |
buffer <<= 8;
|
|
264 |
bufferLen -= 8;
|
|
265 |
case 5:
|
|
266 |
destination.put((byte) (buffer >>> 56));
|
|
267 |
buffer <<= 8;
|
|
268 |
bufferLen -= 8;
|
|
269 |
case 4:
|
|
270 |
destination.putInt((int) (buffer >>> 32));
|
|
271 |
buffer <<= 32;
|
|
272 |
bufferLen -= 32;
|
|
273 |
consumer.accept(buffer, bufferLen);
|
|
274 |
break;
|
|
275 |
case 8:
|
|
276 |
destination.putLong(buffer);
|
|
277 |
buffer = 0;
|
|
278 |
bufferLen = 0;
|
|
279 |
consumer.accept(buffer, bufferLen);
|
|
280 |
break;
|
|
281 |
default:
|
|
282 |
throw new InternalError(String.valueOf(nBytes));
|
|
283 |
}
|
|
284 |
return nBytes;
|
|
285 |
}
|
|
286 |
|
|
287 |
/*
|
|
288 |
* Returns the number of bytes the given number of bits constitute.
|
|
289 |
*/
|
|
290 |
static int bytesForBits(int n) {
|
|
291 |
assert (n / 8 + (n % 8 != 0 ? 1 : 0)) == (n + 7) / 8
|
|
292 |
&& (n + 7) / 8 == ((n + 7) >> 3) : n;
|
|
293 |
return (n + 7) >> 3;
|
|
294 |
}
|
48083
|
295 |
}
|