@@ -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