author | prappo |
Fri, 06 Apr 2018 15:20:43 +0100 | |
branch | http-client-branch |
changeset 56389 | 0ba90c4f1e3f |
parent 56320 | f82729ca8660 |
child 56437 | f8b3f053cfbb |
permissions | -rw-r--r-- |
56263 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
||
26 |
package jdk.internal.net.http.websocket; |
|
27 |
||
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
28 |
import jdk.internal.net.http.common.Utils; |
56263 | 29 |
import jdk.internal.net.http.websocket.Frame.Opcode; |
30 |
||
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.CharBuffer; |
|
33 |
import java.nio.charset.CharacterCodingException; |
|
34 |
||
35 |
import static java.lang.String.format; |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
36 |
import java.lang.System.Logger.Level; |
56263 | 37 |
import static java.nio.charset.StandardCharsets.UTF_8; |
38 |
import static java.util.Objects.requireNonNull; |
|
39 |
import static jdk.internal.net.http.common.Utils.dump; |
|
40 |
import static jdk.internal.net.http.websocket.StatusCodes.NO_STATUS_CODE; |
|
41 |
import static jdk.internal.net.http.websocket.StatusCodes.isLegalToReceiveFromServer; |
|
42 |
||
43 |
/* |
|
44 |
* Consumes frame parts and notifies a message consumer, when there is |
|
45 |
* sufficient data to produce a message, or part thereof. |
|
46 |
* |
|
47 |
* Data consumed but not yet translated is accumulated until it's sufficient to |
|
48 |
* form a message. |
|
49 |
*/ |
|
56290 | 50 |
/* Exposed for testing purposes */ |
56263 | 51 |
class MessageDecoder implements Frame.Consumer { |
52 |
||
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
53 |
private static final boolean DEBUG = Utils.DEBUG_WS; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
54 |
private static final System.Logger debug = |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
55 |
Utils.getWebSocketLogger("[Input]"::toString, DEBUG); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
56 |
|
56263 | 57 |
private final MessageStreamConsumer output; |
58 |
private final UTF8AccumulatingDecoder decoder = new UTF8AccumulatingDecoder(); |
|
59 |
private boolean fin; |
|
60 |
private Opcode opcode, originatingOpcode; |
|
61 |
private long payloadLen; |
|
62 |
private long unconsumedPayloadLen; |
|
63 |
private ByteBuffer binaryData; |
|
64 |
||
65 |
MessageDecoder(MessageStreamConsumer output) { |
|
66 |
this.output = requireNonNull(output); |
|
67 |
} |
|
68 |
||
56290 | 69 |
/* Exposed for testing purposes */ |
56263 | 70 |
MessageStreamConsumer getOutput() { |
71 |
return output; |
|
72 |
} |
|
73 |
||
74 |
@Override |
|
75 |
public void fin(boolean value) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
76 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
77 |
debug.log(Level.DEBUG, "fin %s", value); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
78 |
} |
56263 | 79 |
fin = value; |
80 |
} |
|
81 |
||
82 |
@Override |
|
83 |
public void rsv1(boolean value) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
84 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
85 |
debug.log(Level.DEBUG, "rsv1 %s", value); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
86 |
} |
56263 | 87 |
if (value) { |
88 |
throw new FailWebSocketException("Unexpected rsv1 bit"); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public void rsv2(boolean value) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
94 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
95 |
debug.log(Level.DEBUG, "rsv2 %s", value); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
96 |
} |
56263 | 97 |
if (value) { |
98 |
throw new FailWebSocketException("Unexpected rsv2 bit"); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public void rsv3(boolean value) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
104 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
105 |
debug.log(Level.DEBUG, "rsv3 %s", value); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
106 |
} |
56263 | 107 |
if (value) { |
108 |
throw new FailWebSocketException("Unexpected rsv3 bit"); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
@Override |
|
113 |
public void opcode(Opcode v) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
114 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
115 |
debug.log(Level.DEBUG, "opcode %s", v); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
116 |
} |
56263 | 117 |
if (v == Opcode.PING || v == Opcode.PONG || v == Opcode.CLOSE) { |
118 |
if (!fin) { |
|
119 |
throw new FailWebSocketException("Fragmented control frame " + v); |
|
120 |
} |
|
121 |
opcode = v; |
|
122 |
} else if (v == Opcode.TEXT || v == Opcode.BINARY) { |
|
123 |
if (originatingOpcode != null) { |
|
124 |
throw new FailWebSocketException( |
|
125 |
format("Unexpected frame %s (fin=%s)", v, fin)); |
|
126 |
} |
|
127 |
opcode = v; |
|
128 |
if (!fin) { |
|
129 |
originatingOpcode = v; |
|
130 |
} |
|
131 |
} else if (v == Opcode.CONTINUATION) { |
|
132 |
if (originatingOpcode == null) { |
|
133 |
throw new FailWebSocketException( |
|
134 |
format("Unexpected frame %s (fin=%s)", v, fin)); |
|
135 |
} |
|
136 |
opcode = v; |
|
137 |
} else { |
|
138 |
throw new FailWebSocketException("Unexpected opcode " + v); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public void mask(boolean value) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
144 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
145 |
debug.log(Level.DEBUG, "mask %s", value); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
146 |
} |
56263 | 147 |
if (value) { |
148 |
throw new FailWebSocketException("Masked frame received"); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
@Override |
|
153 |
public void payloadLen(long value) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
154 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
155 |
debug.log(Level.DEBUG, "payloadLen %s", value); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
156 |
} |
56263 | 157 |
if (opcode.isControl()) { |
158 |
if (value > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) { |
|
159 |
throw new FailWebSocketException( |
|
160 |
format("%s's payload length %s", opcode, value)); |
|
161 |
} |
|
162 |
assert Opcode.CLOSE.isControl(); |
|
163 |
if (opcode == Opcode.CLOSE && value == 1) { |
|
164 |
throw new FailWebSocketException("Incomplete status code"); |
|
165 |
} |
|
166 |
} |
|
167 |
payloadLen = value; |
|
168 |
unconsumedPayloadLen = value; |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
public void maskingKey(int value) { |
|
173 |
// `MessageDecoder.mask(boolean)` is where a masked frame is detected and |
|
174 |
// reported on; `MessageDecoder.mask(boolean)` MUST be invoked before |
|
175 |
// this method; |
|
176 |
// So this method (`maskingKey`) is not supposed to be invoked while |
|
177 |
// reading a frame that has came from the server. If this method is |
|
178 |
// invoked, then it's an error in implementation, thus InternalError |
|
179 |
throw new InternalError(); |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
183 |
public void payloadData(ByteBuffer data) { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
184 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
185 |
debug.log(Level.DEBUG, "payload %s", data); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
186 |
} |
56263 | 187 |
unconsumedPayloadLen -= data.remaining(); |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
188 |
boolean lastPayloadChunk = unconsumedPayloadLen == 0; |
56263 | 189 |
if (opcode.isControl()) { |
190 |
if (binaryData != null) { // An intermediate or the last chunk |
|
191 |
binaryData.put(data); |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
192 |
} else if (!lastPayloadChunk) { // The first chunk |
56263 | 193 |
int remaining = data.remaining(); |
194 |
// It shouldn't be 125, otherwise the next chunk will be of size |
|
195 |
// 0, which is not what Reader promises to deliver (eager |
|
196 |
// reading) |
|
197 |
assert remaining < Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH |
|
198 |
: dump(remaining); |
|
199 |
binaryData = ByteBuffer.allocate( |
|
200 |
Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH).put(data); |
|
201 |
} else { // The only chunk |
|
202 |
binaryData = ByteBuffer.allocate(data.remaining()).put(data); |
|
203 |
} |
|
204 |
} else { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
205 |
boolean last = fin && lastPayloadChunk; |
56263 | 206 |
boolean text = opcode == Opcode.TEXT || originatingOpcode == Opcode.TEXT; |
207 |
if (!text) { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
208 |
output.onBinary(data.slice(), last); |
56263 | 209 |
data.position(data.limit()); // Consume |
210 |
} else { |
|
211 |
boolean binaryNonEmpty = data.hasRemaining(); |
|
212 |
CharBuffer textData; |
|
213 |
try { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
214 |
textData = decoder.decode(data, last); |
56263 | 215 |
} catch (CharacterCodingException e) { |
216 |
throw new FailWebSocketException( |
|
217 |
"Invalid UTF-8 in frame " + opcode, |
|
218 |
StatusCodes.NOT_CONSISTENT).initCause(e); |
|
219 |
} |
|
220 |
if (!(binaryNonEmpty && !textData.hasRemaining())) { |
|
221 |
// If there's a binary data, that result in no text, then we |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
222 |
// don't deliver anything, otherwise: |
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
223 |
output.onText(textData, last); |
56263 | 224 |
} |
225 |
} |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
230 |
public void endFrame() { |
|
56389
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
231 |
if (debug.isLoggable(Level.DEBUG)) { |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
232 |
debug.log(Level.DEBUG, "end frame"); |
0ba90c4f1e3f
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents:
56320
diff
changeset
|
233 |
} |
56263 | 234 |
if (opcode.isControl()) { |
235 |
binaryData.flip(); |
|
236 |
} |
|
237 |
switch (opcode) { |
|
238 |
case CLOSE: |
|
239 |
char statusCode = NO_STATUS_CODE; |
|
240 |
String reason = ""; |
|
241 |
if (payloadLen != 0) { |
|
242 |
int len = binaryData.remaining(); |
|
243 |
assert 2 <= len |
|
244 |
&& len <= Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH |
|
245 |
: dump(len, payloadLen); |
|
246 |
statusCode = binaryData.getChar(); |
|
247 |
if (!isLegalToReceiveFromServer(statusCode)) { |
|
248 |
throw new FailWebSocketException( |
|
249 |
"Illegal status code: " + statusCode); |
|
250 |
} |
|
251 |
try { |
|
252 |
reason = UTF_8.newDecoder().decode(binaryData).toString(); |
|
253 |
} catch (CharacterCodingException e) { |
|
254 |
throw new FailWebSocketException("Illegal close reason") |
|
255 |
.initCause(e); |
|
256 |
} |
|
257 |
} |
|
258 |
output.onClose(statusCode, reason); |
|
259 |
break; |
|
260 |
case PING: |
|
261 |
output.onPing(binaryData); |
|
262 |
binaryData = null; |
|
263 |
break; |
|
264 |
case PONG: |
|
265 |
output.onPong(binaryData); |
|
266 |
binaryData = null; |
|
267 |
break; |
|
268 |
default: |
|
269 |
assert opcode == Opcode.TEXT || opcode == Opcode.BINARY |
|
270 |
|| opcode == Opcode.CONTINUATION : dump(opcode); |
|
271 |
if (fin) { |
|
272 |
// It is always the last chunk: |
|
273 |
// either TEXT(FIN=TRUE)/BINARY(FIN=TRUE) or CONT(FIN=TRUE) |
|
274 |
originatingOpcode = null; |
|
275 |
} |
|
276 |
break; |
|
277 |
} |
|
278 |
payloadLen = 0; |
|
279 |
opcode = null; |
|
280 |
} |
|
281 |
} |