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