3
3
import java .nio .ByteBuffer ;
4
4
import java .nio .charset .Charset ;
5
5
import java .util .Arrays ;
6
+ import java .util .Collections ;
6
7
import java .util .Iterator ;
7
8
import java .util .List ;
8
9
@@ -14,29 +15,34 @@ public abstract class Draft{
14
15
/**
15
16
* The WebSocket protocol expects UTF-8 encoded bytes.
16
17
*/
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 );
18
20
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 ;
21
34
public abstract List <Framedata > translateFrame ( ByteBuffer buffer , int read );
22
35
public abstract ByteBuffer createBinaryFrame ( Framedata framedata ); //TODO Allow to send data on the base of an Iterator or InputStream
23
36
public abstract List <Framedata > createFrames ( String text , boolean mask );
24
37
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 ;
25
40
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 );
37
43
}
38
44
39
- public /*static*/ ByteBuffer createHandshake ( Handshakedata handshakedata , Role ownrole ){
45
+ public List < ByteBuffer > createHandshake ( Handshakedata handshakedata , Role ownrole , boolean withcontent ){
40
46
StringBuilder bui = new StringBuilder ( 100 );
41
47
if ( ownrole == Role .CLIENT ){
42
48
bui .append ( "GET " );
@@ -61,15 +67,15 @@ else if( ownrole == Role.SERVER ){
61
67
}
62
68
bui .append ( "\r \n " );
63
69
byte [] httpheader = bui .toString ().getBytes ( UTF8_CHARSET );
64
- byte [] content = handshakedata .getContent () ;
70
+ byte [] content = withcontent ? handshakedata .getContent () : null ;
65
71
ByteBuffer bytebuffer = ByteBuffer .allocate ( ( content ==null ? 0 : content .length ) + httpheader .length );
66
72
bytebuffer .put ( httpheader );
67
73
if ( content !=null )
68
74
bytebuffer .put ( content );
69
- return bytebuffer ;
70
-
75
+ return Collections .singletonList ( bytebuffer );
71
76
}
72
- public static Handshakedata translateHandshake ( byte [] buffer , int readcount ){
77
+
78
+ public static HandshakeBuilder translateHandshakeHttp ( byte [] buffer , int readcount ) throws InvalidHandshakeException {
73
79
HandshakedataImpl1 draft = new HandshakedataImpl1 ();
74
80
75
81
ByteBuffer message = ByteBuffer .allocate ( readcount );
@@ -78,9 +84,10 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
78
84
int previndex = 0 ;
79
85
int index = findNewLine ( lines , previndex );
80
86
if ( index == lines .length )
81
- return null ;
87
+ throw new InvalidHandshakeException ( "not an http header" ); ;
82
88
String line = new String ( lines , previndex , index - previndex );
83
89
String [] firstLineTokens = line .split (" " );
90
+ //if( firstLineTokens.length != 3)
84
91
String path = firstLineTokens [1 ];
85
92
draft .setResourceDescriptor ( path );
86
93
//TODO Care about resources here like: GET /chat HTTP/1.1
@@ -95,7 +102,7 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
95
102
if ( index != previndex ) {
96
103
String [] pair = line .split ( ":" , 2 );
97
104
if ( pair .length != 2 )
98
- return null ;
105
+ throw new InvalidHandshakeException ( "not an http header" ) ;
99
106
draft .put ( pair [ 0 ] , pair [ 1 ].replaceFirst ( "^ +" , "" ) );
100
107
}
101
108
previndex = index + 2 ;
@@ -107,6 +114,10 @@ public static Handshakedata translateHandshake( byte[] buffer, int readcount ){
107
114
draft .setContent ( ByteBuffer .allocate ( length ).put ( lines , previndex , length ).array () );
108
115
return draft ;
109
116
}
117
+
118
+ public Handshakedata translateHandshake ( byte [] buffer , int readcount ) throws InvalidHandshakeException {
119
+ return translateHandshakeHttp ( buffer , readcount );
120
+ }
110
121
111
122
/**will return the index of the first \r\n or the index off the last element in arr*/
112
123
public static int findNewLine ( byte [] arr , int offset ) {
@@ -119,13 +130,12 @@ public static int findNewLine( byte[] arr , int offset ) {
119
130
}
120
131
121
132
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 ] ){
125
135
return false ;
126
136
}
127
137
}
128
- return requestsize >= req .length ;
138
+ return requestsize >= FLASH_POLICY_REQUEST .length ;
129
139
}
130
140
131
141
}
0 commit comments