Skip to content

Commit e56687d

Browse files
committed
improved close frame standard compliance
1 parent 6ea57a7 commit e56687d

9 files changed

Lines changed: 104 additions & 45 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ public static String stringAscii( byte[] bytes, int offset, int length ){
4646
}
4747

4848
public static String stringUtf8( byte[] bytes ) throws CharacterCodingException {
49-
return stringAscii( bytes, 0, bytes.length );
49+
return stringUtf8( bytes, 0, bytes.length );
5050
}
5151

5252
public static String stringUtf8( byte[] bytes, int off, int length ) throws CharacterCodingException {
53-
CharsetDecoder encode = Charset.forName( "UTF8" ).newDecoder();
54-
encode.onMalformedInput( codingErrorAction );
55-
return encode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
53+
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
54+
decode.onMalformedInput( codingErrorAction );
55+
decode.onUnmappableCharacter( codingErrorAction );
56+
//decode.replaceWith( "X" );
57+
String s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
58+
return s;
5659
}
5760
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.nio.charset.CharacterCodingException;
44

5+
import net.tootallnate.websocket.exeptions.InvalidFrameException;
6+
57
public interface CloseFrame extends Framedata {
68
/**
79
* indicates a normal closure, meaning whatever purpose the
@@ -77,6 +79,6 @@ public interface CloseFrame extends Framedata {
7779
public static final int NEVERCONNECTED = -1;
7880
public static final int BUGGYCLOSE = -2;
7981

80-
public int getCloseCode();
81-
public String getMessage() throws CharacterCodingException;
82+
public int getCloseCode() throws InvalidFrameException;
83+
public String getMessage() throws CharacterCodingException , InvalidFrameException;
8284
}

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

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,32 @@
33
import java.nio.ByteBuffer;
44
import java.nio.charset.CharacterCodingException;
55

6+
import net.tootallnate.websocket.exeptions.InvalidDataException;
7+
import net.tootallnate.websocket.exeptions.InvalidFrameException;
8+
69
public class CloseFrameBuilder extends FramedataImpl1 implements CloseFrame {
710

11+
private int code;
12+
private String reason;
13+
814
public CloseFrameBuilder() {
915
super( Opcode.CLOSING );
1016
setFin( true );
1117
}
1218

13-
public CloseFrameBuilder( int code ) {
19+
public CloseFrameBuilder( int code ) throws InvalidDataException {
1420
super( Opcode.CLOSING );
1521
setFin( true );
1622
setCodeAndMessage( code, "" );
1723
}
1824

19-
public CloseFrameBuilder( int code , String m ) {
25+
public CloseFrameBuilder( int code , String m ) throws InvalidDataException {
2026
super( Opcode.CLOSING );
2127
setFin( true );
2228
setCodeAndMessage( code, m );
2329
}
2430

25-
public void setCloseCode( int code ) {
26-
try {
27-
setCodeAndMessage( code, getMessage() );
28-
} catch ( CharacterCodingException e ) {
29-
assert ( false );
30-
// not expected
31-
throw new RuntimeException( e );
32-
}
33-
}
34-
35-
/** This operation changes the payload */
36-
public void setMessage( String message ) {
37-
setCodeAndMessage( getCloseCode(), message );
38-
}
39-
40-
public void setCodeAndMessage( int code, String m ) {
31+
private void setCodeAndMessage( int code, String m ) throws InvalidDataException {
4132
byte[] by = Charsetfunctions.utf8Bytes( m );
4233
ByteBuffer buf = ByteBuffer.allocate( 4 );
4334
buf.putInt( code );
@@ -48,39 +39,60 @@ public void setCodeAndMessage( int code, String m ) {
4839
setPayload( pay.array() );
4940
}
5041

51-
@Override
52-
public int getCloseCode() {
53-
int code = CloseFrame.NOCODE;
54-
if( unmaskedpayload.array().length >= 2 ) {
42+
private void initCloseCode() throws InvalidFrameException {
43+
code = CloseFrame.NOCODE;
44+
byte[] payload = getPayloadData();
45+
if( payload.length >= 2 ) {
5546
ByteBuffer bb = ByteBuffer.allocate( 4 );
5647
bb.position( 2 );
57-
bb.put( unmaskedpayload.array(), 0, 2 );
48+
bb.put( payload, 0, 2 );
5849
bb.position( 0 );
5950
code = bb.getInt();
6051
if( code < 0 || code > Short.MAX_VALUE )
6152
code = CloseFrame.NOCODE;
53+
if( code < CloseFrame.NORMAL || code > CloseFrame.EXTENSION || code == NOCODE || code == 1004 ) {
54+
throw new InvalidFrameException( "bad code " + code );
55+
}
6256
}
63-
return code;
6457
}
6558

6659
@Override
67-
public String getMessage() throws CharacterCodingException {
68-
if( getCloseCode() == CloseFrame.NOCODE ) {
69-
return Charsetfunctions.stringUtf8( unmaskedpayload.array() );
60+
public int getCloseCode() {
61+
return code;
62+
}
63+
64+
private void initMessage() throws CharacterCodingException , InvalidFrameException {
65+
if( code == CloseFrame.NOCODE ) {
66+
reason = Charsetfunctions.stringUtf8( getPayloadData() );
67+
} else {
68+
byte[] payload = getPayloadData();
69+
reason = Charsetfunctions.stringUtf8( payload, 2, payload.length - 2 );
7070
}
71-
else{
72-
return Charsetfunctions.stringUtf8( unmaskedpayload.array(),2,unmaskedpayload.array().length-2 );
73-
}
71+
}
72+
73+
@Override
74+
public String getMessage() {
75+
return reason;
7476
}
7577

7678
@Override
7779
public String toString() {
78-
return super.toString() + "code: " + getCloseCode();
80+
return super.toString() + "code: " + code;
7981
}
8082

83+
@Override
84+
public void setPayload( byte[] payload ) throws InvalidDataException {
85+
super.setPayload( payload );
86+
initCloseCode();
87+
try {
88+
initMessage();
89+
} catch ( CharacterCodingException e ) {
90+
throw new InvalidDataException( CloseFrame.NO_UTF8 );
91+
}
92+
}
8193
@Override
8294
public byte[] getPayloadData() {
83-
if(getCloseCode()==NOCODE)
95+
if( code == NOCODE )
8496
return new byte[ 0 ];
8597
return super.getPayloadData();
8698
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package net.tootallnate.websocket;
22

3+
import net.tootallnate.websocket.exeptions.InvalidDataException;
4+
35
public interface FrameBuilder extends Framedata {
46

57
public abstract void setFin( boolean fin );
68

79
public abstract void setOptcode( Opcode optcode );
810

9-
public abstract void setPayload( byte[] payload );
11+
public abstract void setPayload( byte[] payload ) throws InvalidDataException;
1012

1113
public abstract void setTransferemasked( boolean transferemasked );
1214

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import java.nio.ByteBuffer;
44

5+
import net.tootallnate.websocket.exeptions.InvalidDataException;
56
import net.tootallnate.websocket.exeptions.InvalidFrameException;
67

78
public class FramedataImpl1 implements FrameBuilder {
89
protected static byte[] emptyarray = {};
910
protected boolean fin;
1011
protected Opcode optcode;
11-
protected ByteBuffer unmaskedpayload;
12+
private ByteBuffer unmaskedpayload;
1213
protected boolean transferemasked;
1314

1415
public FramedataImpl1() {
@@ -57,7 +58,7 @@ public void setOptcode( Opcode optcode ) {
5758
}
5859

5960
@Override
60-
public void setPayload( byte[] payload ) {
61+
public void setPayload( byte[] payload ) throws InvalidDataException {
6162
unmaskedpayload = ByteBuffer.wrap( payload );
6263
}
6364

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,12 @@ public void closeDirect( int code, String message ) throws IOException {
307307
if( !closeHandshakeSent ) {
308308
if( handshakeComplete ) {
309309
flush();
310-
sendFrameDirect( new CloseFrameBuilder( code, message ) );
310+
try {
311+
sendFrameDirect( new CloseFrameBuilder( code, message ) );
312+
} catch ( InvalidDataException e ) {
313+
wsl.onError( this, e );
314+
closeConnection( CloseFrame.ABNROMAL_CLOSE, "generated frame is invalid", false );
315+
}
311316
}
312317
if( code == CloseFrame.PROTOCOL_ERROR )// this endpoint found a PROTOCOL_ERROR
313318
closeConnection( code, false );

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.tootallnate.websocket.exeptions.InvalidFrameException;
2424
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
2525
import net.tootallnate.websocket.exeptions.LimitExedeedException;
26+
import net.tootallnate.websocket.exeptions.NotSendableException;
2627

2728
public class Draft_10 extends Draft {
2829

@@ -119,7 +120,11 @@ public ByteBuffer createBinaryFrame( Framedata framedata ) {
119120
@Override
120121
public List<Framedata> createFrames( byte[] binary, boolean mask ) {
121122
FrameBuilder curframe = new FramedataImpl1();
122-
curframe.setPayload( binary );
123+
try {
124+
curframe.setPayload( binary );
125+
} catch ( InvalidDataException e ) {
126+
throw new NotSendableException( e );
127+
}
123128
curframe.setFin( true );
124129
curframe.setOptcode( Opcode.BINARY );
125130
curframe.setTransferemasked( mask );
@@ -130,7 +135,11 @@ public List<Framedata> createFrames( byte[] binary, boolean mask ) {
130135
public List<Framedata> createFrames( String text, boolean mask ) {
131136
FrameBuilder curframe = new FramedataImpl1();
132137
byte[] pay = Charsetfunctions.utf8Bytes( text );
133-
curframe.setPayload( pay );
138+
try {
139+
curframe.setPayload( pay );
140+
} catch ( InvalidDataException e ) {
141+
throw new NotSendableException( e );
142+
}
134143
curframe.setFin( true );
135144
curframe.setOptcode( Opcode.TEXT );
136145
curframe.setTransferemasked( mask );

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.tootallnate.websocket.Handshakedata;
1717
import net.tootallnate.websocket.exeptions.InvalidDataException;
1818
import net.tootallnate.websocket.exeptions.InvalidHandshakeException;
19+
import net.tootallnate.websocket.exeptions.NotSendableException;
1920

2021
public class Draft_75 extends Draft {
2122

@@ -73,7 +74,11 @@ public List<Framedata> createFrames( byte[] binary, boolean mask ) {
7374
@Override
7475
public List<Framedata> createFrames( String text, boolean mask ) {
7576
FrameBuilder frame = new FramedataImpl1();
76-
frame.setPayload( Charsetfunctions.utf8Bytes( text ) );
77+
try {
78+
frame.setPayload( Charsetfunctions.utf8Bytes( text ) );
79+
} catch ( InvalidDataException e ) {
80+
throw new NotSendableException( e );
81+
}
7782
frame.setFin( true );
7883
frame.setOptcode( Opcode.TEXT );
7984
frame.setTransferemasked( mask );
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.tootallnate.websocket.exeptions;
2+
3+
public class NotSendableException extends RuntimeException {
4+
5+
public NotSendableException() {
6+
}
7+
8+
public NotSendableException( String message ) {
9+
super( message );
10+
}
11+
12+
public NotSendableException( Throwable cause ) {
13+
super( cause );
14+
}
15+
16+
public NotSendableException( String message , Throwable cause ) {
17+
super( message, cause );
18+
}
19+
20+
}

0 commit comments

Comments
 (0)