-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Draft_6455.java
805 lines (744 loc) · 28.7 KB
/
Draft_6455.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
* Copyright (c) 2010-2018 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package org.java_websocket.drafts;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.enums.*;
import org.java_websocket.exceptions.*;
import org.java_websocket.extensions.*;
import org.java_websocket.framing.*;
import org.java_websocket.handshake.*;
import org.java_websocket.protocols.IProtocol;
import org.java_websocket.protocols.Protocol;
import org.java_websocket.util.*;
import org.java_websocket.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Implementation for the RFC 6455 websocket protocol
* This is the recommended class for your websocket connection
*/
public class Draft_6455 extends Draft {
/**
* Logger instance
*
* @since 1.4.0
*/
private static final Logger log = LoggerFactory.getLogger(Draft_6455.class);
/**
* Attribute for the used extension in this draft
*/
private IExtension extension = new DefaultExtension();
/**
* Attribute for all available extension in this draft
*/
private List<IExtension> knownExtensions;
/**
* Attribute for the used protocol in this draft
*/
private IProtocol protocol;
/**
* Attribute for all available protocols in this draft
*/
private List<IProtocol> knownProtocols;
/**
* Attribute for the current continuous frame
*/
private Framedata current_continuous_frame;
/**
* Attribute for the payload of the current continuous frame
*/
private List<ByteBuffer> byteBufferList;
/**
* Attribute for the current incomplete frame
*/
private ByteBuffer incompleteframe;
/**
* Attribute for the reusable random instance
*/
private final Random reuseableRandom = new Random();
/**
* Constructor for the websocket protocol specified by RFC 6455 with default extensions
* @since 1.3.5
*/
public Draft_6455() {
this( Collections.<IExtension>emptyList() );
}
/**
* Constructor for the websocket protocol specified by RFC 6455 with custom extensions
*
* @param inputExtension the extension which should be used for this draft
* @since 1.3.5
*/
public Draft_6455( IExtension inputExtension ) {
this( Collections.singletonList( inputExtension ) );
}
/**
* Constructor for the websocket protocol specified by RFC 6455 with custom extensions
*
* @param inputExtensions the extensions which should be used for this draft
* @since 1.3.5
*/
public Draft_6455( List<IExtension> inputExtensions ) {
this( inputExtensions, Collections.<IProtocol>singletonList( new Protocol( "" ) ));
}
/**
* Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
*
* @param inputExtensions the extensions which should be used for this draft
* @param inputProtocols the protocols which should be used for this draft
*
* @since 1.3.7
*/
public Draft_6455( List<IExtension> inputExtensions , List<IProtocol> inputProtocols ) {
if (inputExtensions == null || inputProtocols == null) {
throw new IllegalArgumentException();
}
knownExtensions = new ArrayList<IExtension>( inputExtensions.size());
knownProtocols = new ArrayList<IProtocol>( inputProtocols.size());
boolean hasDefault = false;
byteBufferList = new ArrayList<ByteBuffer>();
for( IExtension inputExtension : inputExtensions ) {
if( inputExtension.getClass().equals( DefaultExtension.class ) ) {
hasDefault = true;
}
}
knownExtensions.addAll( inputExtensions );
//We always add the DefaultExtension to implement the normal RFC 6455 specification
if( !hasDefault ) {
knownExtensions.add( this.knownExtensions.size(), extension );
}
knownProtocols.addAll( inputProtocols );
}
@Override
public HandshakeState acceptHandshakeAsServer( ClientHandshake handshakedata ) throws InvalidHandshakeException {
int v = readVersion( handshakedata );
if( v != 13 ) {
log.trace("acceptHandshakeAsServer - Wrong websocket version.");
return HandshakeState.NOT_MATCHED;
}
HandshakeState extensionState = HandshakeState.NOT_MATCHED;
String requestedExtension = handshakedata.getFieldValue( "Sec-WebSocket-Extensions" );
for( IExtension knownExtension : knownExtensions ) {
if( knownExtension.acceptProvidedExtensionAsServer( requestedExtension ) ) {
extension = knownExtension;
extensionState = HandshakeState.MATCHED;
log.trace("acceptHandshakeAsServer - Matching extension found: {}", extension);
break;
}
}
HandshakeState protocolState = HandshakeState.NOT_MATCHED;
String requestedProtocol = handshakedata.getFieldValue( "Sec-WebSocket-Protocol" );
for( IProtocol knownProtocol : knownProtocols ) {
if( knownProtocol.acceptProvidedProtocol( requestedProtocol ) ) {
protocol = knownProtocol;
protocolState = HandshakeState.MATCHED;
log.trace("acceptHandshakeAsServer - Matching protocol found: {}", protocol);
break;
}
}
if (protocolState == HandshakeState.MATCHED && extensionState == HandshakeState.MATCHED) {
return HandshakeState.MATCHED;
}
log.trace("acceptHandshakeAsServer - No matching extension or protocol found.");
return HandshakeState.NOT_MATCHED;
}
@Override
public HandshakeState acceptHandshakeAsClient( ClientHandshake request, ServerHandshake response ) throws InvalidHandshakeException {
if (! basicAccept( response )) {
log.trace("acceptHandshakeAsClient - Missing/wrong upgrade or connection in handshake.");
return HandshakeState.NOT_MATCHED;
}
if( !request.hasFieldValue( "Sec-WebSocket-Key" ) || !response.hasFieldValue( "Sec-WebSocket-Accept" ) ) {
log.trace("acceptHandshakeAsClient - Missing Sec-WebSocket-Key or Sec-WebSocket-Accept");
return HandshakeState.NOT_MATCHED;
}
String seckey_answere = response.getFieldValue( "Sec-WebSocket-Accept" );
String seckey_challenge = request.getFieldValue( "Sec-WebSocket-Key" );
seckey_challenge = generateFinalKey( seckey_challenge );
if( !seckey_challenge.equals( seckey_answere ) ) {
log.trace("acceptHandshakeAsClient - Wrong key for Sec-WebSocket-Key.");
return HandshakeState.NOT_MATCHED;
}
HandshakeState extensionState= HandshakeState.NOT_MATCHED;
String requestedExtension = response.getFieldValue( "Sec-WebSocket-Extensions" );
for( IExtension knownExtension : knownExtensions ) {
if( knownExtension.acceptProvidedExtensionAsClient( requestedExtension ) ) {
extension = knownExtension;
extensionState = HandshakeState.MATCHED;
log.trace("acceptHandshakeAsClient - Matching extension found: {}",extension);
break;
}
}
HandshakeState protocolState = HandshakeState.NOT_MATCHED;
String requestedProtocol = response.getFieldValue( "Sec-WebSocket-Protocol" );
for( IProtocol knownProtocol : knownProtocols ) {
if( knownProtocol.acceptProvidedProtocol( requestedProtocol ) ) {
protocol = knownProtocol;
protocolState = HandshakeState.MATCHED;
log.trace("acceptHandshakeAsClient - Matching protocol found: {}",protocol);
break;
}
}
if (protocolState == HandshakeState.MATCHED && extensionState == HandshakeState.MATCHED) {
return HandshakeState.MATCHED;
}
log.trace("acceptHandshakeAsClient - No matching extension or protocol found.");
return HandshakeState.NOT_MATCHED;
}
/**
* Getter for the extension which is used by this draft
*
* @return the extension which is used or null, if handshake is not yet done
*/
public IExtension getExtension() {
return extension;
}
/**
* Getter for all available extensions for this draft
* @return the extensions which are enabled for this draft
*/
public List<IExtension> getKnownExtensions() {
return knownExtensions;
}
/**
* Getter for the protocol which is used by this draft
*
* @return the protocol which is used or null, if handshake is not yet done or no valid protocols
* @since 1.3.7
*/
public IProtocol getProtocol() {
return protocol;
}
/**
* Getter for all available protocols for this draft
* @return the protocols which are enabled for this draft
* @since 1.3.7
*/
public List<IProtocol> getKnownProtocols() {
return knownProtocols;
}
@Override
public ClientHandshakeBuilder postProcessHandshakeRequestAsClient( ClientHandshakeBuilder request ) {
request.put( "Upgrade", "websocket" );
request.put( "Connection", "Upgrade" ); // to respond to a Connection keep alives
byte[] random = new byte[16];
reuseableRandom.nextBytes( random );
request.put( "Sec-WebSocket-Key", Base64.encodeBytes( random ) );
request.put( "Sec-WebSocket-Version", "13" );// overwriting the previous
StringBuilder requestedExtensions = new StringBuilder();
for( IExtension knownExtension : knownExtensions ) {
if( knownExtension.getProvidedExtensionAsClient() != null && knownExtension.getProvidedExtensionAsClient().length() != 0 ) {
if (requestedExtensions.length() > 0) {
requestedExtensions.append( ", " );
}
requestedExtensions.append( knownExtension.getProvidedExtensionAsClient() );
}
}
if( requestedExtensions.length() != 0 ) {
request.put( "Sec-WebSocket-Extensions", requestedExtensions.toString() );
}
StringBuilder requestedProtocols = new StringBuilder();
for( IProtocol knownProtocol : knownProtocols ) {
if( knownProtocol.getProvidedProtocol().length() != 0 ) {
if (requestedProtocols.length() > 0) {
requestedProtocols.append( ", " );
}
requestedProtocols.append( knownProtocol.getProvidedProtocol() );
}
}
if( requestedProtocols.length() != 0 ) {
request.put( "Sec-WebSocket-Protocol", requestedProtocols.toString() );
}
return request;
}
@Override
public HandshakeBuilder postProcessHandshakeResponseAsServer( ClientHandshake request, ServerHandshakeBuilder response ) throws InvalidHandshakeException {
response.put( "Upgrade", "websocket" );
response.put( "Connection", request.getFieldValue( "Connection" ) ); // to respond to a Connection keep alives
String seckey = request.getFieldValue( "Sec-WebSocket-Key" );
if( seckey == null )
throw new InvalidHandshakeException( "missing Sec-WebSocket-Key" );
response.put( "Sec-WebSocket-Accept", generateFinalKey( seckey ) );
if( getExtension().getProvidedExtensionAsServer().length() != 0 ) {
response.put( "Sec-WebSocket-Extensions", getExtension().getProvidedExtensionAsServer() );
}
if( getProtocol() != null && getProtocol().getProvidedProtocol().length() != 0 ) {
response.put( "Sec-WebSocket-Protocol", getProtocol().getProvidedProtocol() );
}
response.setHttpStatusMessage( "Web Socket Protocol Handshake" );
response.put( "Server", "TooTallNate Java-WebSocket" );
response.put( "Date", getServerTime() );
return response;
}
@Override
public Draft copyInstance() {
ArrayList<IExtension> newExtensions = new ArrayList<IExtension>();
for( IExtension extension : getKnownExtensions() ) {
newExtensions.add( extension.copyInstance() );
}
ArrayList<IProtocol> newProtocols = new ArrayList<IProtocol>();
for( IProtocol protocol : getKnownProtocols() ) {
newProtocols.add( protocol.copyInstance() );
}
return new Draft_6455( newExtensions, newProtocols );
}
@Override
public ByteBuffer createBinaryFrame( Framedata framedata ) {
getExtension().encodeFrame( framedata );
log.trace( "afterEnconding({}): {}" , framedata.getPayloadData().remaining(), ( framedata.getPayloadData().remaining() > 1000 ? "too big to display" : new String( framedata.getPayloadData().array() ) ) );
return createByteBufferFromFramedata( framedata );
}
private ByteBuffer createByteBufferFromFramedata( Framedata framedata ) {
ByteBuffer mes = framedata.getPayloadData();
boolean mask = role == Role.CLIENT; // framedata.getTransfereMasked();
int sizebytes = mes.remaining() <= 125 ? 1 : mes.remaining() <= 65535 ? 2 : 8;
ByteBuffer buf = ByteBuffer.allocate( 1 + ( sizebytes > 1 ? sizebytes + 1 : sizebytes ) + ( mask ? 4 : 0 ) + mes.remaining() );
byte optcode = fromOpcode( framedata.getOpcode() );
byte one = ( byte ) ( framedata.isFin() ? -128 : 0 );
one |= optcode;
buf.put( one );
byte[] payloadlengthbytes = toByteArray( mes.remaining(), sizebytes );
assert ( payloadlengthbytes.length == sizebytes );
if( sizebytes == 1 ) {
buf.put( ( byte ) ( payloadlengthbytes[0] | ( mask ? ( byte ) -128 : 0 ) ) );
} else if( sizebytes == 2 ) {
buf.put( ( byte ) ( ( byte ) 126 | ( mask ? ( byte ) -128 : 0 ) ) );
buf.put( payloadlengthbytes );
} else if( sizebytes == 8 ) {
buf.put( ( byte ) ( ( byte ) 127 | ( mask ? ( byte ) -128 : 0 ) ) );
buf.put( payloadlengthbytes );
} else
throw new RuntimeException( "Size representation not supported/specified" );
if( mask ) {
ByteBuffer maskkey = ByteBuffer.allocate( 4 );
maskkey.putInt( reuseableRandom.nextInt() );
buf.put( maskkey.array() );
for( int i = 0; mes.hasRemaining(); i++ ) {
buf.put( ( byte ) ( mes.get() ^ maskkey.get( i % 4 ) ) );
}
} else {
buf.put( mes );
//Reset the position of the bytebuffer e.g. for additional use
mes.flip();
}
assert ( buf.remaining() == 0 ) : buf.remaining();
buf.flip();
return buf;
}
public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteException, InvalidDataException {
int maxpacketsize = buffer.remaining();
int realpacketsize = 2;
if( maxpacketsize < realpacketsize ) {
log.trace( "Incomplete frame" );
throw new IncompleteException( realpacketsize );
}
byte b1 = buffer.get( /*0*/ );
boolean FIN = b1 >> 8 != 0;
boolean rsv1 = false;
boolean rsv2 = false;
boolean rsv3 = false;
if( ( b1 & 0x40 ) != 0 ) {
rsv1 = true;
}
if( ( b1 & 0x20 ) != 0 ) {
rsv2 = true;
}
if( ( b1 & 0x10 ) != 0 ) {
rsv3 = true;
}
byte b2 = buffer.get( /*1*/ );
boolean MASK = ( b2 & -128 ) != 0;
int payloadlength = ( byte ) ( b2 & ~( byte ) 128 );
Opcode optcode = toOpcode( ( byte ) ( b1 & 15 ) );
if( !( payloadlength >= 0 && payloadlength <= 125 ) ) {
if( optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING ) {
log.trace( "Invalid frame: more than 125 octets" );
throw new InvalidFrameException( "more than 125 octets" );
}
if( payloadlength == 126 ) {
realpacketsize += 2; // additional length bytes
if( maxpacketsize < realpacketsize ) {
log.trace( "Incomplete frame" );
throw new IncompleteException( realpacketsize );
}
byte[] sizebytes = new byte[3];
sizebytes[1] = buffer.get( /*1 + 1*/ );
sizebytes[2] = buffer.get( /*1 + 2*/ );
payloadlength = new BigInteger( sizebytes ).intValue();
} else {
realpacketsize += 8; // additional length bytes
if( maxpacketsize < realpacketsize ) {
log.trace( "Incomplete frame" );
throw new IncompleteException( realpacketsize );
}
byte[] bytes = new byte[8];
for( int i = 0; i < 8; i++ ) {
bytes[i] = buffer.get( /*1 + i*/ );
}
long length = new BigInteger( bytes ).longValue();
if( length > Integer.MAX_VALUE ) {
log.trace( "Limit exedeed: Payloadsize is to big..." );
throw new LimitExedeedException( "Payloadsize is to big..." );
} else {
payloadlength = ( int ) length;
}
}
}
// int maskskeystart = foff + realpacketsize;
realpacketsize += ( MASK ? 4 : 0 );
// int payloadstart = foff + realpacketsize;
realpacketsize += payloadlength;
if( maxpacketsize < realpacketsize )
throw new IncompleteException( realpacketsize );
ByteBuffer payload = ByteBuffer.allocate( checkAlloc( payloadlength ) );
if( MASK ) {
byte[] maskskey = new byte[4];
buffer.get( maskskey );
for( int i = 0; i < payloadlength; i++ ) {
payload.put( ( byte ) ( buffer.get( /*payloadstart + i*/ ) ^ maskskey[i % 4] ) );
}
} else {
payload.put( buffer.array(), buffer.position(), payload.limit() );
buffer.position( buffer.position() + payload.limit() );
}
FramedataImpl1 frame = FramedataImpl1.get( optcode );
frame.setFin( FIN );
frame.setRSV1( rsv1 );
frame.setRSV2( rsv2 );
frame.setRSV3( rsv3 );
payload.flip();
frame.setPayload( payload );
getExtension().isFrameValid(frame);
getExtension().decodeFrame(frame);
log.trace( "afterDecoding({}): {}", frame.getPayloadData().remaining(), ( frame.getPayloadData().remaining() > 1000 ? "too big to display" : new String( frame.getPayloadData().array() ) ) );
frame.isValid();
return frame;
}
@Override
public List<Framedata> translateFrame( ByteBuffer buffer ) throws InvalidDataException {
while( true ) {
List<Framedata> frames = new LinkedList<Framedata>();
Framedata cur;
if( incompleteframe != null ) {
// complete an incomplete frame
try {
buffer.mark();
int available_next_byte_count = buffer.remaining();// The number of bytes received
int expected_next_byte_count = incompleteframe.remaining();// The number of bytes to complete the incomplete frame
if( expected_next_byte_count > available_next_byte_count ) {
// did not receive enough bytes to complete the frame
incompleteframe.put( buffer.array(), buffer.position(), available_next_byte_count );
buffer.position( buffer.position() + available_next_byte_count );
return Collections.emptyList();
}
incompleteframe.put( buffer.array(), buffer.position(), expected_next_byte_count );
buffer.position( buffer.position() + expected_next_byte_count );
cur = translateSingleFrame( ( ByteBuffer ) incompleteframe.duplicate().position( 0 ) );
frames.add( cur );
incompleteframe = null;
} catch ( IncompleteException e ) {
// extending as much as suggested
ByteBuffer extendedframe = ByteBuffer.allocate( checkAlloc( e.getPreferredSize() ) );
assert ( extendedframe.limit() > incompleteframe.limit() );
incompleteframe.rewind();
extendedframe.put( incompleteframe );
incompleteframe = extendedframe;
continue;
}
}
while( buffer.hasRemaining() ) {// Read as much as possible full frames
buffer.mark();
try {
cur = translateSingleFrame( buffer );
frames.add( cur );
} catch ( IncompleteException e ) {
// remember the incomplete data
buffer.reset();
int pref = e.getPreferredSize();
incompleteframe = ByteBuffer.allocate( checkAlloc( pref ) );
incompleteframe.put( buffer );
break;
}
}
return frames;
}
}
@Override
public List<Framedata> createFrames( ByteBuffer binary, boolean mask ) {
BinaryFrame curframe = new BinaryFrame();
curframe.setPayload( binary );
curframe.setTransferemasked( mask );
try {
curframe.isValid();
} catch ( InvalidDataException e ) {
throw new NotSendableException( e );
}
return Collections.singletonList( ( Framedata ) curframe );
}
@Override
public List<Framedata> createFrames( String text, boolean mask ) {
TextFrame curframe = new TextFrame();
curframe.setPayload( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( text ) ) );
curframe.setTransferemasked( mask );
try {
curframe.isValid();
} catch ( InvalidDataException e ) {
throw new NotSendableException( e );
}
return Collections.singletonList( ( Framedata ) curframe );
}
@Override
public void reset() {
incompleteframe = null;
if( extension != null ) {
extension.reset();
}
extension = new DefaultExtension();
protocol = null;
}
/**
* Generate a date for for the date-header
*
* @return the server time
*/
private String getServerTime() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z", Locale.US );
dateFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
return dateFormat.format( calendar.getTime() );
}
/**
* Generate a final key from a input string
* @param in the input string
* @return a final key
*/
private String generateFinalKey( String in ) {
String seckey = in.trim();
String acc = seckey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
MessageDigest sh1;
try {
sh1 = MessageDigest.getInstance( "SHA1" );
} catch ( NoSuchAlgorithmException e ) {
throw new IllegalStateException( e );
}
return Base64.encodeBytes( sh1.digest( acc.getBytes() ) );
}
private byte[] toByteArray( long val, int bytecount ) {
byte[] buffer = new byte[bytecount];
int highest = 8 * bytecount - 8;
for( int i = 0; i < bytecount; i++ ) {
buffer[i] = ( byte ) ( val >>> ( highest - 8 * i ) );
}
return buffer;
}
private byte fromOpcode( Opcode opcode ) {
if( opcode == Opcode.CONTINUOUS )
return 0;
else if( opcode == Opcode.TEXT )
return 1;
else if( opcode == Opcode.BINARY )
return 2;
else if( opcode == Opcode.CLOSING )
return 8;
else if( opcode == Opcode.PING )
return 9;
else if( opcode == Opcode.PONG )
return 10;
throw new IllegalArgumentException( "Don't know how to handle " + opcode.toString() );
}
private Opcode toOpcode( byte opcode ) throws InvalidFrameException {
switch(opcode) {
case 0:
return Opcode.CONTINUOUS;
case 1:
return Opcode.TEXT;
case 2:
return Opcode.BINARY;
// 3-7 are not yet defined
case 8:
return Opcode.CLOSING;
case 9:
return Opcode.PING;
case 10:
return Opcode.PONG;
// 11-15 are not yet defined
default:
throw new InvalidFrameException( "Unknown opcode " + ( short ) opcode );
}
}
@Override
public void processFrame( WebSocketImpl webSocketImpl, Framedata frame ) throws InvalidDataException {
Opcode curop = frame.getOpcode();
if( curop == Opcode.CLOSING ) {
int code = CloseFrame.NOCODE;
String reason = "";
if( frame instanceof CloseFrame ) {
CloseFrame cf = ( CloseFrame ) frame;
code = cf.getCloseCode();
reason = cf.getMessage();
}
if( webSocketImpl.getReadyState() == ReadyState.CLOSING ) {
// complete the close handshake by disconnecting
webSocketImpl.closeConnection( code, reason, true );
} else {
// echo close handshake
if( getCloseHandshakeType() == CloseHandshakeType.TWOWAY )
webSocketImpl.close( code, reason, true );
else
webSocketImpl.flushAndClose( code, reason, false );
}
} else if( curop == Opcode.PING ) {
webSocketImpl.getWebSocketListener().onWebsocketPing( webSocketImpl, frame );
} else if( curop == Opcode.PONG ) {
webSocketImpl.updateLastPong();
webSocketImpl.getWebSocketListener().onWebsocketPong( webSocketImpl, frame );
} else if( !frame.isFin() || curop == Opcode.CONTINUOUS ) {
if( curop != Opcode.CONTINUOUS ) {
if( current_continuous_frame != null ) {
log.trace( "Protocol error: Previous continuous frame sequence not completed." );
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "Previous continuous frame sequence not completed." );
}
current_continuous_frame = frame;
byteBufferList.add( frame.getPayloadData() );
} else if( frame.isFin() ) {
if( current_continuous_frame == null ) {
log.trace( "Protocol error: Previous continuous frame sequence not completed." );
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence was not started." );
}
byteBufferList.add( frame.getPayloadData() );
if( current_continuous_frame.getOpcode() == Opcode.TEXT ) {
((FramedataImpl1) current_continuous_frame).setPayload( getPayloadFromByteBufferList() );
((FramedataImpl1) current_continuous_frame ).isValid();
try {
webSocketImpl.getWebSocketListener().onWebsocketMessage( webSocketImpl, Charsetfunctions.stringUtf8( current_continuous_frame.getPayloadData() ) );
} catch ( RuntimeException e ) {
log.error( "Runtime exception during onWebsocketMessage", e );
webSocketImpl.getWebSocketListener().onWebsocketError( webSocketImpl, e );
}
} else if( current_continuous_frame.getOpcode() == Opcode.BINARY ) {
((FramedataImpl1) current_continuous_frame).setPayload( getPayloadFromByteBufferList() );
((FramedataImpl1) current_continuous_frame ).isValid();
try {
webSocketImpl.getWebSocketListener().onWebsocketMessage( webSocketImpl, current_continuous_frame.getPayloadData() );
} catch ( RuntimeException e ) {
log.error( "Runtime exception during onWebsocketMessage", e );
webSocketImpl.getWebSocketListener().onWebsocketError( webSocketImpl, e );
}
}
current_continuous_frame = null;
byteBufferList.clear();
} else if( current_continuous_frame == null ) {
log.error( "Protocol error: Continuous frame sequence was not started." );
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence was not started." );
}
//Check if the whole payload is valid utf8, when the opcode indicates a text
if( curop == Opcode.TEXT ) {
if( !Charsetfunctions.isValidUTF8( frame.getPayloadData() ) ) {
log.error( "Protocol error: Payload is not UTF8" );
throw new InvalidDataException( CloseFrame.NO_UTF8 );
}
}
//Checking if the current continuous frame contains a correct payload with the other frames combined
if( curop == Opcode.CONTINUOUS && current_continuous_frame != null ) {
byteBufferList.add( frame.getPayloadData() );
}
} else if( current_continuous_frame != null ) {
log.error( "Protocol error: Continuous frame sequence not completed." );
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence not completed." );
} else if( curop == Opcode.TEXT ) {
try {
webSocketImpl.getWebSocketListener().onWebsocketMessage( webSocketImpl, Charsetfunctions.stringUtf8( frame.getPayloadData() ) );
} catch ( RuntimeException e ) {
log.error( "Runtime exception during onWebsocketMessage", e );
webSocketImpl.getWebSocketListener().onWebsocketError( webSocketImpl, e );
}
} else if( curop == Opcode.BINARY ) {
try {
webSocketImpl.getWebSocketListener().onWebsocketMessage( webSocketImpl, frame.getPayloadData() );
} catch ( RuntimeException e ) {
log.error( "Runtime exception during onWebsocketMessage", e );
webSocketImpl.getWebSocketListener().onWebsocketError( webSocketImpl, e );
}
} else {
log.error( "non control or continious frame expected");
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "non control or continious frame expected" );
}
}
@Override
public CloseHandshakeType getCloseHandshakeType() {
return CloseHandshakeType.TWOWAY;
}
@Override
public String toString() {
String result = super.toString();
if( getExtension() != null )
result += " extension: " + getExtension().toString();
if ( getProtocol() != null )
result += " protocol: " + getProtocol().toString();
return result;
}
@Override
public boolean equals( Object o ) {
if( this == o ) return true;
if( o == null || getClass() != o.getClass() ) return false;
Draft_6455 that = ( Draft_6455 ) o;
if( extension != null ? !extension.equals( that.extension ) : that.extension != null ) return false;
return protocol != null ? protocol.equals( that.protocol ) : that.protocol == null;
}
@Override
public int hashCode() {
int result = extension != null ? extension.hashCode() : 0;
result = 31 * result + ( protocol != null ? protocol.hashCode() : 0 );
return result;
}
/**
* Method to generate a full bytebuffer out of all the fragmented frame payload
* @return a bytebuffer containing all the data
* @throws LimitExedeedException will be thrown when the totalSize is bigger then Integer.MAX_VALUE due to not being able to allocate more
*/
private ByteBuffer getPayloadFromByteBufferList() throws LimitExedeedException {
long totalSize = 0;
for (ByteBuffer buffer : byteBufferList) {
totalSize +=buffer.limit();
}
if (totalSize > Integer.MAX_VALUE) {
log.trace( "Payloadsize is to big...");
throw new LimitExedeedException( "Payloadsize is to big..." );
}
ByteBuffer resultingByteBuffer = ByteBuffer.allocate( (int) totalSize );
for (ByteBuffer buffer : byteBufferList) {
resultingByteBuffer.put( buffer );
}
resultingByteBuffer.flip();
return resultingByteBuffer;
}
}