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
packageorg.java_websocket;
22

3+
importjava.net.InetSocketAddress;
4+
35
importorg.java_websocket.drafts.Draft;
46
importorg.java_websocket.exceptions.InvalidDataException;
7+
importorg.java_websocket.exceptions.InvalidHandshakeException;
58
importorg.java_websocket.framing.Framedata;
69
importorg.java_websocket.framing.Framedata.Opcode;
710
importorg.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-
publicStringgetFlashPolicy( WebSocketconn ){
87-
return"<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";
90+
publicStringgetFlashPolicy( WebSocketconn ) throwsInvalidDataException{
91+
InetSocketAddressadr = conn.getLocalSocketAddress();
92+
if(null == adr){
93+
thrownewInvalidHandshakeException( "socket not bound" );
94+
}
95+
96+
StringBuffersb = newStringBuffer( 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+
returnsb.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
HandshakeStateisflashedgecase = 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 ( InvalidDataExceptione ){
205+
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
206+
}
203207
returnfalse;
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-
publicStringgetFlashPolicy( WebSocketconn );
144+
publicStringgetFlashPolicy( WebSocketconn )throwsInvalidDataException;
144145

145146
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
146147
publicvoidonWriteDemand( WebSocketconn );

0 commit comments

Comments
(0)