33import java .nio .ByteBuffer ;
44import java .nio .charset .Charset ;
55import java .util .Arrays ;
6+ import java .util .Collections ;
67import java .util .Iterator ;
78import java .util .List ;
89
@@ -14,29 +15,34 @@ public abstract class Draft{
1415 /**
1516 * The WebSocket protocol expects UTF-8 encoded bytes.
1617 */
17- public final static Charset UTF8_CHARSET = Charset .forName ( "UTF-8" );
18+ public static final Charset UTF8_CHARSET = Charset .forName ( "UTF-8" );
19+ private static final byte [] FLASH_POLICY_REQUEST = "<policy-file-request/>" .getBytes ( UTF8_CHARSET );
1820
19- public abstract boolean acceptHandshakeAsServer ( Handshakedata handshakedata ) throws InvalidHandshakeException ;
20- public abstract boolean acceptHandshakeAsClient ( Handshakedata request , Handshakedata response ) throws InvalidHandshakeException ;
21+ public enum HandshakeState {
22+ /**Handshake matched this Draft successfully*/
23+ MATCHED ,
24+ /**Handshake is does not match this Draft*/
25+ NOT_MATCHED ,
26+ /**Handshake matches this Draft but is not complete*/
27+ MATCHING
28+ //,/**Can not yet say anything*/
29+ //PENDING not yet in use
30+ }
31+
32+ public abstract HandshakeState acceptHandshakeAsServer ( Handshakedata handshakedata ) throws InvalidHandshakeException ;
33+ public abstract HandshakeState acceptHandshakeAsClient ( Handshakedata request , Handshakedata response ) throws InvalidHandshakeException ;
2134 public abstract List <Framedata > translateFrame ( ByteBuffer buffer , int read );
2235 public abstract ByteBuffer createBinaryFrame ( Framedata framedata ); //TODO Allow to send data on the base of an Iterator or InputStream
2336 public abstract List <Framedata > createFrames ( String text , boolean mask );
2437 public abstract List <Framedata > createFrames ( byte [] binary , boolean mask );
38+ public abstract HandshakeBuilder postProcessHandshakeRequestAsClient ( HandshakeBuilder request ) throws InvalidHandshakeException ;
39+ public abstract HandshakeBuilder postProcessHandshakeResponseAsServer ( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException ;
2540
26- public HandshakeBuilder postProcessHandshakeRequestAsClient ( HandshakeBuilder request ){
27- request .put ( "Upgrade" , "websocket" );
28- request .put ( "Connection" , "Upgrade" );
29- return request ;
30- }
31-
32- public HandshakeBuilder postProcessHandshakeResponseAsServer ( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException {
33- //sb.append ( "HTTP/1.1 101 Switching Protocols\r\n" );
34- response .put ( "Upgrade" , "websocket" );
35- response .put ( "Connection" , /*"Upgrade"*/ request .getFieldValue ( "Connection" ) ); //to respond a Connection keep alives
36- return response ;
41+ public List <ByteBuffer > createHandshake ( Handshakedata handshakedata , Role ownrole ){
42+ return createHandshake ( handshakedata , ownrole , true );
3743 }
3844
39- public /*static*/ ByteBuffer createHandshake ( Handshakedata handshakedata , Role ownrole ){
45+ public List < ByteBuffer > createHandshake ( Handshakedata handshakedata , Role ownrole , boolean withcontent ){
4046 StringBuilder bui = new StringBuilder ( 100 );
4147 if ( ownrole == Role .CLIENT ){
4248 bui .append ( "GET " );
@@ -61,15 +67,15 @@ else if( ownrole == Role.SERVER ){
6167 }
6268 bui .append ( "\r \n " );
6369 byte [] httpheader = bui .toString ().getBytes ( UTF8_CHARSET );
64- byte [] content = handshakedata .getContent () ;
70+ byte [] content = withcontent ? handshakedata .getContent () : null ;
6571 ByteBuffer bytebuffer = ByteBuffer .allocate ( ( content ==null ? 0 : content .length ) + httpheader .length );
6672 bytebuffer .put ( httpheader );
6773 if ( content !=null )
6874 bytebuffer .put ( content );
69- return bytebuffer ;
70-
75+ return Collections .singletonList ( bytebuffer );
7176 }
72- public static Handshakedata translateHandshake ( byte [] buffer , int readcount ){
77+
78+ public static HandshakeBuilder translateHandshakeHttp ( byte [] buffer , int readcount ) throws InvalidHandshakeException {
7379 HandshakedataImpl1 draft = new HandshakedataImpl1 ();
7480
7581 ByteBuffer message = ByteBuffer .allocate ( readcount );
@@ -78,9 +84,10 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
7884 int previndex = 0 ;
7985 int index = findNewLine ( lines , previndex );
8086 if ( index == lines .length )
81- return null ;
87+ throw new InvalidHandshakeException ( "not an http header" ); ;
8288 String line = new String ( lines , previndex , index - previndex );
8389 String [] firstLineTokens = line .split (" " );
90+ //if( firstLineTokens.length != 3)
8491 String path = firstLineTokens [1 ];
8592 draft .setResourceDescriptor ( path );
8693 //TODO Care about resources here like: GET /chat HTTP/1.1
@@ -95,7 +102,7 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
95102 if ( index != previndex ) {
96103 String [] pair = line .split ( ":" , 2 );
97104 if ( pair .length != 2 )
98- return null ;
105+ throw new InvalidHandshakeException ( "not an http header" ) ;
99106 draft .put ( pair [ 0 ] , pair [ 1 ].replaceFirst ( "^ +" , "" ) );
100107 }
101108 previndex = index + 2 ;
@@ -107,6 +114,10 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
107114 draft .setContent ( ByteBuffer .allocate ( length ).put ( lines , previndex , length ).array () );
108115 return draft ;
109116 }
117+
118+ public Handshakedata translateHandshake ( byte [] buffer , int readcount ) throws InvalidHandshakeException {
119+ return translateHandshakeHttp ( buffer , readcount );
120+ }
110121
111122 /**will return the index of the first \r\n or the index off the last element in arr*/
112123 public static int findNewLine ( byte [] arr , int offset ) {
@@ -119,13 +130,12 @@ public static int findNewLine( byte[] arr , int offset ) {
119130 }
120131
121132 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 ] ){
133+ for ( int i = 0 ; i < requestsize && i < FLASH_POLICY_REQUEST .length ; i ++ ){
134+ if ( FLASH_POLICY_REQUEST [i ] != request [i ] ){
125135 return false ;
126136 }
127137 }
128- return requestsize >= req .length ;
138+ return requestsize >= FLASH_POLICY_REQUEST .length ;
129139 }
130140
131141}
0 commit comments