Skip to content

Commit aef9229

Browse files
committed
fixed npe cause by racing condition TooTallNate#205
1 parent 2c6f21d commit aef9229

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/main/java/org/java_websocket/WebSocketAdapter.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.java_websocket;
22

3+
import java.net.InetSocketAddress;
4+
35
import org.java_websocket.drafts.Draft;
46
import org.java_websocket.exceptions.InvalidDataException;
7+
import org.java_websocket.exceptions.InvalidHandshakeException;
58
import org.java_websocket.framing.Framedata;
69
import org.java_websocket.framing.Framedata.Opcode;
710
import org.java_websocket.framing.FramedataImpl1;
@@ -79,12 +82,23 @@ public void onWebsocketPong( WebSocket conn, Framedata f ) {
7982
* This is specifically implemented for gitime's WebSocket client for Flash:
8083
* http://github.com/gimite/web-socket-js
8184
*
82-
* @return An XML String that comforms to Flash's security policy. You MUST
85+
* @return An XML String that comforts to Flash's security policy. You MUST
8386
* not include the null char at the end, it is appended automatically.
87+
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained e.g because the websocket is not connected.
8488
*/
8589
@Override
86-
public String getFlashPolicy( WebSocket conn ) {
87-
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";
90+
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException {
91+
InetSocketAddress adr = conn.getLocalSocketAddress();
92+
if(null == adr){
93+
throw new InvalidHandshakeException( "socket not bound" );
94+
}
95+
96+
StringBuffer sb = new StringBuffer( 90 );
97+
sb.append( "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" );
98+
sb.append(adr.getPort());
99+
sb.append( "\" /></cross-domain-policy>\0" );
100+
101+
return sb.toString();
88102
}
89103

90104
}

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,12 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
198198
if( draft == null ) {
199199
HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer );
200200
if( isflashedgecase == HandshakeState.MATCHED ) {
201-
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
202-
close( CloseFrame.FLASHPOLICY, "" );
201+
try {
202+
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
203+
close( CloseFrame.FLASHPOLICY, "" );
204+
} catch ( InvalidDataException e ) {
205+
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
206+
}
203207
return false;
204208
}
205209
}

src/main/java/org/java_websocket/WebSocketListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ public interface WebSocketListener {
139139
/**
140140
* Gets the XML string that should be returned if a client requests a Flash
141141
* security policy.
142+
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained.
142143
*/
143-
public String getFlashPolicy( WebSocket conn );
144+
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException;
144145

145146
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
146147
public void onWriteDemand( WebSocket conn );

0 commit comments

Comments
 (0)