Skip to content

Commit a40e618

Browse files
committed
attempt to fix TooTallNate#64
1 parent d427c07 commit a40e618

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public interface CloseFrame extends Framedata {
7777
/** The connection had never been established */
7878
public static final int NEVERCONNECTED = -1;
7979
public static final int BUGGYCLOSE = -2;
80+
public static final int FLASHPOLICY = -3;
8081

8182
public int getCloseCode() throws InvalidFrameException;
8283
public String getMessage() throws InvalidDataException;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public enum HandshakeState {
2424
// PENDING not yet in use
2525
}
2626

27-
private static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>" );
27+
public static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>\0" );
2828

2929
protected Role role = null;
3030

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ public enum Role {
7979

8080
public List<Draft> known_drafts;
8181

82-
private static final byte[] FLASH_POLICY_REQUEST = Charsetfunctions.utf8Bytes( "<policy-file-request/>" );
83-
84-
private int flash_policy_index = 0;
85-
8682
private SocketChannel sockchannel;
8783

8884
// CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -149,13 +145,18 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
149145
System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + "}" );
150146
if( !handshakeComplete ) {
151147
Handshakedata handshake;
152-
HandshakeState handshakestate = null;
153148

154-
handshakestate = isFlashEdgeCase( socketBuffer );
155-
if( handshakestate == HandshakeState.MATCHED ) {
156-
channelWriteDirect( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
157-
return;
149+
if( draft == null ) {
150+
HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer );
151+
if( isflashedgecase == HandshakeState.MATCHED ) {
152+
channelWriteDirect( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
153+
closeDirect( CloseFrame.FLASHPOLICY, "" );
154+
return;
155+
} else if( isflashedgecase == HandshakeState.MATCHING ) {
156+
return;
157+
}
158158
}
159+
HandshakeState handshakestate = null;
159160
socketBuffer.mark();
160161
try {
161162
if( role == Role.SERVER ) {
@@ -321,6 +322,8 @@ public void closeDirect( int code, String message ) throws IOException {
321322
} else {
322323
closeConnection( code, false );
323324
}
325+
} else if( code == CloseFrame.FLASHPOLICY ) {
326+
closeConnection( CloseFrame.FLASHPOLICY, true );
324327
} else {
325328
closeConnection( CloseFrame.NEVERCONNECTED, false );
326329
}
@@ -432,16 +435,21 @@ public void flush() throws IOException {
432435
}
433436

434437
public HandshakeState isFlashEdgeCase( ByteBuffer request ) {
435-
if( flash_policy_index >= FLASH_POLICY_REQUEST.length )
438+
if( request.limit() > Draft.FLASH_POLICY_REQUEST.length )
436439
return HandshakeState.NOT_MATCHED;
437-
request.mark();
438-
for( ; request.hasRemaining() && flash_policy_index < FLASH_POLICY_REQUEST.length ; flash_policy_index++ ) {
439-
if( FLASH_POLICY_REQUEST[ flash_policy_index ] != request.get() ) {
440-
request.reset();
441-
return HandshakeState.NOT_MATCHED;
440+
else if( request.limit() < Draft.FLASH_POLICY_REQUEST.length ) {
441+
return HandshakeState.MATCHING;
442+
} else {
443+
request.mark();
444+
for( int flash_policy_index = 0 ; request.hasRemaining() ; flash_policy_index++ ) {
445+
if( Draft.FLASH_POLICY_REQUEST[ flash_policy_index ] != request.get() ) {
446+
request.reset();
447+
return HandshakeState.NOT_MATCHED;
448+
}
442449
}
450+
return HandshakeState.MATCHED;
451+
// return request.remaining() >= Draft.FLASH_POLICY_REQUEST.length ? HandshakeState.MATCHED : HandshakeState.MATCHING;
443452
}
444-
return request.remaining() >= FLASH_POLICY_REQUEST.length ? HandshakeState.MATCHED : HandshakeState.MATCHING;
445453
}
446454

447455
public void startHandshake( HandshakeBuilder handshakedata ) throws InvalidHandshakeException , InterruptedException {

0 commit comments

Comments
 (0)