77import java .nio .charset .Charset ;
88import java .security .NoSuchAlgorithmException ;
99import java .util .ArrayList ;
10- import java .util .HashMap ;
1110import java .util .List ;
11+
12+ import net .tootallnate .websocket .Framedata .Opcode ;
1213import net .tootallnate .websocket .drafts .*;
1314import net .tootallnate .websocket .exeptions .InvalidFrameException ;
1415import net .tootallnate .websocket .exeptions .InvalidHandshakeException ;
@@ -59,7 +60,7 @@ public enum Role{
5960 */
6061 public static final byte END_OF_FRAME = (byte )0xFF ;
6162
62- public static final boolean DEBUG = true ;
63+ public static final boolean DEBUG = false ;
6364
6465
6566 // INSTANCE PROPERTIES /////////////////////////////////////////////////////
@@ -102,6 +103,8 @@ public enum Role{
102103 private Handshakedata handshakerequest = null ;
103104
104105 public List <Draft > known_drafts ;
106+
107+ private final int maxpayloadsize ;
105108
106109
107110 // CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -115,24 +118,26 @@ public enum Role{
115118 * @param listener The {@link WebSocketListener} to notify of events when
116119 * they occur.
117120 */
118- WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , Draft draft ) {
121+ WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , Draft draft , int maxpayloadsize ) {
119122 this .socketChannel = socketChannel ;
120123 this .bufferQueue = bufferQueue ;
121124 this .handshakeComplete = false ;
122125 this .socketBuffer = ByteBuffer .allocate (8192 );
123126 this .wsl = listener ;
124127 this .role = Role .CLIENT ;
125128 this .draft = draft ;
129+ this .maxpayloadsize = maxpayloadsize ;
126130 }
127131
128- WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , List <Draft > drafts ) {
132+ WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue , WebSocketListener listener , List <Draft > drafts , int maxpayloadsize ) {
129133 this .socketChannel = socketChannel ;
130134 this .bufferQueue = bufferQueue ;
131135 this .handshakeComplete = false ;
132136 this .socketBuffer = ByteBuffer .allocate (8192 );
133137 this .wsl = listener ;
134138 this .role = Role .SERVER ;
135139 this .draft = null ;
140+ this .maxpayloadsize = maxpayloadsize ;
136141 if ( known_drafts == null || known_drafts .isEmpty () ){
137142 known_drafts = new ArrayList <Draft > ( 1 );
138143 known_drafts .add ( new Draft_10 () );
@@ -158,9 +163,14 @@ void handleRead() throws IOException, NoSuchAlgorithmException {
158163 bytesRead = this .socketChannel .read (this .socketBuffer );
159164
160165
161- if (bytesRead == -1 ) {
166+ if ( bytesRead == -1 ) {
162167 close ();
163- } else if (bytesRead > 0 ) {
168+ }
169+ else if ( bytesRead > maxpayloadsize ){
170+ wsl .onError ( new RuntimeException ("recived packet to big" ) );
171+ abort ( "recived packet to big" );
172+ }
173+ else if ( bytesRead > 0 ) {
164174 if (DEBUG ) System .out .println ( "got: {" + new String ( socketBuffer .array () , 0 , bytesRead ) + "}" );
165175 if ( !handshakeComplete ){
166176 try {
@@ -199,17 +209,38 @@ else if( role == Role.CLIENT){
199209 }
200210 }
201211 else {
202- List <Framedata > frames = draft .translateFrame ( socketBuffer . array () , bytesRead );
212+ List <Framedata > frames = draft .translateFrame ( socketBuffer , bytesRead );
203213 for ( Framedata f : frames ){
214+ Opcode curop = f .getOpcode ();
215+ if ( curop == null )// Ignore undefined opcodes
216+ continue ;
217+ else if ( curop == Opcode .CLOSING ){
218+ sendFrame ( new FramedataImpl1 ( Opcode .CLOSING ) );
219+ close ();
220+ }
221+ else if ( curop == Opcode .PING ){
222+ sendFrame ( new FramedataImpl1 ( Opcode .PONG ) );
223+ }
224+ else if ( curop == Opcode .PONG ){
225+ wsl .onPong ();
226+ }
204227 if ( currentframe == null ){
205228 if ( f .isFin () ){
206- wsl .onMessage ( this , new String ( f .getPayloadData () , UTF8_CHARSET ) );
229+ if ( f .getOpcode () == Opcode .TEXT ){
230+ wsl .onMessage ( this , new String ( f .getPayloadData () , UTF8_CHARSET ) );
231+ }
232+ else if ( f .getOpcode () == Opcode .BINARY ){
233+ wsl .onMessage ( this , f .getPayloadData () );
234+ }
235+ else {
236+ if (DEBUG ) System .out .println ( "Ignoring frame:" + f .toString () );
237+ }
207238 }
208239 else {
209240 currentframe = f ;
210241 }
211242 }
212- else {
243+ else if ( f . getOpcode () == Opcode . CONTINIOUS ) {
213244 try {
214245 currentframe .append ( f );
215246 } catch ( InvalidFrameException e ) {
0 commit comments