1+ package net .tootallnate .websocket ;
2+
3+ import java .nio .ByteBuffer ;
4+ import java .nio .charset .Charset ;
5+ import java .util .Iterator ;
6+ import java .util .List ;
7+
8+ import net .tootallnate .websocket .WebSocket .Role ;
9+ import net .tootallnate .websocket .exeptions .InvalidHandshakeException ;
10+
11+ public abstract class Draft {
12+
13+ /**
14+ * The WebSocket protocol expects UTF-8 encoded bytes.
15+ */
16+ public final static Charset UTF8_CHARSET = Charset .forName ( "UTF-8" );
17+
18+ public abstract boolean acceptHandshakeAsServer ( Handshakedata handshakedata ) throws InvalidHandshakeException ;
19+ public abstract boolean acceptHandshakeAsClient ( Handshakedata request , Handshakedata response );
20+ public abstract List <Framedata > translateFrame ( byte [] buffer , int read );
21+ public abstract ByteBuffer createBinaryFrame ( Framedata framedata ); //TODO Allow to send data on the base of an Iterator or InputStream
22+ public abstract List <Framedata > createFrames ( String text , boolean mask );
23+ public abstract List <Framedata > createFrames ( byte [] binary , boolean mask );
24+
25+ public HandshakeBuilder postProcessHandshakeRequestAsClient ( HandshakeBuilder request ){
26+ request .put ( "Upgrade" , "websocket" );
27+ request .put ( "Connection" , "Upgrade" );
28+ return request ;
29+ }
30+
31+ public HandshakeBuilder postProcessHandshakeResponseAsServer ( Handshakedata request , HandshakeBuilder response ) throws InvalidHandshakeException {
32+ //sb.append ( "HTTP/1.1 101 Switching Protocols\r\n" );
33+ response .put ( "Upgrade" , "websocket" );
34+ response .put ( "Connection" , "Upgrade" );
35+ return response ;
36+ }
37+
38+ public /*static*/ ByteBuffer createHandshake ( Handshakedata handshakedata , Role ownrole ){
39+ StringBuilder bui = new StringBuilder ( 100 );
40+ if ( ownrole == Role .CLIENT ){
41+ bui .append ( "GET " );
42+ bui .append ( handshakedata .getResourceDescriptor () );
43+ bui .append ( " HTTP/1.1" );
44+ }
45+ else if ( ownrole == Role .SERVER ){
46+ bui .append ( "HTTP/1.1 101 Switching Protocols" );
47+ }
48+ else {
49+ throw new RuntimeException ( "unknow role" );
50+ }
51+ bui .append ( "\r \n " );
52+ Iterator <String > it = handshakedata .iterateHttpFields ();
53+ while ( it .hasNext () ){
54+ String fieldname = it .next ();
55+ String fieldvalue = handshakedata .getFieldValue ( fieldname );
56+ bui .append ( fieldname );
57+ bui .append ( ": " );
58+ bui .append ( fieldvalue );
59+ bui .append ( "\r \n " );
60+ }
61+ byte [] httpheader = bui .toString ().getBytes ( UTF8_CHARSET );
62+ byte [] content = handshakedata .getContent ();
63+ ByteBuffer bytebuffer = ByteBuffer .allocate ( ( content ==null ? 0 : content .length ) + httpheader .length + 2 );
64+ bytebuffer .put ( httpheader );
65+ if ( content !=null )
66+ bytebuffer .put ( content );
67+ bytebuffer .put ( ( byte )'\r' );
68+ bytebuffer .put ( ( byte )'\n' );
69+ return bytebuffer ;
70+
71+ }
72+ public static Handshakedata translateHandshake ( byte [] buffer , int readcount ){
73+ HandshakedataImpl1 draft = new HandshakedataImpl1 ();
74+
75+ ByteBuffer message = ByteBuffer .allocate ( readcount );
76+ message .put ( buffer , 0 , readcount );
77+ byte [] lines = message .array ();
78+ int previndex = 0 ;
79+ int index = findNewLine ( lines , previndex );
80+ if ( index == -1 )
81+ return null ;
82+ String line = new String ( lines , previndex , index - previndex );
83+ //TODO Care about resources here like: GET /chat HTTP/1.1
84+ //if ( line.startsWith ( "GET" ) == false )
85+ //if ( line.startsWith ( "HTTP" ) == false )
86+
87+ previndex = index + 2 ;
88+ index = findNewLine ( lines , previndex );
89+
90+ while ( index != -1 ) {
91+ int length = index - previndex ;
92+ line = new String ( lines , previndex , length );
93+ if ( index != previndex ) {
94+ String [] pair = line .split ( ":" , 2 );
95+ 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+ }
99+ previndex = index + 2 ;
100+ index = findNewLine ( lines , previndex );
101+ }
102+ return draft ;
103+ }
104+
105+ public static int findNewLine ( byte [] arr , int offset ) {
106+ int len = arr .length - 1 ;
107+ for ( int i = offset ; i < len ; i ++ )
108+ if ( arr [i ] == (byte )'\r' && arr [ i + 1 ] == (byte )'\n' )
109+ return i ;
110+ return -1 ;
111+ }
112+ }
0 commit comments