author | prappo |
Fri, 09 Mar 2018 19:10:01 +0000 | |
branch | http-client-branch |
changeset 56291 | c8c4c707ff3a |
parent 56269 | 234813fd33bc |
child 56295 | 898dfb226bd0 |
permissions | -rw-r--r-- |
56263 | 1 |
/* |
2 |
* Copyright (c) 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.Utils; |
|
29 |
import jdk.internal.net.http.websocket.Frame.Opcode; |
|
30 |
||
31 |
import java.io.IOException; |
|
32 |
import java.nio.ByteBuffer; |
|
33 |
import java.nio.CharBuffer; |
|
34 |
import java.nio.charset.CharacterCodingException; |
|
35 |
import java.nio.charset.CharsetEncoder; |
|
36 |
import java.nio.charset.CoderResult; |
|
37 |
import java.nio.charset.CodingErrorAction; |
|
38 |
import java.nio.charset.StandardCharsets; |
|
39 |
import java.security.SecureRandom; |
|
40 |
||
41 |
/* |
|
42 |
* A stateful producer of binary representations of WebSocket messages being |
|
43 |
* sent from the client to the server. |
|
44 |
* |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
45 |
* An encoding method is given an original message and a byte buffer to put the |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
46 |
* resulting bytes to. The method is called until it returns true. Then the |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
47 |
* reset method is called. The whole sequence repeats with next message. |
56263 | 48 |
*/ |
49 |
public class MessageEncoder { |
|
50 |
||
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
51 |
private final static boolean DEBUG = true; |
56263 | 52 |
|
53 |
private final SecureRandom maskingKeySource = new SecureRandom(); |
|
54 |
private final Frame.HeaderWriter headerWriter = new Frame.HeaderWriter(); |
|
55 |
private final Frame.Masker payloadMasker = new Frame.Masker(); |
|
56 |
private final CharsetEncoder charsetEncoder |
|
57 |
= StandardCharsets.UTF_8.newEncoder() |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
58 |
.onMalformedInput(CodingErrorAction.REPORT) |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
59 |
.onUnmappableCharacter(CodingErrorAction.REPORT); |
56263 | 60 |
/* |
61 |
* This buffer is used both to encode characters to UTF-8 and to calculate |
|
62 |
* the length of the resulting frame's payload. The length of the payload |
|
63 |
* must be known before the frame's header can be written. |
|
64 |
* For implementation reasons, this buffer must have a capacity of at least |
|
65 |
* the maximum size of a Close frame payload, which is 125 bytes |
|
66 |
* (or Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH). |
|
67 |
*/ |
|
68 |
private final ByteBuffer intermediateBuffer = createIntermediateBuffer( |
|
69 |
Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH); |
|
70 |
private final ByteBuffer headerBuffer = ByteBuffer.allocate( |
|
71 |
Frame.MAX_HEADER_SIZE_BYTES); |
|
72 |
||
73 |
private boolean started; |
|
74 |
private boolean flushing; |
|
75 |
private boolean moreText = true; |
|
76 |
private long headerCount; |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
77 |
/* Has the previous frame got its fin flag set? */ |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
78 |
private boolean previousFin = true; |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
79 |
/* Was the previous frame TEXT or a CONTINUATION thereof? */ |
56263 | 80 |
private boolean previousText; |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
81 |
private boolean closed; // TODO: too late, need to check it before accepting otherwise the queue might blow up |
56263 | 82 |
|
83 |
/* |
|
84 |
* How many bytes of the current message have been already encoded. |
|
85 |
* |
|
86 |
* Even though the user hands their buffers over to us, they still can |
|
87 |
* manipulate these buffers while we are getting data out of them. |
|
88 |
* The number of produced bytes guards us from such behaviour in the |
|
89 |
* case of messages that must be restricted in size (Ping, Pong and Close). |
|
90 |
* For other messages this measure provides a best-effort attempt to detect |
|
91 |
* concurrent changes to buffer. |
|
92 |
* |
|
93 |
* Making a shallow copy (duplicate/slice) and then checking the size |
|
94 |
* precondition on it would also solve the problem, but at the cost of this |
|
95 |
* extra copy. |
|
96 |
*/ |
|
97 |
private int actualLen; |
|
98 |
||
99 |
/* |
|
100 |
* How many bytes were originally there in the message, before the encoding |
|
101 |
* started. |
|
102 |
*/ |
|
103 |
private int expectedLen; |
|
104 |
||
105 |
/* Exposed for testing purposes */ |
|
106 |
protected ByteBuffer createIntermediateBuffer(int minSize) { |
|
107 |
int capacity = Utils.getIntegerNetProperty( |
|
108 |
"jdk.httpclient.websocket.intermediateBufferSize", 16384); |
|
109 |
return ByteBuffer.allocate(Math.max(minSize, capacity)); |
|
110 |
} |
|
111 |
||
112 |
public void reset() { |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
113 |
// Do not reset the message stream state fields, e.g. previousFin, |
56263 | 114 |
// previousText. Just an individual message state: |
115 |
started = false; |
|
116 |
flushing = false; |
|
117 |
moreText = true; |
|
118 |
headerCount = 0; |
|
119 |
actualLen = 0; |
|
120 |
} |
|
121 |
||
122 |
/* |
|
123 |
* Encodes text messages by cutting them into fragments of maximum size of |
|
124 |
* intermediateBuffer.capacity() |
|
125 |
*/ |
|
126 |
public boolean encodeText(CharBuffer src, boolean last, ByteBuffer dst) |
|
127 |
throws IOException |
|
128 |
{ |
|
129 |
if (DEBUG) { |
|
130 |
System.out.printf("[Output] encodeText src.remaining()=%s, %s, %s%n", |
|
131 |
src.remaining(), last, dst); |
|
132 |
} |
|
133 |
if (closed) { |
|
134 |
throw new IOException("Output closed"); |
|
135 |
} |
|
136 |
if (!started) { |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
137 |
if (!previousText && !previousFin) { |
56263 | 138 |
// Previous data message was a partial binary message |
139 |
throw new IllegalStateException("Unexpected text message"); |
|
140 |
} |
|
141 |
started = true; |
|
142 |
headerBuffer.position(0).limit(0); |
|
143 |
intermediateBuffer.position(0).limit(0); |
|
144 |
charsetEncoder.reset(); |
|
145 |
} |
|
146 |
while (true) { |
|
147 |
if (DEBUG) { |
|
148 |
System.out.printf("[Output] put%n"); |
|
149 |
} |
|
150 |
if (!putAvailable(headerBuffer, dst)) { |
|
151 |
return false; |
|
152 |
} |
|
153 |
if (DEBUG) { |
|
154 |
System.out.printf("[Output] mask%n"); |
|
155 |
} |
|
156 |
if (maskAvailable(intermediateBuffer, dst) < 0) { |
|
157 |
return false; |
|
158 |
} |
|
159 |
if (DEBUG) { |
|
160 |
System.out.printf("[Output] moreText%n"); |
|
161 |
} |
|
162 |
if (!moreText) { |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
163 |
previousFin = last; |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
164 |
previousText = true; |
56263 | 165 |
return true; |
166 |
} |
|
167 |
intermediateBuffer.clear(); |
|
168 |
CoderResult r = null; |
|
169 |
if (!flushing) { |
|
170 |
r = charsetEncoder.encode(src, intermediateBuffer, true); |
|
171 |
if (r.isUnderflow()) { |
|
172 |
flushing = true; |
|
173 |
} |
|
174 |
} |
|
175 |
if (flushing) { |
|
176 |
r = charsetEncoder.flush(intermediateBuffer); |
|
177 |
if (r.isUnderflow()) { |
|
178 |
moreText = false; |
|
179 |
} |
|
180 |
} |
|
181 |
if (r.isError()) { |
|
182 |
try { |
|
183 |
r.throwException(); |
|
184 |
} catch (CharacterCodingException e) { |
|
185 |
throw new IOException("Malformed text message", e); |
|
186 |
} |
|
187 |
} |
|
188 |
if (DEBUG) { |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
189 |
System.out.printf("[Output] frame #%s%n", headerCount); |
56263 | 190 |
} |
191 |
intermediateBuffer.flip(); |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
192 |
Opcode opcode = previousFin && headerCount == 0 |
56263 | 193 |
? Opcode.TEXT : Opcode.CONTINUATION; |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
194 |
boolean fin = last && !moreText; |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
195 |
setupHeader(opcode, fin, intermediateBuffer.remaining()); |
56263 | 196 |
headerCount++; |
197 |
} |
|
198 |
} |
|
199 |
||
200 |
private boolean putAvailable(ByteBuffer src, ByteBuffer dst) { |
|
201 |
int available = dst.remaining(); |
|
202 |
if (available >= src.remaining()) { |
|
203 |
dst.put(src); |
|
204 |
return true; |
|
205 |
} else { |
|
206 |
int lim = src.limit(); // save the limit |
|
207 |
src.limit(src.position() + available); |
|
208 |
dst.put(src); |
|
209 |
src.limit(lim); // restore the limit |
|
210 |
return false; |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
public boolean encodeBinary(ByteBuffer src, boolean last, ByteBuffer dst) |
|
215 |
throws IOException |
|
216 |
{ |
|
217 |
if (DEBUG) { |
|
218 |
System.out.printf("[Output] encodeBinary %s, %s, %s%n", |
|
219 |
src, last, dst); |
|
220 |
} |
|
221 |
if (closed) { |
|
222 |
throw new IOException("Output closed"); |
|
223 |
} |
|
224 |
if (!started) { |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
225 |
if (previousText && !previousFin) { |
56263 | 226 |
// Previous data message was a partial text message |
227 |
throw new IllegalStateException("Unexpected binary message"); |
|
228 |
} |
|
229 |
expectedLen = src.remaining(); |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
230 |
Opcode opcode = previousFin ? Opcode.BINARY : Opcode.CONTINUATION; |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
231 |
setupHeader(opcode, last, expectedLen); |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
232 |
previousFin = last; |
56263 | 233 |
previousText = false; |
234 |
started = true; |
|
235 |
} |
|
236 |
if (!putAvailable(headerBuffer, dst)) { |
|
237 |
return false; |
|
238 |
} |
|
239 |
int count = maskAvailable(src, dst); |
|
240 |
actualLen += Math.abs(count); |
|
241 |
if (count >= 0 && actualLen != expectedLen) { |
|
242 |
throw new IOException("Concurrent message modification"); |
|
243 |
} |
|
244 |
return count >= 0; |
|
245 |
} |
|
246 |
||
247 |
private int maskAvailable(ByteBuffer src, ByteBuffer dst) { |
|
248 |
int r0 = dst.remaining(); |
|
249 |
payloadMasker.transferMasking(src, dst); |
|
250 |
int masked = r0 - dst.remaining(); |
|
251 |
return src.hasRemaining() ? -masked : masked; |
|
252 |
} |
|
253 |
||
254 |
public boolean encodePing(ByteBuffer src, ByteBuffer dst) |
|
255 |
throws IOException |
|
256 |
{ |
|
257 |
if (closed) { |
|
258 |
throw new IOException("Output closed"); |
|
259 |
} |
|
260 |
if (DEBUG) System.out.printf("[Output] encodePing %s, %s%n", src, dst); |
|
261 |
if (!started) { |
|
262 |
expectedLen = src.remaining(); |
|
263 |
if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) { |
|
264 |
throw new IllegalArgumentException("Long message: " + expectedLen); |
|
265 |
} |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
266 |
setupHeader(Opcode.PING, true, expectedLen); |
56263 | 267 |
started = true; |
268 |
} |
|
269 |
if (!putAvailable(headerBuffer, dst)) { |
|
270 |
return false; |
|
271 |
} |
|
272 |
int count = maskAvailable(src, dst); |
|
273 |
actualLen += Math.abs(count); |
|
274 |
if (count >= 0 && actualLen != expectedLen) { |
|
275 |
throw new IOException("Concurrent message modification"); |
|
276 |
} |
|
277 |
return count >= 0; |
|
278 |
} |
|
279 |
||
280 |
public boolean encodePong(ByteBuffer src, ByteBuffer dst) |
|
281 |
throws IOException |
|
282 |
{ |
|
283 |
if (closed) { |
|
284 |
throw new IOException("Output closed"); |
|
285 |
} |
|
286 |
if (DEBUG) { |
|
287 |
System.out.printf("[Output] encodePong %s, %s%n", |
|
288 |
src, dst); |
|
289 |
} |
|
290 |
if (!started) { |
|
291 |
expectedLen = src.remaining(); |
|
292 |
if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) { |
|
293 |
throw new IllegalArgumentException("Long message: " + expectedLen); |
|
294 |
} |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
295 |
setupHeader(Opcode.PONG, true, expectedLen); |
56263 | 296 |
started = true; |
297 |
} |
|
298 |
if (!putAvailable(headerBuffer, dst)) { |
|
299 |
return false; |
|
300 |
} |
|
301 |
int count = maskAvailable(src, dst); |
|
302 |
actualLen += Math.abs(count); |
|
303 |
if (count >= 0 && actualLen != expectedLen) { |
|
304 |
throw new IOException("Concurrent message modification"); |
|
305 |
} |
|
306 |
return count >= 0; |
|
307 |
} |
|
308 |
||
309 |
public boolean encodeClose(int statusCode, CharBuffer reason, ByteBuffer dst) |
|
310 |
throws IOException |
|
311 |
{ |
|
312 |
if (DEBUG) { |
|
313 |
System.out.printf("[Output] encodeClose %s, reason.length=%s, %s%n", |
|
314 |
statusCode, reason.length(), dst); |
|
315 |
} |
|
316 |
if (closed) { |
|
317 |
throw new IOException("Output closed"); |
|
318 |
} |
|
319 |
if (!started) { |
|
320 |
if (DEBUG) { |
|
321 |
System.out.printf("[Output] reason size %s%n", reason.remaining()); |
|
322 |
} |
|
323 |
intermediateBuffer.position(0).limit(Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH); |
|
324 |
intermediateBuffer.putChar((char) statusCode); |
|
325 |
CoderResult r = charsetEncoder.reset().encode(reason, intermediateBuffer, true); |
|
326 |
if (r.isUnderflow()) { |
|
327 |
if (DEBUG) { |
|
328 |
System.out.printf("[Output] flushing%n"); |
|
329 |
} |
|
330 |
r = charsetEncoder.flush(intermediateBuffer); |
|
331 |
} |
|
332 |
if (DEBUG) { |
|
333 |
System.out.printf("[Output] encoding result: %s%n", r); |
|
334 |
} |
|
335 |
if (r.isError()) { |
|
336 |
try { |
|
337 |
r.throwException(); |
|
338 |
} catch (CharacterCodingException e) { |
|
339 |
throw new IllegalArgumentException("Malformed reason", e); |
|
340 |
} |
|
341 |
} else if (r.isOverflow()) { |
|
342 |
// Here the 125 bytes size is ensured by the check for overflow |
|
343 |
throw new IllegalArgumentException("Long reason"); |
|
344 |
} else if (!r.isUnderflow()) { |
|
345 |
throw new InternalError(); // assertion |
|
346 |
} |
|
347 |
intermediateBuffer.flip(); |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
348 |
setupHeader(Opcode.CLOSE, true, intermediateBuffer.remaining()); |
56263 | 349 |
started = true; |
350 |
closed = true; |
|
351 |
if (DEBUG) { |
|
352 |
System.out.printf("[Output] intermediateBuffer=%s%n", |
|
353 |
intermediateBuffer); |
|
354 |
} |
|
355 |
} |
|
356 |
if (!putAvailable(headerBuffer, dst)) { |
|
357 |
return false; |
|
358 |
} |
|
359 |
return maskAvailable(intermediateBuffer, dst) >= 0; |
|
360 |
} |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
361 |
|
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
362 |
private void setupHeader(Opcode opcode, boolean fin, long payloadLen) { |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
363 |
if (DEBUG) { |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
364 |
System.out.printf("[Output] frame opcode=%s fin=%s len=%s%n", |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
365 |
opcode, fin, payloadLen); |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
366 |
} |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
367 |
headerBuffer.clear(); |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
368 |
int mask = maskingKeySource.nextInt(); |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
369 |
headerWriter.fin(fin) |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
370 |
.opcode(opcode) |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
371 |
.payloadLen(payloadLen) |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
372 |
.mask(mask) |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
373 |
.write(headerBuffer); |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
374 |
headerBuffer.flip(); |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
375 |
payloadMasker.mask(mask); |
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
376 |
} |
56263 | 377 |
} |