author | prappo |
Mon, 19 Mar 2018 14:20:18 +0000 | |
branch | http-client-branch |
changeset 56320 | f82729ca8660 |
parent 56304 | 065641767a75 |
child 56389 | 0ba90c4f1e3f |
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) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
76 |
debug.log(Level.DEBUG, "fin %s", value); |
56263 | 77 |
fin = value; |
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
public void rsv1(boolean value) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
82 |
debug.log(Level.DEBUG, "rsv1 %s", value); |
56263 | 83 |
if (value) { |
84 |
throw new FailWebSocketException("Unexpected rsv1 bit"); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
89 |
public void rsv2(boolean value) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
90 |
debug.log(Level.DEBUG, "rsv2 %s", value); |
56263 | 91 |
if (value) { |
92 |
throw new FailWebSocketException("Unexpected rsv2 bit"); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
@Override |
|
97 |
public void rsv3(boolean value) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
98 |
debug.log(Level.DEBUG, "rsv3 %s", value); |
56263 | 99 |
if (value) { |
100 |
throw new FailWebSocketException("Unexpected rsv3 bit"); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
@Override |
|
105 |
public void opcode(Opcode v) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
106 |
debug.log(Level.DEBUG, "opcode %s", v); |
56263 | 107 |
if (v == Opcode.PING || v == Opcode.PONG || v == Opcode.CLOSE) { |
108 |
if (!fin) { |
|
109 |
throw new FailWebSocketException("Fragmented control frame " + v); |
|
110 |
} |
|
111 |
opcode = v; |
|
112 |
} else if (v == Opcode.TEXT || v == Opcode.BINARY) { |
|
113 |
if (originatingOpcode != null) { |
|
114 |
throw new FailWebSocketException( |
|
115 |
format("Unexpected frame %s (fin=%s)", v, fin)); |
|
116 |
} |
|
117 |
opcode = v; |
|
118 |
if (!fin) { |
|
119 |
originatingOpcode = v; |
|
120 |
} |
|
121 |
} else if (v == Opcode.CONTINUATION) { |
|
122 |
if (originatingOpcode == null) { |
|
123 |
throw new FailWebSocketException( |
|
124 |
format("Unexpected frame %s (fin=%s)", v, fin)); |
|
125 |
} |
|
126 |
opcode = v; |
|
127 |
} else { |
|
128 |
throw new FailWebSocketException("Unexpected opcode " + v); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
@Override |
|
133 |
public void mask(boolean value) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
134 |
debug.log(Level.DEBUG, "mask %s", value); |
56263 | 135 |
if (value) { |
136 |
throw new FailWebSocketException("Masked frame received"); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public void payloadLen(long value) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
142 |
debug.log(Level.DEBUG, "payloadLen %s", value); |
56263 | 143 |
if (opcode.isControl()) { |
144 |
if (value > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) { |
|
145 |
throw new FailWebSocketException( |
|
146 |
format("%s's payload length %s", opcode, value)); |
|
147 |
} |
|
148 |
assert Opcode.CLOSE.isControl(); |
|
149 |
if (opcode == Opcode.CLOSE && value == 1) { |
|
150 |
throw new FailWebSocketException("Incomplete status code"); |
|
151 |
} |
|
152 |
} |
|
153 |
payloadLen = value; |
|
154 |
unconsumedPayloadLen = value; |
|
155 |
} |
|
156 |
||
157 |
@Override |
|
158 |
public void maskingKey(int value) { |
|
159 |
// `MessageDecoder.mask(boolean)` is where a masked frame is detected and |
|
160 |
// reported on; `MessageDecoder.mask(boolean)` MUST be invoked before |
|
161 |
// this method; |
|
162 |
// So this method (`maskingKey`) is not supposed to be invoked while |
|
163 |
// reading a frame that has came from the server. If this method is |
|
164 |
// invoked, then it's an error in implementation, thus InternalError |
|
165 |
throw new InternalError(); |
|
166 |
} |
|
167 |
||
168 |
@Override |
|
169 |
public void payloadData(ByteBuffer data) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
170 |
debug.log(Level.DEBUG, "payload %s", data); |
56263 | 171 |
unconsumedPayloadLen -= data.remaining(); |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
172 |
boolean lastPayloadChunk = unconsumedPayloadLen == 0; |
56263 | 173 |
if (opcode.isControl()) { |
174 |
if (binaryData != null) { // An intermediate or the last chunk |
|
175 |
binaryData.put(data); |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
176 |
} else if (!lastPayloadChunk) { // The first chunk |
56263 | 177 |
int remaining = data.remaining(); |
178 |
// It shouldn't be 125, otherwise the next chunk will be of size |
|
179 |
// 0, which is not what Reader promises to deliver (eager |
|
180 |
// reading) |
|
181 |
assert remaining < Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH |
|
182 |
: dump(remaining); |
|
183 |
binaryData = ByteBuffer.allocate( |
|
184 |
Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH).put(data); |
|
185 |
} else { // The only chunk |
|
186 |
binaryData = ByteBuffer.allocate(data.remaining()).put(data); |
|
187 |
} |
|
188 |
} else { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
189 |
boolean last = fin && lastPayloadChunk; |
56263 | 190 |
boolean text = opcode == Opcode.TEXT || originatingOpcode == Opcode.TEXT; |
191 |
if (!text) { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
192 |
output.onBinary(data.slice(), last); |
56263 | 193 |
data.position(data.limit()); // Consume |
194 |
} else { |
|
195 |
boolean binaryNonEmpty = data.hasRemaining(); |
|
196 |
CharBuffer textData; |
|
197 |
try { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
198 |
textData = decoder.decode(data, last); |
56263 | 199 |
} catch (CharacterCodingException e) { |
200 |
throw new FailWebSocketException( |
|
201 |
"Invalid UTF-8 in frame " + opcode, |
|
202 |
StatusCodes.NOT_CONSISTENT).initCause(e); |
|
203 |
} |
|
204 |
if (!(binaryNonEmpty && !textData.hasRemaining())) { |
|
205 |
// 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
|
206 |
// don't deliver anything, otherwise: |
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56304
diff
changeset
|
207 |
output.onText(textData, last); |
56263 | 208 |
} |
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
@Override |
|
214 |
public void endFrame() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56295
diff
changeset
|
215 |
debug.log(Level.DEBUG, "end frame"); |
56263 | 216 |
if (opcode.isControl()) { |
217 |
binaryData.flip(); |
|
218 |
} |
|
219 |
switch (opcode) { |
|
220 |
case CLOSE: |
|
221 |
char statusCode = NO_STATUS_CODE; |
|
222 |
String reason = ""; |
|
223 |
if (payloadLen != 0) { |
|
224 |
int len = binaryData.remaining(); |
|
225 |
assert 2 <= len |
|
226 |
&& len <= Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH |
|
227 |
: dump(len, payloadLen); |
|
228 |
statusCode = binaryData.getChar(); |
|
229 |
if (!isLegalToReceiveFromServer(statusCode)) { |
|
230 |
throw new FailWebSocketException( |
|
231 |
"Illegal status code: " + statusCode); |
|
232 |
} |
|
233 |
try { |
|
234 |
reason = UTF_8.newDecoder().decode(binaryData).toString(); |
|
235 |
} catch (CharacterCodingException e) { |
|
236 |
throw new FailWebSocketException("Illegal close reason") |
|
237 |
.initCause(e); |
|
238 |
} |
|
239 |
} |
|
240 |
output.onClose(statusCode, reason); |
|
241 |
break; |
|
242 |
case PING: |
|
243 |
output.onPing(binaryData); |
|
244 |
binaryData = null; |
|
245 |
break; |
|
246 |
case PONG: |
|
247 |
output.onPong(binaryData); |
|
248 |
binaryData = null; |
|
249 |
break; |
|
250 |
default: |
|
251 |
assert opcode == Opcode.TEXT || opcode == Opcode.BINARY |
|
252 |
|| opcode == Opcode.CONTINUATION : dump(opcode); |
|
253 |
if (fin) { |
|
254 |
// It is always the last chunk: |
|
255 |
// either TEXT(FIN=TRUE)/BINARY(FIN=TRUE) or CONT(FIN=TRUE) |
|
256 |
originatingOpcode = null; |
|
257 |
} |
|
258 |
break; |
|
259 |
} |
|
260 |
payloadLen = 0; |
|
261 |
opcode = null; |
|
262 |
} |
|
263 |
} |