44import java .nio .channels .NotYetConnectedException ;
55import java .nio .channels .SocketChannel ;
66import java .nio .charset .Charset ;
7+ import java .security .NoSuchAlgorithmException ;
78
89/**
910 * Represents one end (client or server) of a single WebSocket connection.
@@ -37,7 +38,7 @@ final class WebSocket {
3738 * The byte representing the end of a WebSocket text frame.
3839 */
3940 public static final byte END_OF_FRAME = (byte )0xFF ;
40-
41+
4142
4243 // INSTANCE PROPERTIES /////////////////////////////////////////////////////
4344 /**
@@ -90,14 +91,14 @@ public WebSocket(SocketChannel socketChannel, WebSocketListener listener) {
9091 * Should be called when a Selector has a key that is writable for this
9192 * WebSocket's SocketChannel connection.
9293 * @throws IOException When socket related I/O errors occur.
94+ * @throws NoSuchAlgorithmException
9395 */
94- public void handleRead () throws IOException {
96+ public void handleRead () throws IOException , NoSuchAlgorithmException {
9597 this .buffer .rewind ();
9698 int bytesRead = -1 ;
9799 try {
98100 bytesRead = this .socketChannel .read (this .buffer );
99101 } catch (Exception ex ) {}
100-
101102 if (bytesRead == -1 )
102103 close ();
103104
@@ -168,7 +169,7 @@ private void recieveFrame() {
168169 }
169170 }
170171
171- private void recieveHandshake () throws IOException {
172+ private void recieveHandshake () throws IOException , NoSuchAlgorithmException {
172173 ByteBuffer ch = ByteBuffer .allocate ((this .remoteHandshake != null ? this .remoteHandshake .capacity () : 0 ) + this .buffer .capacity ());
173174 if (this .remoteHandshake != null ) {
174175 this .remoteHandshake .rewind ();
@@ -180,19 +181,36 @@ private void recieveHandshake() throws IOException {
180181 // If the ByteBuffer ends with 0x0D 0x0A 0x0D 0x0A
181182 // (or two CRLFs), then the client handshake is complete
182183 byte [] h = this .remoteHandshake .array ();
183- if ((h .length >=4 && h [h .length -4 ] == CR
184- && h [h .length -3 ] == LF
185- && h [h .length -2 ] == CR
186- && h [h .length -1 ] == LF ) ||
187- (h .length ==23 && h [h .length -1 ] == 0 )) {
188- completeHandshake ();
189- }
184+ if ((h .length >=12 && h [h .length -12 ] == CR
185+ && h [h .length -11 ] == LF
186+ && h [h .length -10 ] == CR
187+ && h [h .length -9 ] == LF )) {
188+ byte [] key3 = new byte [8 ];
189+ key3 [0 ]=h [h .length -8 ];
190+ key3 [1 ]=h [h .length -7 ];
191+ key3 [2 ]=h [h .length -6 ];
192+ key3 [3 ]=h [h .length -5 ];
193+ key3 [4 ]=h [h .length -4 ];
194+ key3 [5 ]=h [h .length -3 ];
195+ key3 [6 ]=h [h .length -2 ];
196+ key3 [7 ]=h [h .length -1 ];
197+ completeHandshake (key3 );
198+ }
199+ else if ((h .length >=4 && h [h .length -4 ] == CR
200+ && h [h .length -3 ] == LF
201+ && h [h .length -2 ] == CR
202+ && h [h .length -1 ] == LF ) && !(new String (this .remoteHandshake .array (),UTF8_CHARSET ).contains ("Sec" )) ||
203+ (h .length ==23 && h [h .length -1 ] == 0 ) ) {
204+
205+ completeHandshake (null );
206+ }
190207 }
191208
192- private void completeHandshake () throws IOException {
193- String handshake = new String (this .remoteHandshake .array (), UTF8_CHARSET );
209+ private void completeHandshake (byte [] key3 ) throws IOException , NoSuchAlgorithmException {
210+ byte [] handshakeBytes =this .remoteHandshake .array ();
211+ String handshake = new String (handshakeBytes , UTF8_CHARSET );
194212 this .handshakeComplete = true ;
195- if (this .wsl .onHandshakeRecieved (this , handshake )) {
213+ if (this .wsl .onHandshakeRecieved (this ,handshake , key3 )) {
196214 this .wsl .onOpen (this );
197215 } else {
198216 close ();
0 commit comments