Merge pull request #127 from veebs/WebSocketVersion3.2

Format source
This commit is contained in:
Vibul Imtarnasan 2011-12-14 21:57:33 -08:00
commit 8145ad96f6
41 changed files with 1524 additions and 1546 deletions

View File

@ -25,8 +25,8 @@ import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
* A Web Socket echo server for running the <a href="http://www.tavendo.de/autobahn/testsuite.html">autobahn</a> * A Web Socket echo server for running the <a href="http://www.tavendo.de/autobahn/testsuite.html">autobahn</a> test
* test suite * suite
*/ */
public class WebSocketServer { public class WebSocketServer {
public static void main(String[] args) { public static void main(String[] args) {
@ -39,7 +39,7 @@ public class WebSocketServer {
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
//bootstrap.setOption("child.tcpNoDelay", true); // bootstrap.setOption("child.tcpNoDelay", true);
// Set up the event pipeline factory. // Set up the event pipeline factory.
bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory()); bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());

View File

@ -90,8 +90,9 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
ctx.getChannel().write( ctx.getChannel().write(
new PongWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData())); new PongWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
} else if (frame instanceof TextWebSocketFrame) { } else if (frame instanceof TextWebSocketFrame) {
//String text = ((TextWebSocketFrame) frame).getText(); // String text = ((TextWebSocketFrame) frame).getText();
ctx.getChannel().write(new TextWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData())); ctx.getChannel().write(
new TextWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
} else if (frame instanceof BinaryWebSocketFrame) { } else if (frame instanceof BinaryWebSocketFrame) {
ctx.getChannel().write( ctx.getChannel().write(
new BinaryWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData())); new BinaryWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));

View File

@ -24,7 +24,6 @@ package org.jboss.netty.example.http.websocketx.client;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame; import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
/** /**
* Copied from https://github.com/cgbystrom/netty-tools * Copied from https://github.com/cgbystrom/netty-tools
* *

View File

@ -38,8 +38,8 @@ import java.util.concurrent.Executors;
/** /**
* Copied from https://github.com/cgbystrom/netty-tools * Copied from https://github.com/cgbystrom/netty-tools
* *
* A factory for creating WebSocket clients. The entry point for creating and * A factory for creating WebSocket clients. The entry point for creating and connecting a client. Can and should be
* connecting a client. Can and should be used to create multiple instances. * used to create multiple instances.
*/ */
public class WebSocketClientFactory { public class WebSocketClientFactory {
@ -57,8 +57,7 @@ public class WebSocketClientFactory {
* Callback interface to receive events * Callback interface to receive events
* @param customHeaders * @param customHeaders
* Map of custom headers to add to the client request * Map of custom headers to add to the client request
* @return A WebSocket client. Call {@link WebSocketClient#connect()} to * @return A WebSocket client. Call {@link WebSocketClient#connect()} to connect.
* connect.
*/ */
public WebSocketClient newClient(final URI url, final WebSocketVersion version, final WebSocketCallback callback, public WebSocketClient newClient(final URI url, final WebSocketVersion version, final WebSocketCallback callback,
final Map<String, String> customHeaders) { final Map<String, String> customHeaders) {

View File

@ -44,9 +44,8 @@ import org.jboss.netty.util.CharsetUtil;
/** /**
* Copied from https://github.com/cgbystrom/netty-tools * Copied from https://github.com/cgbystrom/netty-tools
* *
* Handles socket communication for a connected WebSocket client Not intended * Handles socket communication for a connected WebSocket client Not intended for end-users. Please use
* for end-users. Please use {@link WebSocketClient} or * {@link WebSocketClient} or {@link WebSocketCallback} for controlling your client.
* {@link WebSocketCallback} for controlling your client.
*/ */
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler implements WebSocketClient { public class WebSocketClientHandler extends SimpleChannelUpstreamHandler implements WebSocketClient {

View File

@ -29,11 +29,10 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
* *
* http://localhost:8080/websocket * http://localhost:8080/websocket
* *
* Open your browser at http://localhost:8080/, then the demo page will be * Open your browser at http://localhost:8080/, then the demo page will be loaded and a Web Socket connection will be
* loaded and a Web Socket connection will be made automatically. * made automatically.
* *
* This server illustrates support for the different web socket specification * This server illustrates support for the different web socket specification versions and will work with:
* versions and will work with:
* *
* <ul> * <ul>
* <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00) * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
@ -63,6 +62,7 @@ public class WebSocketServer {
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));
System.out.println("Web Socket Server started on 8080. Open your browser and navigate to http://localhost:8080/"); System.out
.println("Web Socket Server started on 8080. Open your browser and navigate to http://localhost:8080/");
} }
} }

View File

@ -19,7 +19,6 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.util.CharsetUtil; import org.jboss.netty.util.CharsetUtil;
/** /**
* Generates the demo HTML page which is served at http://localhost:8080/ * Generates the demo HTML page which is served at http://localhost:8080/
*/ */
@ -28,40 +27,66 @@ public class WebSocketServerIndexPage {
private static final String NEWLINE = "\r\n"; private static final String NEWLINE = "\r\n";
public static ChannelBuffer getContent(String webSocketLocation) { public static ChannelBuffer getContent(String webSocketLocation) {
return ChannelBuffers.copiedBuffer( return ChannelBuffers
"<html><head><title>Web Socket Test</title></head>" + NEWLINE + .copiedBuffer(
"<body>" + NEWLINE + "<html><head><title>Web Socket Test</title></head>"
"<script type=\"text/javascript\">" + NEWLINE + + NEWLINE
"var socket;" + NEWLINE + + "<body>"
"if (!window.WebSocket) {" + NEWLINE + + NEWLINE
" window.WebSocket = window.MozWebSocket;" + NEWLINE + + "<script type=\"text/javascript\">"
"}" + NEWLINE + + NEWLINE
"if (window.WebSocket) {" + NEWLINE + + "var socket;"
" socket = new WebSocket(\"" + webSocketLocation + "\");" + NEWLINE + + NEWLINE
" socket.onmessage = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + '\\n' + event.data };" + NEWLINE + + "if (!window.WebSocket) {"
" socket.onopen = function(event) { var ta = document.getElementById('responseText'); ta.value = \"Web Socket opened!\"; };" + NEWLINE + + NEWLINE
" socket.onclose = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + \"Web Socket closed\"; };" + NEWLINE + + " window.WebSocket = window.MozWebSocket;"
"} else {" + NEWLINE + + NEWLINE
" alert(\"Your browser does not support Web Socket.\");" + NEWLINE + + "}"
"}" + NEWLINE + + NEWLINE
NEWLINE + + "if (window.WebSocket) {"
"function send(message) {" + NEWLINE + + NEWLINE
" if (!window.WebSocket) { return; }" + NEWLINE + + " socket = new WebSocket(\""
" if (socket.readyState == WebSocket.OPEN) {" + NEWLINE + + webSocketLocation
" socket.send(message);" + NEWLINE + + "\");"
" } else {" + NEWLINE + + NEWLINE
" alert(\"The socket is not open.\");" + NEWLINE + + " socket.onmessage = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + '\\n' + event.data };"
" }" + NEWLINE + + NEWLINE
"}" + NEWLINE + + " socket.onopen = function(event) { var ta = document.getElementById('responseText'); ta.value = \"Web Socket opened!\"; };"
"</script>" + NEWLINE + + NEWLINE
"<form onsubmit=\"return false;\">" + NEWLINE + + " socket.onclose = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + \"Web Socket closed\"; };"
"<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>" + + NEWLINE
"<input type=\"button\" value=\"Send Web Socket Data\" onclick=\"send(this.form.message.value)\" />" + NEWLINE + + "} else {"
"<h3>Output</h3>" + NEWLINE + + NEWLINE
"<textarea id=\"responseText\" style=\"width: 500px; height:300px;\"></textarea>" + NEWLINE + + " alert(\"Your browser does not support Web Socket.\");"
"</form>" + NEWLINE + + NEWLINE
"</body>" + NEWLINE + + "}"
"</html>" + NEWLINE, + NEWLINE
+ NEWLINE
+ "function send(message) {"
+ NEWLINE
+ " if (!window.WebSocket) { return; }"
+ NEWLINE
+ " if (socket.readyState == WebSocket.OPEN) {"
+ NEWLINE
+ " socket.send(message);"
+ NEWLINE
+ " } else {"
+ NEWLINE
+ " alert(\"The socket is not open.\");"
+ NEWLINE
+ " }"
+ NEWLINE
+ "}"
+ NEWLINE
+ "</script>"
+ NEWLINE
+ "<form onsubmit=\"return false;\">"
+ NEWLINE
+ "<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>"
+ "<input type=\"button\" value=\"Send Web Socket Data\" onclick=\"send(this.form.message.value)\" />"
+ NEWLINE + "<h3>Output</h3>" + NEWLINE
+ "<textarea id=\"responseText\" style=\"width: 500px; height:300px;\"></textarea>"
+ NEWLINE + "</form>" + NEWLINE + "</body>" + NEWLINE + "</html>" + NEWLINE,
CharsetUtil.US_ASCII); CharsetUtil.US_ASCII);
} }
} }

View File

@ -29,11 +29,10 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
* *
* https://localhost:8081/websocket * https://localhost:8081/websocket
* *
* Open your browser at https://localhost:8081/, then the demo page will be * Open your browser at https://localhost:8081/, then the demo page will be loaded and a Web Socket connection will be
* loaded and a Web Socket connection will be made automatically. * made automatically.
* *
* This server illustrates support for the different web socket specification * This server illustrates support for the different web socket specification versions and will work with:
* versions and will work with:
* *
* <ul> * <ul>
* <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00) * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
@ -75,6 +74,7 @@ public class WebSocketSslServer {
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8081)); bootstrap.bind(new InetSocketAddress(8081));
System.out.println("Web Socket Server started on 8081. Open your browser and navigate to https://localhost:8081/"); System.out
.println("Web Socket Server started on 8081. Open your browser and navigate to https://localhost:8081/");
} }
} }

View File

@ -19,7 +19,6 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.util.CharsetUtil; import org.jboss.netty.util.CharsetUtil;
/** /**
* Generates the demo HTML page which is served at http://localhost:8080/ * Generates the demo HTML page which is served at http://localhost:8080/
*/ */
@ -28,40 +27,66 @@ public class WebSocketSslServerIndexPage {
private static final String NEWLINE = "\r\n"; private static final String NEWLINE = "\r\n";
public static ChannelBuffer getContent(String webSocketLocation) { public static ChannelBuffer getContent(String webSocketLocation) {
return ChannelBuffers.copiedBuffer( return ChannelBuffers
"<html><head><title>Web Socket Test</title></head>" + NEWLINE + .copiedBuffer(
"<body>" + NEWLINE + "<html><head><title>Web Socket Test</title></head>"
"<script type=\"text/javascript\">" + NEWLINE + + NEWLINE
"var socket;" + NEWLINE + + "<body>"
"if (!window.WebSocket) {" + NEWLINE + + NEWLINE
" window.WebSocket = window.MozWebSocket;" + NEWLINE + + "<script type=\"text/javascript\">"
"}" + NEWLINE + + NEWLINE
"if (window.WebSocket) {" + NEWLINE + + "var socket;"
" socket = new WebSocket(\"" + webSocketLocation + "\");" + NEWLINE + + NEWLINE
" socket.onmessage = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + '\\n' + event.data };" + NEWLINE + + "if (!window.WebSocket) {"
" socket.onopen = function(event) { var ta = document.getElementById('responseText'); ta.value = \"Web Socket opened!\"; };" + NEWLINE + + NEWLINE
" socket.onclose = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + \"Web Socket closed\"; };" + NEWLINE + + " window.WebSocket = window.MozWebSocket;"
"} else {" + NEWLINE + + NEWLINE
" alert(\"Your browser does not support Web Socket.\");" + NEWLINE + + "}"
"}" + NEWLINE + + NEWLINE
NEWLINE + + "if (window.WebSocket) {"
"function send(message) {" + NEWLINE + + NEWLINE
" if (!window.WebSocket) { return; }" + NEWLINE + + " socket = new WebSocket(\""
" if (socket.readyState == WebSocket.OPEN) {" + NEWLINE + + webSocketLocation
" socket.send(message);" + NEWLINE + + "\");"
" } else {" + NEWLINE + + NEWLINE
" alert(\"The socket is not open.\");" + NEWLINE + + " socket.onmessage = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + '\\n' + event.data };"
" }" + NEWLINE + + NEWLINE
"}" + NEWLINE + + " socket.onopen = function(event) { var ta = document.getElementById('responseText'); ta.value = \"Web Socket opened!\"; };"
"</script>" + NEWLINE + + NEWLINE
"<form onsubmit=\"return false;\">" + NEWLINE + + " socket.onclose = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + \"Web Socket closed\"; };"
"<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>" + + NEWLINE
"<input type=\"button\" value=\"Send Web Socket Data\" onclick=\"send(this.form.message.value)\" />" + NEWLINE + + "} else {"
"<h3>Output</h3>" + NEWLINE + + NEWLINE
"<textarea id=\"responseText\" style=\"width: 500px; height:300px;\"></textarea>" + NEWLINE + + " alert(\"Your browser does not support Web Socket.\");"
"</form>" + NEWLINE + + NEWLINE
"</body>" + NEWLINE + + "}"
"</html>" + NEWLINE, + NEWLINE
+ NEWLINE
+ "function send(message) {"
+ NEWLINE
+ " if (!window.WebSocket) { return; }"
+ NEWLINE
+ " if (socket.readyState == WebSocket.OPEN) {"
+ NEWLINE
+ " socket.send(message);"
+ NEWLINE
+ " } else {"
+ NEWLINE
+ " alert(\"The socket is not open.\");"
+ NEWLINE
+ " }"
+ NEWLINE
+ "}"
+ NEWLINE
+ "</script>"
+ NEWLINE
+ "<form onsubmit=\"return false;\">"
+ NEWLINE
+ "<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>"
+ "<input type=\"button\" value=\"Send Web Socket Data\" onclick=\"send(this.form.message.value)\" />"
+ NEWLINE + "<h3>Output</h3>" + NEWLINE
+ "<textarea id=\"responseText\" style=\"width: 500px; height:300px;\"></textarea>"
+ NEWLINE + "</form>" + NEWLINE + "</body>" + NEWLINE + "</html>" + NEWLINE,
CharsetUtil.US_ASCII); CharsetUtil.US_ASCII);
} }
} }

View File

@ -42,9 +42,8 @@ public class WebSocketSslServerSslContext {
} }
/** /**
* SingletonHolder is loaded on the first execution of * SingletonHolder is loaded on the first execution of Singleton.getInstance() or the first access to
* Singleton.getInstance() or the first access to SingletonHolder.INSTANCE, * SingletonHolder.INSTANCE, not before.
* not before.
* *
* See http://en.wikipedia.org/wiki/Singleton_pattern * See http://en.wikipedia.org/wiki/Singleton_pattern
*/ */

View File

@ -31,8 +31,7 @@ public class BinaryWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new binary frame with the specified binary data. The final * Creates a new binary frame with the specified binary data. The final fragment flag is set to true.
* fragment flag is set to true.
* *
* @param binaryData * @param binaryData
* the content of the frame. * the content of the frame.
@ -42,8 +41,7 @@ public class BinaryWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new binary frame with the specified binary data and the final * Creates a new binary frame with the specified binary data and the final fragment flag.
* fragment flag.
* *
* @param finalFragment * @param finalFragment
* flag indicating if this frame is the final fragment * flag indicating if this frame is the final fragment

View File

@ -20,9 +20,8 @@ import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.util.CharsetUtil; import org.jboss.netty.util.CharsetUtil;
/** /**
* Web Socket continuation frame containing continuation text or binary data. * Web Socket continuation frame containing continuation text or binary data. This is used for fragmented messages where
* This is used for fragmented messages where the contents of a messages is * the contents of a messages is contained more than 1 frame.
* contained more than 1 frame.
*/ */
public class ContinuationWebSocketFrame extends WebSocketFrame { public class ContinuationWebSocketFrame extends WebSocketFrame {
@ -36,8 +35,7 @@ public class ContinuationWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new continuation frame with the specified binary data. The * Creates a new continuation frame with the specified binary data. The final fragment flag is set to true.
* final fragment flag is set to true.
* *
* @param binaryData * @param binaryData
* the content of the frame. * the content of the frame.
@ -72,8 +70,7 @@ public class ContinuationWebSocketFrame extends WebSocketFrame {
* @param binaryData * @param binaryData
* the content of the frame. * the content of the frame.
* @param aggregatedText * @param aggregatedText
* Aggregated text set by decoder on the final continuation frame * Aggregated text set by decoder on the final continuation frame of a fragmented text message
* of a fragmented text message
*/ */
public ContinuationWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData, String aggregatedText) { public ContinuationWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData, String aggregatedText) {
this.setFinalFragment(finalFragment); this.setFinalFragment(finalFragment);
@ -128,8 +125,7 @@ public class ContinuationWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Aggregated text returned by decoder on the final continuation frame of a * Aggregated text returned by decoder on the final continuation frame of a fragmented text message
* fragmented text message
*/ */
public String getAggregatedText() { public String getAggregatedText() {
return aggregatedText; return aggregatedText;

View File

@ -32,8 +32,7 @@ public class TextWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new text frame with the specified text string. The final * Creates a new text frame with the specified text string. The final fragment flag is set to true.
* fragment flag is set to true.
* *
* @param text * @param text
* String to put in the frame * String to put in the frame
@ -47,8 +46,7 @@ public class TextWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new text frame with the specified binary data. The final * Creates a new text frame with the specified binary data. The final fragment flag is set to true.
* fragment flag is set to true.
* *
* @param binaryData * @param binaryData
* the content of the frame. Must be UTF-8 encoded * the content of the frame. Must be UTF-8 encoded
@ -58,8 +56,7 @@ public class TextWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new text frame with the specified text string. The final * Creates a new text frame with the specified text string. The final fragment flag is set to true.
* fragment flag is set to true.
* *
* @param finalFragment * @param finalFragment
* flag indicating if this frame is the final fragment * flag indicating if this frame is the final fragment
@ -79,8 +76,7 @@ public class TextWebSocketFrame extends WebSocketFrame {
} }
/** /**
* Creates a new text frame with the specified binary data. The final * Creates a new text frame with the specified binary data. The final fragment flag is set to true.
* fragment flag is set to true.
* *
* @param finalFragment * @param finalFragment
* flag indicating if this frame is the final fragment * flag indicating if this frame is the final fragment

View File

@ -26,12 +26,20 @@ final class UTF8Output {
private static final int UTF8_ACCEPT = 0; private static final int UTF8_ACCEPT = 0;
private static final int UTF8_REJECT = 12; private static final int UTF8_REJECT = 12;
private static final byte[] TYPES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, private static final byte[] TYPES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11, 6, 6, 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11,
6, 6, 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 };
private static final byte[] STATES = { 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0, 12, 12, 12, 12, 12, 0, 12, 0, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 12, 12, private static final byte[] STATES = { 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12 }; 12, 12, 12, 12, 12, 12, 0, 12, 12, 12, 12, 12, 0, 12, 0, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 24,
12, 12, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12,
12, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12 };
private int state = UTF8_ACCEPT; private int state = UTF8_ACCEPT;
private int codep = 0; private int codep = 0;

View File

@ -25,9 +25,9 @@ import org.jboss.netty.handler.codec.replay.VoidEnum;
/** /**
* Decodes {@link ChannelBuffer}s into {@link WebSocketFrame}s. * Decodes {@link ChannelBuffer}s into {@link WebSocketFrame}s.
* <p> * <p>
* For the detailed instruction on adding add Web Socket support to your HTTP * For the detailed instruction on adding add Web Socket support to your HTTP server, take a look into the
* server, take a look into the <tt>WebSocketServer</tt> example located in the * <tt>WebSocketServer</tt> example located in the {@code org.jboss.netty.example.http.websocket} package.
* {@code org.jboss.netty.example.http.websocket} package. *
* @apiviz.landmark * @apiviz.landmark
* @apiviz.uses org.jboss.netty.handler.codec.http.websocket.WebSocketFrame * @apiviz.uses org.jboss.netty.handler.codec.http.websocket.WebSocketFrame
*/ */
@ -43,9 +43,8 @@ public class WebSocket00FrameDecoder extends ReplayingDecoder<VoidEnum> {
} }
/** /**
* Creates a new instance of {@code WebSocketFrameDecoder} with the * Creates a new instance of {@code WebSocketFrameDecoder} with the specified {@code maxFrameSize}. If the client
* specified {@code maxFrameSize}. If the client sends a frame size larger * sends a frame size larger than {@code maxFrameSize}, the channel will be closed.
* than {@code maxFrameSize}, the channel will be closed.
* *
* @param maxFrameSize * @param maxFrameSize
* the maximum frame size to decode * the maximum frame size to decode
@ -55,7 +54,8 @@ public class WebSocket00FrameDecoder extends ReplayingDecoder<VoidEnum> {
} }
@Override @Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, VoidEnum state) throws Exception { protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, VoidEnum state)
throws Exception {
// Discard all data received if closing handshake was received before. // Discard all data received if closing handshake was received before.
if (receivedClosingHandshake) { if (receivedClosingHandshake) {

View File

@ -24,9 +24,9 @@ import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
/** /**
* Encodes a {@link WebSocketFrame} into a {@link ChannelBuffer}. * Encodes a {@link WebSocketFrame} into a {@link ChannelBuffer}.
* <p> * <p>
* For the detailed instruction on adding add Web Socket support to your HTTP * For the detailed instruction on adding add Web Socket support to your HTTP server, take a look into the
* server, take a look into the <tt>WebSocketServer</tt> example located in the * <tt>WebSocketServer</tt> example located in the {@code org.jboss.netty.example.http.websocket} package.
* {@code org.jboss.netty.example.http.websocket} package. *
* @apiviz.landmark * @apiviz.landmark
* @apiviz.uses org.jboss.netty.handler.codec.http.websocket.WebSocketFrame * @apiviz.uses org.jboss.netty.handler.codec.http.websocket.WebSocketFrame
*/ */
@ -40,7 +40,8 @@ public class WebSocket00FrameEncoder extends OneToOneEncoder {
if (frame instanceof TextWebSocketFrame) { if (frame instanceof TextWebSocketFrame) {
// Text frame // Text frame
ChannelBuffer data = frame.getBinaryData(); ChannelBuffer data = frame.getBinaryData();
ChannelBuffer encoded = channel.getConfig().getBufferFactory().getBuffer(data.order(), data.readableBytes() + 2); ChannelBuffer encoded = channel.getConfig().getBufferFactory()
.getBuffer(data.order(), data.readableBytes() + 2);
encoded.writeByte((byte) 0x00); encoded.writeByte((byte) 0x00);
encoded.writeBytes(data, data.readerIndex(), data.readableBytes()); encoded.writeBytes(data, data.readerIndex(), data.readableBytes());
encoded.writeByte((byte) 0xFF); encoded.writeByte((byte) 0xFF);

View File

@ -50,9 +50,8 @@ import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory; import org.jboss.netty.logging.InternalLoggerFactory;
/** /**
* Decodes a web socket frame from wire protocol version 8 format. This code was * Decodes a web socket frame from wire protocol version 8 format. This code was forked from <a
* forked from <a href="https://github.com/joewalnes/webbit">webbit</a> and * href="https://github.com/joewalnes/webbit">webbit</a> and modified.
* modified.
*/ */
public class WebSocket08FrameDecoder extends ReplayingDecoder<WebSocket08FrameDecoder.State> { public class WebSocket08FrameDecoder extends ReplayingDecoder<WebSocket08FrameDecoder.State> {
@ -88,8 +87,8 @@ public class WebSocket08FrameDecoder extends ReplayingDecoder<WebSocket08FrameDe
* Constructor * Constructor
* *
* @param maskedPayload * @param maskedPayload
* Web socket servers must set this to true processed incoming * Web socket servers must set this to true processed incoming masked payload. Client implementations
* masked payload. Client implementations must set this to false. * must set this to false.
* @param allowExtensions * @param allowExtensions
* Flag to allow reserved extension bits to be used or not * Flag to allow reserved extension bits to be used or not
*/ */

View File

@ -51,9 +51,8 @@ import org.jboss.netty.logging.InternalLoggerFactory;
/** /**
* <p> * <p>
* Encodes a web socket frame into wire protocol version 8 format. This code was * Encodes a web socket frame into wire protocol version 8 format. This code was forked from <a
* forked from <a href="https://github.com/joewalnes/webbit">webbit</a> and * href="https://github.com/joewalnes/webbit">webbit</a> and modified.
* modified.
* </p> * </p>
*/ */
public class WebSocket08FrameEncoder extends OneToOneEncoder { public class WebSocket08FrameEncoder extends OneToOneEncoder {
@ -73,8 +72,8 @@ public class WebSocket08FrameEncoder extends OneToOneEncoder {
* Constructor * Constructor
* *
* @param maskPayload * @param maskPayload
* Web socket clients must set this to true to mask payload. * Web socket clients must set this to true to mask payload. Server implementations must set this to
* Server implementations must set this to false. * false.
*/ */
public WebSocket08FrameEncoder(boolean maskPayload) { public WebSocket08FrameEncoder(boolean maskPayload) {
this.maskPayload = maskPayload; this.maskPayload = maskPayload;
@ -126,7 +125,8 @@ public class WebSocket08FrameEncoder extends OneToOneEncoder {
ChannelBuffer body; ChannelBuffer body;
if (opcode == OPCODE_PING && length > 125) { if (opcode == OPCODE_PING && length > 125) {
throw new TooLongFrameException("invalid payload for PING (payload length must be <= 125, was " + length); throw new TooLongFrameException("invalid payload for PING (payload length must be <= 125, was "
+ length);
} }
int maskLength = this.maskPayload ? 4 : 0; int maskLength = this.maskPayload ? 4 : 0;

View File

@ -39,8 +39,7 @@
package org.jboss.netty.handler.codec.http.websocketx; package org.jboss.netty.handler.codec.http.websocketx;
/** /**
* Decodes a web socket frame from wire protocol version 13 format. * Decodes a web socket frame from wire protocol version 13 format. V13 is essentially the same as V8.
* V13 is essentially the same as V8.
*/ */
public class WebSocket13FrameDecoder extends WebSocket08FrameDecoder { public class WebSocket13FrameDecoder extends WebSocket08FrameDecoder {
@ -48,8 +47,8 @@ public class WebSocket13FrameDecoder extends WebSocket08FrameDecoder {
* Constructor * Constructor
* *
* @param maskedPayload * @param maskedPayload
* Web socket servers must set this to true processed incoming * Web socket servers must set this to true processed incoming masked payload. Client implementations
* masked payload. Client implementations must set this to false. * must set this to false.
* @param allowExtensions * @param allowExtensions
* Flag to allow reserved extension bits to be used or not * Flag to allow reserved extension bits to be used or not
*/ */

View File

@ -38,11 +38,9 @@
package org.jboss.netty.handler.codec.http.websocketx; package org.jboss.netty.handler.codec.http.websocketx;
/** /**
* <p> * <p>
* Encodes a web socket frame into wire protocol version 13 format. V13 is essentially the same * Encodes a web socket frame into wire protocol version 13 format. V13 is essentially the same as V8.
* as V8.
* </p> * </p>
*/ */
public class WebSocket13FrameEncoder extends WebSocket08FrameEncoder { public class WebSocket13FrameEncoder extends WebSocket08FrameEncoder {
@ -51,8 +49,8 @@ public class WebSocket13FrameEncoder extends WebSocket08FrameEncoder {
* Constructor * Constructor
* *
* @param maskPayload * @param maskPayload
* Web socket clients must set this to true to mask payload. * Web socket clients must set this to true to mask payload. Server implementations must set this to
* Server implementations must set this to false. * false.
*/ */
public WebSocket13FrameEncoder(boolean maskPayload) { public WebSocket13FrameEncoder(boolean maskPayload) {
super(maskPayload); super(maskPayload);

View File

@ -48,12 +48,10 @@ public abstract class WebSocketClientHandshaker {
* Base constructor * Base constructor
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param version * @param version
* Version of web socket specification to use to connect to the * Version of web socket specification to use to connect to the server
* server
* @param subProtocol * @param subProtocol
* Sub protocol request sent to the server. * Sub protocol request sent to the server.
* @param customHeaders * @param customHeaders
@ -101,8 +99,7 @@ public abstract class WebSocketClientHandshaker {
} }
/** /**
* Returns the sub protocol request sent to the server as specified in the * Returns the sub protocol request sent to the server as specified in the constructor
* constructor
*/ */
public String getSubProtocolRequest() { public String getSubProtocolRequest() {
return subProtocolRequest; return subProtocolRequest;
@ -113,8 +110,7 @@ public abstract class WebSocketClientHandshaker {
} }
/** /**
* Returns the sub protocol response and sent by the server. Only available * Returns the sub protocol response and sent by the server. Only available after end of handshake.
* after end of handshake.
*/ */
public String getSubProtocolResponse() { public String getSubProtocolResponse() {
return subProtocolResponse; return subProtocolResponse;

View File

@ -35,10 +35,9 @@ import org.jboss.netty.handler.codec.http.HttpVersion;
/** /**
* <p> * <p>
* Performs client side opening and closing handshakes for web socket * Performs client side opening and closing handshakes for web socket specification version <a
* specification version <a * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00" >draft-ietf-hybi-thewebsocketprotocol-
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00" * 00</a>
* >draft-ietf-hybi-thewebsocketprotocol- 00</a>
* </p> * </p>
* <p> * <p>
* A very large portion of this code was taken from the Netty 3.2 HTTP example. * A very large portion of this code was taken from the Netty 3.2 HTTP example.
@ -49,16 +48,13 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
private byte[] expectedChallengeResponseBytes = null; private byte[] expectedChallengeResponseBytes = null;
/** /**
* Constructor specifying the destination web socket location and version to * Constructor specifying the destination web socket location and version to initiate
* initiate
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param version * @param version
* Version of web socket specification to use to connect to the * Version of web socket specification to use to connect to the server
* server
* @param subProtocol * @param subProtocol
* Sub protocol request sent to the server. * Sub protocol request sent to the server.
* @param customHeaders * @param customHeaders
@ -177,8 +173,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
* @param channel * @param channel
* Channel * Channel
* @param response * @param response
* HTTP response returned from the server for the request sent by * HTTP response returned from the server for the request sent by beginOpeningHandshake00().
* beginOpeningHandshake00().
* @throws WebSocketHandshakeException * @throws WebSocketHandshakeException
*/ */
@Override @Override

View File

@ -35,10 +35,9 @@ import org.jboss.netty.util.CharsetUtil;
/** /**
* <p> * <p>
* Performs client side opening and closing handshakes for web socket * Performs client side opening and closing handshakes for web socket specification version <a
* specification version <a * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" >draft-ietf-hybi-thewebsocketprotocol-
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" * 10</a>
* >draft-ietf-hybi-thewebsocketprotocol- 10</a>
* </p> * </p>
*/ */
public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
@ -54,21 +53,17 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
private boolean allowExtensions = false; private boolean allowExtensions = false;
/** /**
* Constructor specifying the destination web socket location and version to * Constructor specifying the destination web socket location and version to initiate
* initiate
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param version * @param version
* Version of web socket specification to use to connect to the * Version of web socket specification to use to connect to the server
* server
* @param subProtocol * @param subProtocol
* Sub protocol request sent to the server. * Sub protocol request sent to the server.
* @param allowExtensions * @param allowExtensions
* Allow extensions to be used in the reserved bits of the web * Allow extensions to be used in the reserved bits of the web socket frame
* socket frame
* @param customHeaders * @param customHeaders
* Map of custom headers to add to the client request * Map of custom headers to add to the client request
*/ */
@ -158,8 +153,7 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
* @param channel * @param channel
* Channel * Channel
* @param response * @param response
* HTTP response returned from the server for the request sent by * HTTP response returned from the server for the request sent by beginOpeningHandshake00().
* beginOpeningHandshake00().
* @throws WebSocketHandshakeException * @throws WebSocketHandshakeException
*/ */
@Override @Override

View File

@ -35,9 +35,8 @@ import org.jboss.netty.util.CharsetUtil;
/** /**
* <p> * <p>
* Performs client side opening and closing handshakes for <a * Performs client side opening and closing handshakes for <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>.
* href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>. This was originally * This was originally <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17"
* <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17"
* >draft-ietf-hybi-thewebsocketprotocol- 17</a> * >draft-ietf-hybi-thewebsocketprotocol- 17</a>
* </p> * </p>
*/ */
@ -54,21 +53,17 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
private boolean allowExtensions = false; private boolean allowExtensions = false;
/** /**
* Constructor specifying the destination web socket location and version to * Constructor specifying the destination web socket location and version to initiate
* initiate
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param version * @param version
* Version of web socket specification to use to connect to the * Version of web socket specification to use to connect to the server
* server
* @param subProtocol * @param subProtocol
* Sub protocol request sent to the server. * Sub protocol request sent to the server.
* @param allowExtensions * @param allowExtensions
* Allow extensions to be used in the reserved bits of the web * Allow extensions to be used in the reserved bits of the web socket frame
* socket frame
* @param customHeaders * @param customHeaders
* Map of custom headers to add to the client request * Map of custom headers to add to the client request
*/ */
@ -158,8 +153,7 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
* @param channel * @param channel
* Channel * Channel
* @param response * @param response
* HTTP response returned from the server for the request sent by * HTTP response returned from the server for the request sent by beginOpeningHandshake00().
* beginOpeningHandshake00().
* @throws WebSocketHandshakeException * @throws WebSocketHandshakeException
*/ */
@Override @Override

View File

@ -27,18 +27,14 @@ public class WebSocketClientHandshakerFactory {
* Instances a new handshaker * Instances a new handshaker
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param version * @param version
* Version of web socket specification to use to connect to the * Version of web socket specification to use to connect to the server
* server
* @param subProtocol * @param subProtocol
* Sub protocol request sent to the server. Null if no * Sub protocol request sent to the server. Null if no sub-protocol support is required.
* sub-protocol support is required.
* @param allowExtensions * @param allowExtensions
* Allow extensions to be used in the reserved bits of the web * Allow extensions to be used in the reserved bits of the web socket frame
* socket frame
* @param customHeaders * @param customHeaders
* custom HTTP headers * custom HTTP headers
* @throws WebSocketHandshakeException * @throws WebSocketHandshakeException

View File

@ -23,8 +23,8 @@ import org.jboss.netty.buffer.ChannelBuffer;
public abstract class WebSocketFrame { public abstract class WebSocketFrame {
/** /**
* Flag to indicate if this frame is the final fragment in a message. The * Flag to indicate if this frame is the final fragment in a message. The first fragment (frame) may also be the
* first fragment (frame) may also be the final fragment. * final fragment.
*/ */
private boolean finalFragment = true; private boolean finalFragment = true;
@ -53,8 +53,8 @@ public abstract class WebSocketFrame {
} }
/** /**
* Flag to indicate if this frame is the final fragment in a message. The * Flag to indicate if this frame is the final fragment in a message. The first fragment (frame) may also be the
* first fragment (frame) may also be the final fragment. * final fragment.
*/ */
public boolean isFinalFragment() { public boolean isFinalFragment() {
return finalFragment; return finalFragment;

View File

@ -39,12 +39,10 @@ public abstract class WebSocketServerHandshaker {
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param subProtocols * @param subProtocols
* CSV of supported protocols. Null if sub protocols not * CSV of supported protocols. Null if sub protocols not supported.
* supported.
*/ */
public WebSocketServerHandshaker(String webSocketURL, String subProtocols) { public WebSocketServerHandshaker(String webSocketURL, String subProtocols) {
this.webSocketURL = webSocketURL; this.webSocketURL = webSocketURL;

View File

@ -48,10 +48,9 @@ import org.jboss.netty.logging.InternalLoggerFactory;
/** /**
* <p> * <p>
* Performs server side opening and closing handshakes for web socket * Performs server side opening and closing handshakes for web socket specification version <a
* specification version <a * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00" >draft-ietf-hybi-thewebsocketprotocol-
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00" * 00</a>
* >draft-ietf-hybi-thewebsocketprotocol- 00</a>
* </p> * </p>
* <p> * <p>
* A very large portion of this code was taken from the Netty 3.2 HTTP example. * A very large portion of this code was taken from the Netty 3.2 HTTP example.
@ -65,8 +64,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param subProtocols * @param subProtocols
* CSV of supported protocols * CSV of supported protocols
@ -78,12 +76,9 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
/** /**
* <p> * <p>
* Handle the web socket handshake for the web socket specification <a href= * Handle the web socket handshake for the web socket specification <a href=
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">HyBi * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">HyBi version 0</a> and lower. This standard
* version 0</a> and lower. This standard is really a rehash of <a * is really a rehash of <a href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76" >hixie-76</a> and
* href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76" * <a href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75" >hixie-75</a>.
* >hixie-76</a> and <a
* href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75"
* >hixie-75</a>.
* </p> * </p>
* *
* <p> * <p>
@ -132,7 +127,8 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
this.setVersion(WebSocketVersion.V00); this.setVersion(WebSocketVersion.V00);
// Serve the WebSocket handshake request. // Serve the WebSocket handshake request.
if (!Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) || !WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) { if (!Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION))
|| !WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) {
return; return;
} }
@ -140,7 +136,8 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
boolean isHixie76 = req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2); boolean isHixie76 = req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2);
// Create the WebSocket handshake response. // Create the WebSocket handshake response.
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake")); HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101,
isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake"));
res.addHeader(Names.UPGRADE, WEBSOCKET); res.addHeader(Names.UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, Values.UPGRADE); res.addHeader(CONNECTION, Values.UPGRADE);

View File

@ -38,16 +38,14 @@ import org.jboss.netty.util.CharsetUtil;
/** /**
* <p> * <p>
* Performs server side opening and closing handshakes for web socket * Performs server side opening and closing handshakes for web socket specification version <a
* specification version <a * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" >draft-ietf-hybi-thewebsocketprotocol-
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" * 10</a>
* >draft-ietf-hybi-thewebsocketprotocol- 10</a>
* </p> * </p>
*/ */
public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
private static final InternalLogger logger = InternalLoggerFactory private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandshaker08.class);
.getInstance(WebSocketServerHandshaker08.class);
public static final String WEBSOCKET_08_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; public static final String WEBSOCKET_08_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
@ -57,17 +55,14 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param subProtocols * @param subProtocols
* CSV of supported protocols * CSV of supported protocols
* @param allowExtensions * @param allowExtensions
* Allow extensions to be used in the reserved bits of the web * Allow extensions to be used in the reserved bits of the web socket frame
* socket frame
*/ */
public WebSocketServerHandshaker08(String webSocketURL, public WebSocketServerHandshaker08(String webSocketURL, String subProtocols, boolean allowExtensions) {
String subProtocols, boolean allowExtensions) {
super(webSocketURL, subProtocols); super(webSocketURL, subProtocols);
this.allowExtensions = allowExtensions; this.allowExtensions = allowExtensions;
} }
@ -75,8 +70,8 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
/** /**
* <p> * <p>
* Handle the web socket handshake for the web socket specification <a href= * Handle the web socket handshake for the web socket specification <a href=
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi version 8 to 10</a>. Version 8, 9 and
* version 8 to 10</a>. Version 8, 9 and 10 share the same wire protocol. * 10 share the same wire protocol.
* </p> * </p>
* *
* <p> * <p>
@ -116,13 +111,10 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
public void performOpeningHandshake(Channel channel, HttpRequest req) { public void performOpeningHandshake(Channel channel, HttpRequest req) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format( logger.debug(String.format("Channel %s WS version 08 handshake", channel.getId()));
"Channel %s WS version 08 handshake",
channel.getId()));
} }
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols"));
new HttpResponseStatus(101, "Switching Protocols"));
this.setVersion(WebSocketVersion.V08); this.setVersion(WebSocketVersion.V08);
String key = req.getHeader(Names.SEC_WEBSOCKET_KEY); String key = req.getHeader(Names.SEC_WEBSOCKET_KEY);
@ -135,9 +127,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
String accept = base64Encode(sha1); String accept = base64Encode(sha1);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format( logger.debug(String.format("WS Version 08 Server Handshake key: %s. Response: %s.", key, accept));
"WS Version 08 Server Handshake key: %s. Response: %s.",
key, accept));
} }
res.setStatus(new HttpResponseStatus(101, "Switching Protocols")); res.setStatus(new HttpResponseStatus(101, "Switching Protocols"));
@ -146,8 +136,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept); res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept);
String protocol = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL); String protocol = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
if (protocol != null) { if (protocol != null) {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol));
this.selectSubProtocol(protocol));
} }
channel.write(res); channel.write(res);
@ -155,10 +144,8 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
// Upgrade the connection and send the handshake response. // Upgrade the connection and send the handshake response.
ChannelPipeline p = channel.getPipeline(); ChannelPipeline p = channel.getPipeline();
p.remove(HttpChunkAggregator.class); p.remove(HttpChunkAggregator.class);
p.replace(HttpRequestDecoder.class, "wsdecoder", p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket08FrameDecoder(true, this.allowExtensions));
new WebSocket08FrameDecoder(true, this.allowExtensions)); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket08FrameEncoder(false));
p.replace(HttpResponseEncoder.class, "wsencoder",
new WebSocket08FrameEncoder(false));
} }
@ -171,8 +158,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
* Web Socket frame that was received * Web Socket frame that was received
*/ */
@Override @Override
public void performClosingHandshake(Channel channel, public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
CloseWebSocketFrame frame) {
ChannelFuture f = channel.write(frame); ChannelFuture f = channel.write(frame);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }

View File

@ -38,16 +38,14 @@ import org.jboss.netty.util.CharsetUtil;
/** /**
* <p> * <p>
* Performs server side opening and closing handshakes for <a * Performs server side opening and closing handshakes for <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>.
* href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>. This was originally * This was originally <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17"
* <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17"
* >draft-ietf-hybi-thewebsocketprotocol- 17</a> * >draft-ietf-hybi-thewebsocketprotocol- 17</a>
* </p> * </p>
*/ */
public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
private static final InternalLogger logger = InternalLoggerFactory private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandshaker13.class);
.getInstance(WebSocketServerHandshaker13.class);
public static final String WEBSOCKET_17_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; public static final String WEBSOCKET_17_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
@ -57,17 +55,14 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param subProtocols * @param subProtocols
* CSV of supported protocols * CSV of supported protocols
* @param allowExtensions * @param allowExtensions
* Allow extensions to be used in the reserved bits of the web * Allow extensions to be used in the reserved bits of the web socket frame
* socket frame
*/ */
public WebSocketServerHandshaker13(String webSocketURL, public WebSocketServerHandshaker13(String webSocketURL, String subProtocols, boolean allowExtensions) {
String subProtocols, boolean allowExtensions) {
super(webSocketURL, subProtocols); super(webSocketURL, subProtocols);
this.allowExtensions = allowExtensions; this.allowExtensions = allowExtensions;
} }
@ -75,8 +70,8 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
/** /**
* <p> * <p>
* Handle the web socket handshake for the web socket specification <a href= * Handle the web socket handshake for the web socket specification <a href=
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">HyBi * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">HyBi versions 13-17</a>. Versions 13-17
* versions 13-17</a>. Versions 13-17 share the same wire protocol. * share the same wire protocol.
* </p> * </p>
* *
* <p> * <p>
@ -116,13 +111,10 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
public void performOpeningHandshake(Channel channel, HttpRequest req) { public void performOpeningHandshake(Channel channel, HttpRequest req) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format( logger.debug(String.format("Channel %s WS version 13 handshake", channel.getId()));
"Channel %s WS version 13 handshake",
channel.getId()));
} }
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols"));
new HttpResponseStatus(101, "Switching Protocols"));
this.setVersion(WebSocketVersion.V13); this.setVersion(WebSocketVersion.V13);
String key = req.getHeader(Names.SEC_WEBSOCKET_KEY); String key = req.getHeader(Names.SEC_WEBSOCKET_KEY);
@ -135,9 +127,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
String accept = base64Encode(sha1); String accept = base64Encode(sha1);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format( logger.debug(String.format("WS Version 13 Server Handshake key: %s. Response: %s.", key, accept));
"WS Version 13 Server Handshake key: %s. Response: %s.", key,
accept));
} }
res.setStatus(new HttpResponseStatus(101, "Switching Protocols")); res.setStatus(new HttpResponseStatus(101, "Switching Protocols"));
@ -146,8 +136,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept); res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept);
String protocol = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL); String protocol = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
if (protocol != null) { if (protocol != null) {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol));
this.selectSubProtocol(protocol));
} }
channel.write(res); channel.write(res);
@ -155,10 +144,8 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
// Upgrade the connection and send the handshake response. // Upgrade the connection and send the handshake response.
ChannelPipeline p = channel.getPipeline(); ChannelPipeline p = channel.getPipeline();
p.remove(HttpChunkAggregator.class); p.remove(HttpChunkAggregator.class);
p.replace(HttpRequestDecoder.class, "wsdecoder", p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket13FrameDecoder(true, this.allowExtensions));
new WebSocket13FrameDecoder(true, this.allowExtensions)); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false));
p.replace(HttpResponseEncoder.class, "wsencoder",
new WebSocket13FrameEncoder(false));
} }
@ -171,8 +158,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
* Web Socket frame that was received * Web Socket frame that was received
*/ */
@Override @Override
public void performClosingHandshake(Channel channel, public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
CloseWebSocketFrame frame) {
ChannelFuture f = channel.write(frame); ChannelFuture f = channel.write(frame);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }

View File

@ -38,15 +38,12 @@ public class WebSocketServerHandshakerFactory {
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
* "ws://myhost.com/mypath". Subsequent web socket frames will be
* sent to this URL. * sent to this URL.
* @param subProtocols * @param subProtocols
* CSV of supported protocols. Null if sub protocols not * CSV of supported protocols. Null if sub protocols not supported.
* supported.
* @param allowExtensions * @param allowExtensions
* Allow extensions to be used in the reserved bits of the web * Allow extensions to be used in the reserved bits of the web socket frame
* socket frame
*/ */
public WebSocketServerHandshakerFactory(String webSocketURL, String subProtocols, boolean allowExtensions) { public WebSocketServerHandshakerFactory(String webSocketURL, String subProtocols, boolean allowExtensions) {
this.webSocketURL = webSocketURL; this.webSocketURL = webSocketURL;
@ -57,8 +54,8 @@ public class WebSocketServerHandshakerFactory {
/** /**
* Instances a new handshaker * Instances a new handshaker
* *
* @return A new WebSocketServerHandshaker for the requested web socket * @return A new WebSocketServerHandshaker for the requested web socket version. Null if web socket version is not
* version. Null if web socket version is not supported. * supported.
*/ */
public WebSocketServerHandshaker newHandshaker(HttpRequest req) { public WebSocketServerHandshaker newHandshaker(HttpRequest req) {

View File

@ -24,24 +24,21 @@ public enum WebSocketVersion {
UNKNOWN, UNKNOWN,
/** /**
* <a href= * <a href= "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00"
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00"
* >draft-ietf-hybi-thewebsocketprotocol- 00</a>. * >draft-ietf-hybi-thewebsocketprotocol- 00</a>.
*/ */
V00, V00,
/** /**
* <a href= * <a href= "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10"
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10"
* >draft-ietf-hybi-thewebsocketprotocol- 10</a> * >draft-ietf-hybi-thewebsocketprotocol- 10</a>
*/ */
V08, V08,
/** /**
* <a href="http://tools.ietf.org/html/rfc6455 ">RFC 6455</a>. This was * <a href="http://tools.ietf.org/html/rfc6455 ">RFC 6455</a>. This was originally <a href=
* originally <a href= * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" >draft-ietf-hybi-thewebsocketprotocol-
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" * 17</a>
* >draft-ietf-hybi-thewebsocketprotocol- 17</a>
*/ */
V13; V13;