22
33import java .nio .ByteBuffer ;
44import java .nio .charset .Charset ;
5+ import java .util .Arrays ;
56import java .util .Iterator ;
67import java .util .List ;
78
@@ -43,7 +44,7 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer( Handshakedata req
4344 bui .append ( " HTTP/1.1" );
4445 }
4546 else if ( ownrole == Role .SERVER ){
46- bui .append ( "HTTP/1.1 101 Switching Protocols" );
47+ bui .append ( "HTTP/1.1 101 " + handshakedata . getHttpStatusMessage () );
4748 }
4849 else {
4950 throw new RuntimeException ( "unknow role" );
@@ -58,14 +59,13 @@ else if( ownrole == Role.SERVER ){
5859 bui .append ( fieldvalue );
5960 bui .append ( "\r \n " );
6061 }
62+ bui .append ( "\r \n " );
6163 byte [] httpheader = bui .toString ().getBytes ( UTF8_CHARSET );
6264 byte [] content = handshakedata .getContent ();
63- ByteBuffer bytebuffer = ByteBuffer .allocate ( ( content ==null ? 0 : content .length ) + httpheader .length + 2 );
65+ ByteBuffer bytebuffer = ByteBuffer .allocate ( ( content ==null ? 0 : content .length ) + httpheader .length );
6466 bytebuffer .put ( httpheader );
6567 if ( content !=null )
6668 bytebuffer .put ( content );
67- bytebuffer .put ( ( byte )'\r' );
68- bytebuffer .put ( ( byte )'\n' );
6969 return bytebuffer ;
7070
7171 }
@@ -80,6 +80,9 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
8080 if ( index == lines .length )
8181 return null ;
8282 String line = new String ( lines , previndex , index - previndex );
83+ String [] firstLineTokens = line .split (" " );
84+ String path = firstLineTokens [1 ];
85+ draft .setResourceDescriptor ( path );
8386 //TODO Care about resources here like: GET /chat HTTP/1.1
8487 //if ( line.startsWith ( "GET" ) == false )
8588 //if ( line.startsWith ( "HTTP" ) == false )
@@ -88,18 +91,20 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
8891 index = findNewLine ( lines , previndex );
8992 int length = index - previndex ;
9093 while ( length != 0 ) {
91-
9294 line = new String ( lines , previndex , length );
9395 if ( index != previndex ) {
9496 String [] pair = line .split ( ":" , 2 );
9597 if ( pair .length != 2 )
96- draft . setContent ( ByteBuffer . allocate ( length ). put ( lines , previndex , length ). array () ); //this approach will also accept suspicious looking handshakes...
97- draft .put ( pair [ 0 ] , pair [ 1 ] );
98+ return null ;
99+ draft .put ( pair [ 0 ] , pair [ 1 ]. replaceFirst ( "^ +" , "" ) );
98100 }
99101 previndex = index + 2 ;
100102 index = findNewLine ( lines , previndex );
101103 length = index - previndex ;
102104 }
105+ previndex = index + 2 ;
106+ length = lines .length - previndex ;
107+ draft .setContent ( ByteBuffer .allocate ( length ).put ( lines , previndex , length ).array () );
103108 return draft ;
104109 }
105110
@@ -112,4 +117,15 @@ public static int findNewLine( byte[] arr , int offset ) {
112117 return i ;
113118 return i ;//the end of input will be handled like newline
114119 }
120+
121+ public static boolean isFlashEdgeCase ( byte [] request , int requestsize ){
122+ byte [] req = "<policy-file-request/>" .getBytes ( UTF8_CHARSET );
123+ for ( int i = 0 ; i < requestsize && i < req .length ; i ++ ){
124+ if ( req [i ] != request [i ] ){
125+ return false ;
126+ }
127+ }
128+ return requestsize >= req .length ;
129+ }
130+
115131}
0 commit comments