37874
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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 java.net.http;
|
|
26 |
|
|
27 |
import java.net.http.WSFrame.Opcode;
|
|
28 |
import java.net.http.WebSocket.MessagePart;
|
|
29 |
import java.nio.ByteBuffer;
|
|
30 |
import java.nio.CharBuffer;
|
|
31 |
import java.nio.charset.CharacterCodingException;
|
|
32 |
import java.util.concurrent.atomic.AtomicInteger;
|
|
33 |
|
|
34 |
import static java.lang.String.format;
|
|
35 |
import static java.lang.System.Logger.Level.TRACE;
|
|
36 |
import static java.net.http.WSUtils.dump;
|
|
37 |
import static java.net.http.WSUtils.logger;
|
|
38 |
import static java.net.http.WebSocket.CloseCode.NOT_CONSISTENT;
|
|
39 |
import static java.net.http.WebSocket.CloseCode.of;
|
|
40 |
import static java.util.Objects.requireNonNull;
|
|
41 |
|
|
42 |
/*
|
|
43 |
* Consumes frame parts and notifies a message consumer, when there is
|
|
44 |
* sufficient data to produce a message, or part thereof.
|
|
45 |
*
|
|
46 |
* Data consumed but not yet translated is accumulated until it's sufficient to
|
|
47 |
* form a message.
|
|
48 |
*/
|
|
49 |
final class WSFrameConsumer implements WSFrame.Consumer {
|
|
50 |
|
|
51 |
private final AtomicInteger invocationOrder = new AtomicInteger();
|
|
52 |
|
|
53 |
private final WSMessageConsumer output;
|
|
54 |
private final WSCharsetToolkit.Decoder decoder = new WSCharsetToolkit.Decoder();
|
|
55 |
private boolean fin;
|
|
56 |
private Opcode opcode, originatingOpcode;
|
|
57 |
private MessagePart part = MessagePart.WHOLE;
|
|
58 |
private long payloadLen;
|
|
59 |
private WSShared<ByteBuffer> binaryData;
|
|
60 |
|
|
61 |
WSFrameConsumer(WSMessageConsumer output) {
|
|
62 |
this.output = requireNonNull(output);
|
|
63 |
}
|
|
64 |
|
|
65 |
@Override
|
|
66 |
public void fin(boolean value) {
|
|
67 |
assert invocationOrder.compareAndSet(0, 1) : dump(invocationOrder, value);
|
|
68 |
if (logger.isLoggable(TRACE)) {
|
|
69 |
// Checked for being loggable because of autoboxing of 'value'
|
|
70 |
logger.log(TRACE, "Reading fin: {0}", value);
|
|
71 |
}
|
|
72 |
fin = value;
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
public void rsv1(boolean value) {
|
|
77 |
assert invocationOrder.compareAndSet(1, 2) : dump(invocationOrder, value);
|
|
78 |
if (logger.isLoggable(TRACE)) {
|
|
79 |
logger.log(TRACE, "Reading rsv1: {0}", value);
|
|
80 |
}
|
|
81 |
if (value) {
|
|
82 |
throw new WSProtocolException("5.2.", "rsv1 bit is set unexpectedly");
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
@Override
|
|
87 |
public void rsv2(boolean value) {
|
|
88 |
assert invocationOrder.compareAndSet(2, 3) : dump(invocationOrder, value);
|
|
89 |
if (logger.isLoggable(TRACE)) {
|
|
90 |
logger.log(TRACE, "Reading rsv2: {0}", value);
|
|
91 |
}
|
|
92 |
if (value) {
|
|
93 |
throw new WSProtocolException("5.2.", "rsv2 bit is set unexpectedly");
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
@Override
|
|
98 |
public void rsv3(boolean value) {
|
|
99 |
assert invocationOrder.compareAndSet(3, 4) : dump(invocationOrder, value);
|
|
100 |
if (logger.isLoggable(TRACE)) {
|
|
101 |
logger.log(TRACE, "Reading rsv3: {0}", value);
|
|
102 |
}
|
|
103 |
if (value) {
|
|
104 |
throw new WSProtocolException("5.2.", "rsv3 bit is set unexpectedly");
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
|
109 |
public void opcode(Opcode v) {
|
|
110 |
assert invocationOrder.compareAndSet(4, 5) : dump(invocationOrder, v);
|
|
111 |
logger.log(TRACE, "Reading opcode: {0}", v);
|
|
112 |
if (v == Opcode.PING || v == Opcode.PONG || v == Opcode.CLOSE) {
|
|
113 |
if (!fin) {
|
|
114 |
throw new WSProtocolException("5.5.", "A fragmented control frame " + v);
|
|
115 |
}
|
|
116 |
opcode = v;
|
|
117 |
} else if (v == Opcode.TEXT || v == Opcode.BINARY) {
|
|
118 |
if (originatingOpcode != null) {
|
|
119 |
throw new WSProtocolException
|
|
120 |
("5.4.", format("An unexpected frame %s (fin=%s)", v, fin));
|
|
121 |
}
|
|
122 |
opcode = v;
|
|
123 |
if (!fin) {
|
|
124 |
originatingOpcode = v;
|
|
125 |
}
|
|
126 |
} else if (v == Opcode.CONTINUATION) {
|
|
127 |
if (originatingOpcode == null) {
|
|
128 |
throw new WSProtocolException
|
|
129 |
("5.4.", format("An unexpected frame %s (fin=%s)", v, fin));
|
|
130 |
}
|
|
131 |
opcode = v;
|
|
132 |
} else {
|
|
133 |
throw new WSProtocolException("5.2.", "An unknown opcode " + v);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
@Override
|
|
138 |
public void mask(boolean value) {
|
|
139 |
assert invocationOrder.compareAndSet(5, 6) : dump(invocationOrder, value);
|
|
140 |
if (logger.isLoggable(TRACE)) {
|
|
141 |
logger.log(TRACE, "Reading mask: {0}", value);
|
|
142 |
}
|
|
143 |
if (value) {
|
|
144 |
throw new WSProtocolException
|
|
145 |
("5.1.", "Received a masked frame from the server");
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
@Override
|
|
150 |
public void payloadLen(long value) {
|
|
151 |
assert invocationOrder.compareAndSet(6, 7) : dump(invocationOrder, value);
|
|
152 |
if (logger.isLoggable(TRACE)) {
|
|
153 |
logger.log(TRACE, "Reading payloadLen: {0}", value);
|
|
154 |
}
|
|
155 |
if (opcode.isControl()) {
|
|
156 |
if (value > 125) {
|
|
157 |
throw new WSProtocolException
|
|
158 |
("5.5.", format("A control frame %s has a payload length of %s",
|
|
159 |
opcode, value));
|
|
160 |
}
|
|
161 |
assert Opcode.CLOSE.isControl();
|
|
162 |
if (opcode == Opcode.CLOSE && value == 1) {
|
|
163 |
throw new WSProtocolException
|
|
164 |
("5.5.1.", "A Close frame's status code is only 1 byte long");
|
|
165 |
}
|
|
166 |
}
|
|
167 |
payloadLen = value;
|
|
168 |
}
|
|
169 |
|
|
170 |
@Override
|
|
171 |
public void maskingKey(int value) {
|
|
172 |
assert false : dump(invocationOrder, value);
|
|
173 |
}
|
|
174 |
|
|
175 |
@Override
|
|
176 |
public void payloadData(WSShared<ByteBuffer> data, boolean isLast) {
|
|
177 |
assert invocationOrder.compareAndSet(7, isLast ? 8 : 7)
|
|
178 |
: dump(invocationOrder, data, isLast);
|
|
179 |
if (logger.isLoggable(TRACE)) {
|
|
180 |
logger.log(TRACE, "Reading payloadData: data={0}, isLast={1}", data, isLast);
|
|
181 |
}
|
|
182 |
if (opcode.isControl()) {
|
|
183 |
if (binaryData != null) {
|
|
184 |
binaryData.put(data);
|
|
185 |
data.dispose();
|
|
186 |
} else if (!isLast) {
|
|
187 |
// The first chunk of the message
|
|
188 |
int remaining = data.remaining();
|
|
189 |
// It shouldn't be 125, otherwise the next chunk will be of size
|
|
190 |
// 0, which is not what Reader promises to deliver (eager
|
|
191 |
// reading)
|
|
192 |
assert remaining < 125 : dump(remaining);
|
|
193 |
WSShared<ByteBuffer> b = WSShared.wrap(ByteBuffer.allocate(125)).put(data);
|
|
194 |
data.dispose();
|
|
195 |
binaryData = b; // Will be disposed by the user
|
|
196 |
} else {
|
|
197 |
// The only chunk; will be disposed by the user
|
|
198 |
binaryData = data.position(data.limit()); // FIXME: remove this hack
|
|
199 |
}
|
|
200 |
} else {
|
|
201 |
part = determinePart(isLast);
|
|
202 |
boolean text = opcode == Opcode.TEXT || originatingOpcode == Opcode.TEXT;
|
|
203 |
if (!text) {
|
|
204 |
output.onBinary(part, data);
|
|
205 |
} else {
|
|
206 |
boolean binaryNonEmpty = data.hasRemaining();
|
|
207 |
WSShared<CharBuffer> textData;
|
|
208 |
try {
|
|
209 |
textData = decoder.decode(data, part.isLast());
|
|
210 |
} catch (CharacterCodingException e) {
|
|
211 |
throw new WSProtocolException
|
|
212 |
("5.6.", "Invalid UTF-8 sequence in frame " + opcode, NOT_CONSISTENT, e);
|
|
213 |
}
|
|
214 |
if (!(binaryNonEmpty && !textData.hasRemaining())) {
|
|
215 |
// If there's a binary data, that result in no text, then we
|
|
216 |
// don't deliver anything
|
|
217 |
output.onText(part, new WSDisposableText(textData));
|
|
218 |
}
|
|
219 |
}
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
@Override
|
|
224 |
public void endFrame() {
|
|
225 |
assert invocationOrder.compareAndSet(8, 0) : dump(invocationOrder);
|
|
226 |
if (opcode.isControl()) {
|
|
227 |
binaryData.flip();
|
|
228 |
}
|
|
229 |
switch (opcode) {
|
|
230 |
case CLOSE:
|
|
231 |
WebSocket.CloseCode cc;
|
|
232 |
String reason;
|
|
233 |
if (payloadLen == 0) {
|
|
234 |
cc = null;
|
|
235 |
reason = "";
|
|
236 |
} else {
|
|
237 |
ByteBuffer b = binaryData.buffer();
|
|
238 |
int len = b.remaining();
|
|
239 |
assert 2 <= len && len <= 125 : dump(len, payloadLen);
|
|
240 |
try {
|
|
241 |
cc = of(b.getChar());
|
|
242 |
reason = WSCharsetToolkit.decode(b).toString();
|
|
243 |
} catch (IllegalArgumentException e) {
|
|
244 |
throw new WSProtocolException
|
|
245 |
("5.5.1", "Incorrect status code", e);
|
|
246 |
} catch (CharacterCodingException e) {
|
|
247 |
throw new WSProtocolException
|
|
248 |
("5.5.1", "Close reason is a malformed UTF-8 sequence", e);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
binaryData.dispose(); // Manual dispose
|
|
252 |
output.onClose(cc, reason);
|
|
253 |
break;
|
|
254 |
case PING:
|
|
255 |
output.onPing(binaryData);
|
|
256 |
binaryData = null;
|
|
257 |
break;
|
|
258 |
case PONG:
|
|
259 |
output.onPong(binaryData);
|
|
260 |
binaryData = null;
|
|
261 |
break;
|
|
262 |
default:
|
|
263 |
assert opcode == Opcode.TEXT || opcode == Opcode.BINARY
|
|
264 |
|| opcode == Opcode.CONTINUATION : dump(opcode);
|
|
265 |
if (fin) {
|
|
266 |
// It is always the last chunk:
|
|
267 |
// either TEXT(FIN=TRUE)/BINARY(FIN=TRUE) or CONT(FIN=TRUE)
|
|
268 |
originatingOpcode = null;
|
|
269 |
}
|
|
270 |
break;
|
|
271 |
}
|
|
272 |
payloadLen = 0;
|
|
273 |
opcode = null;
|
|
274 |
}
|
|
275 |
|
|
276 |
private MessagePart determinePart(boolean isLast) {
|
|
277 |
boolean lastChunk = fin && isLast;
|
|
278 |
switch (part) {
|
|
279 |
case LAST:
|
|
280 |
case WHOLE:
|
|
281 |
return lastChunk ? MessagePart.WHOLE : MessagePart.FIRST;
|
|
282 |
case FIRST:
|
|
283 |
case PART:
|
|
284 |
return lastChunk ? MessagePart.LAST : MessagePart.PART;
|
|
285 |
default:
|
|
286 |
throw new InternalError(String.valueOf(part));
|
|
287 |
}
|
|
288 |
}
|
|
289 |
}
|