Skip to content

Commit 6ea57a7

Browse files
committed
added autobahn proved fragmentation support
1 parent 391c186 commit 6ea57a7

3 files changed

Lines changed: 71 additions & 56 deletions

File tree

‎src/net/tootallnate/websocket/FramedataImpl1.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ public void append( Framedata nextframe ) throws InvalidFrameException {
7575
ByteBuffer tmp = ByteBuffer.allocate( nextframe.getPayloadData().length + unmaskedpayload.capacity() );
7676
tmp.put( unmaskedpayload.array() );
7777
tmp.put( nextframe.getPayloadData() );
78+
unmaskedpayload = tmp;
7879
}
80+
fin = nextframe.isFin();
7981
}
8082

8183
@Override

‎src/net/tootallnate/websocket/WebSocket.java‎

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.nio.ByteBuffer;
66
import java.nio.channels.NotYetConnectedException;
77
import java.nio.channels.SocketChannel;
8+
import java.nio.charset.CharacterCodingException;
89
import java.util.ArrayList;
910
import java.util.Collection;
1011
import java.util.List;
@@ -229,65 +230,60 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
229230
List<Framedata> frames;
230231
try {
231232
frames = draft.translateFrame( socketBuffer );
232-
} catch ( InvalidDataException e1 ) {
233-
close( e1 );
234-
return;
235-
}
236-
for( Framedata f : frames ) {
237-
if( DEBUG )
238-
System.out.println( "matched frame: " + f );
239-
Opcode curop = f.getOpcode();
240-
if( curop == null )// Ignore undefined opcodes
241-
continue;
242-
else if( curop == Opcode.CLOSING ) {
243-
int code = CloseFrame.NOCODE;
244-
String reason = "";
245-
if( f instanceof CloseFrame ) {
246-
CloseFrame cf = (CloseFrame) f;
247-
code = cf.getCloseCode();
248-
reason = cf.getMessage();
249-
}
250-
if( closeHandshakeSent ) {
251-
// complete the close handshake by disconnecting
252-
closeConnection( code, reason, true );
253-
} else {
254-
// echo close handshake
255-
close( code, reason );
256-
closeConnection( code, reason, false );
257-
}
258-
continue;
259-
} else if( curop == Opcode.PING ) {
260-
wsl.onPing( this, f );
261-
continue;
262-
} else if( curop == Opcode.PONG ) {
263-
wsl.onPong( this, f );
264-
continue;
265-
}
266-
if( currentframe == null ) {
267-
if( f.isFin() ) {
268-
if( f.getOpcode() == Opcode.TEXT ) {
269-
wsl.onMessage( this, Charsetfunctions.stringUtf8( f.getPayloadData() ) );
270-
} else if( f.getOpcode() == Opcode.BINARY ) {
271-
wsl.onMessage( this, f.getPayloadData() );
233+
for( Framedata f : frames ) {
234+
if( DEBUG )
235+
System.out.println( "matched frame: " + f );
236+
Opcode curop = f.getOpcode();
237+
if( curop == Opcode.CLOSING ) {
238+
int code = CloseFrame.NOCODE;
239+
String reason = "";
240+
if( f instanceof CloseFrame ) {
241+
CloseFrame cf = (CloseFrame) f;
242+
code = cf.getCloseCode();
243+
reason = cf.getMessage();
244+
}
245+
if( closeHandshakeSent ) {
246+
// complete the close handshake by disconnecting
247+
closeConnection( code, reason, true );
272248
} else {
273-
if( DEBUG )
274-
System.out.println( "Ignoring frame:" + f.toString() );
249+
// echo close handshake
250+
close( code, reason );
251+
closeConnection( code, reason, false );
275252
}
253+
continue;
254+
} else if( curop == Opcode.PING ) {
255+
wsl.onPing( this, f );
256+
continue;
257+
} else if( curop == Opcode.PONG ) {
258+
wsl.onPong( this, f );
259+
continue;
276260
} else {
277-
currentframe = f;
278-
}
279-
} else if( f.getOpcode() == Opcode.CONTINIOUS ) {
280-
try {
281-
currentframe.append( f );
282-
} catch ( InvalidFrameException e ) {
283-
wsl.onError( this, e );
284-
close( e );
285-
}
286-
if( f.isFin() ) {
287-
wsl.onMessage( this, Charsetfunctions.stringUtf8( currentframe.getPayloadData() ) );
288-
currentframe = null;
261+
// process non control frames
262+
if( currentframe == null ) {
263+
if( f.getOpcode() == Opcode.CONTINIOUS ) {
264+
throw new InvalidFrameException( "unexpected continious frame" );
265+
} else if( f.isFin() ) {
266+
// receive normal onframe message
267+
deliverMessage( f );
268+
} else {
269+
// remember the frame whose payload is about to be continued
270+
currentframe = f;
271+
}
272+
} else if( f.getOpcode() == Opcode.CONTINIOUS ) {
273+
currentframe.append( f );
274+
if( f.isFin() ) {
275+
deliverMessage( currentframe );
276+
currentframe = null;
277+
}
278+
} else {
279+
throw new InvalidDataException( CloseFrame.PROTOCOL_ERROR, "non control or continious frame expected" );
280+
}
289281
}
290282
}
283+
} catch ( InvalidDataException e1 ) {
284+
wsl.onError( this, e1 );
285+
close( e1 );
286+
return;
291287
}
292288
}
293289
}
@@ -303,7 +299,7 @@ public void close( int code, String message ) {
303299
try {
304300
closeDirect( code, message );
305301
} catch ( IOException e ) {
306-
closeConnection( CloseFrame.ABNROMAL_CLOSE,true );
302+
closeConnection( CloseFrame.ABNROMAL_CLOSE, true );
307303
}
308304
}
309305

@@ -463,6 +459,17 @@ private void writeDirect( List<ByteBuffer> bufs ) throws IOException {
463459
channelWriteDirect( b );
464460
}
465461
}
462+
private void deliverMessage( Framedata d ) throws CharacterCodingException {
463+
if( d.getOpcode() == Opcode.TEXT ) {
464+
wsl.onMessage( this, Charsetfunctions.stringUtf8( d.getPayloadData() ) );
465+
} else if( d.getOpcode() == Opcode.BINARY ) {
466+
wsl.onMessage( this, d.getPayloadData() );
467+
} else {
468+
if( DEBUG )
469+
System.out.println( "Ignoring frame:" + d.toString() );
470+
assert ( false );
471+
}
472+
}
466473

467474
private void open() throws IOException {
468475
if( DEBUG )

‎src/net/tootallnate/websocket/drafts/Draft_10.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static int readVersion( Handshakedata handshakedata ) {
5151
}
5252

5353
private ByteBuffer incompleteframe;
54+
private Framedata fragmentedframe = null;
5455

5556
@Override
5657
public HandshakeState acceptHandshakeAsClient( Handshakedata request, Handshakedata response ) throws InvalidHandshakeException {
@@ -278,7 +279,6 @@ public List<Framedata> translateFrame( ByteBuffer buffer ) throws LimitExedeedEx
278279
}
279280

280281
public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteException , InvalidDataException {
281-
282282
int maxpacketsize = buffer.limit() - buffer.position();
283283
int realpacketsize = 2;
284284
if( maxpacketsize < realpacketsize )
@@ -293,6 +293,12 @@ public Framedata translateSingleFrame( ByteBuffer buffer ) throws IncompleteExce
293293
int payloadlength = (byte) ( b2 & ~(byte) 128 );
294294
Opcode optcode = toOpcode( (byte) ( b1 & 15 ) );
295295

296+
if( !FIN ) {
297+
if( optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING ) {
298+
throw new InvalidFrameException( "control frames may no be fragmented" );
299+
}
300+
}
301+
296302
if( payloadlength >= 0 && payloadlength <= 125 ) {
297303
} else {
298304
if( optcode == Opcode.PING || optcode == Opcode.PONG || optcode == Opcode.CLOSING ) {

0 commit comments

Comments
 (0)