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