Skip to content

Commit 108dfad

Browse files
julianjulian
authored andcommitted
Add Support for Draft 76 and to Start Server in different Draft modes
1 parent 3a2e23b commit 108dfad

File tree

6 files changed

+163
-38
lines changed

6 files changed

+163
-38
lines changed

example/ChatClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@ public void run() {
126126
}
127127
});
128128
}
129+
130+
@Override
131+
public Draft getDraft() {
132+
// TODO Auto-generated method stub
133+
return null;
134+
}
129135
}

example/ChatServer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class ChatServer extends WebSocketServer {
88

99
public ChatServer(int port) {
10-
super(port);
10+
super(port,Draft.AUTO);
1111
}
1212

1313
public void onClientOpen(WebSocket conn) {
@@ -17,6 +17,7 @@ public void onClientOpen(WebSocket conn) {
1717
ex.printStackTrace();
1818
}
1919
System.out.println(conn + " entered the room!");
20+
2021
}
2122

2223
public void onClientClose(WebSocket conn) {
@@ -38,7 +39,7 @@ public void onClientMessage(WebSocket conn, String message) {
3839
}
3940

4041
public static void main(String[] args) {
41-
int port = 80;
42+
int port = 88;
4243
try {
4344
port = Integer.parseInt(args[0]);
4445
} catch(Exception ex) {}

src/WebSocket.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.channels.NotYetConnectedException;
55
import java.nio.channels.SocketChannel;
66
import 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();

src/WebSocketClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.channels.SelectionKey;
77
import java.nio.channels.Selector;
88
import java.nio.channels.SocketChannel;
9+
import java.security.NoSuchAlgorithmException;
910
import java.util.Iterator;
1011
import java.util.Set;
1112

@@ -155,7 +156,10 @@ public void run() {
155156

156157
} catch (IOException ex) {
157158
ex.printStackTrace();
158-
}
159+
} catch (NoSuchAlgorithmException e) {
160+
// TODO Auto-generated catch block
161+
e.printStackTrace();
162+
}
159163
}
160164

161165

@@ -170,7 +174,7 @@ public void run() {
170174
* handshake, <var>false</var> otherwise.
171175
* @throws IOException When socket related I/O errors occur.
172176
*/
173-
public boolean onHandshakeRecieved(WebSocket conn, String handshake) throws IOException {
177+
public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] key3) throws IOException {
174178
// TODO: Do some parsing of the returned handshake, and close connection
175179
// (return false) if we recieved anything unexpected.
176180
return true;

src/WebSocketListener.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11

22
import java.io.IOException;
3+
import java.security.NoSuchAlgorithmException;
34

45
/**
56
* Implemented by <tt>WebSocketClient</tt> and <tt>WebSocketServer</tt>.
67
* The methods within are called by <tt>WebSocket</tt>.
78
* @author Nathan Rajlich
89
*/
910
interface WebSocketListener {
11+
/**
12+
* Enum for WebSocket Draft
13+
*/
14+
public static enum Draft
15+
{
16+
AUTO,
17+
DRAFT75,
18+
DRAFT76
19+
}
1020
/**
1121
* Called when the socket connection is first established, and the WebSocket
1222
* handshake has been recieved. This method should parse the
@@ -17,8 +27,9 @@ interface WebSocketListener {
1727
* @return <var>true</var> if the handshake is valid, and <var>onOpen</var>
1828
* should be immediately called afterwards. <var>false</var> if the
1929
* handshake was invalid, and the connection should be terminated.
30+
* @throws NoSuchAlgorithmException
2031
*/
21-
public boolean onHandshakeRecieved(WebSocket conn, String handshake) throws IOException;
32+
public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] key3) throws IOException, NoSuchAlgorithmException;
2233
/**
2334
* Called when an entire text frame has been recieved. Do whatever you want
2435
* here...
@@ -39,4 +50,9 @@ interface WebSocketListener {
3950
* @param conn The <tt>WebSocket</tt> instance this event is occuring on.
4051
*/
4152
public void onClose(WebSocket conn);
53+
/**
54+
* Called to retrive the Draft of this listener.
55+
*/
56+
public Draft getDraft();
57+
4258
}

0 commit comments

Comments
 (0)