Support websocket client custom headers. See pull #123
This commit is contained in:
parent
cedbfba07d
commit
f5cc1e02fa
@ -18,6 +18,7 @@ package org.jboss.netty.example.http.websocketx.client;
|
|||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.logging.ConsoleHandler;
|
import java.util.logging.ConsoleHandler;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@ -55,31 +56,34 @@ public class App {
|
|||||||
MyCallbackHandler callbackHandler = new MyCallbackHandler();
|
MyCallbackHandler callbackHandler = new MyCallbackHandler();
|
||||||
WebSocketClientFactory factory = new WebSocketClientFactory();
|
WebSocketClientFactory factory = new WebSocketClientFactory();
|
||||||
|
|
||||||
// Connect with spec version 17. You can change it to V10 or V00.
|
HashMap<String, String> customHeaders = new HashMap<String, String>();
|
||||||
// If you change it to V00, ping is not supported and remember to change HttpResponseDecoder to
|
customHeaders.put("MyHeader", "MyValue");
|
||||||
// WebSocketHttpResponseDecoder in the pipeline.
|
|
||||||
WebSocketClient client = factory.newClient(new URI("ws://localhost:8080/websocket"),
|
// Connect with V13 (RFC 6455). You can change it to V08 or V00.
|
||||||
WebSocketVersion.V13, callbackHandler);
|
// If you change it to V00, ping is not supported and remember to change
|
||||||
|
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
|
||||||
|
WebSocketClient client = factory.newClient(new URI("ws://localhost:8080/websocket"), WebSocketVersion.V13,
|
||||||
|
callbackHandler, customHeaders);
|
||||||
|
|
||||||
// Connect
|
// Connect
|
||||||
System.out.println("WebSocket Client connecting");
|
System.out.println("WebSocket Client connecting");
|
||||||
client.connect().awaitUninterruptibly();
|
client.connect().awaitUninterruptibly();
|
||||||
Thread.sleep(200);
|
Thread.sleep(200);
|
||||||
|
|
||||||
// Send 10 messages and wait for responses
|
// Send 10 messages and wait for responses
|
||||||
System.out.println("WebSocket Client sending message");
|
System.out.println("WebSocket Client sending message");
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
client.send(new TextWebSocketFrame("Message #" + i));
|
client.send(new TextWebSocketFrame("Message #" + i));
|
||||||
}
|
}
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
|
||||||
// Ping
|
// Ping
|
||||||
System.out.println("WebSocket Client sending ping");
|
System.out.println("WebSocket Client sending ping");
|
||||||
client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
|
client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
|
||||||
// Close
|
// Close
|
||||||
System.out.println("WebSocket Client sending close");
|
System.out.println("WebSocket Client sending close");
|
||||||
client.send(new CloseWebSocketFrame());
|
client.send(new CloseWebSocketFrame());
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
@ -29,26 +29,26 @@ import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|||||||
*/
|
*/
|
||||||
public interface WebSocketClient {
|
public interface WebSocketClient {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to server Host and port is setup by the factory.
|
* Connect to server Host and port is setup by the factory.
|
||||||
*
|
*
|
||||||
* @return Connect future. Fires when connected.
|
* @return Connect future. Fires when connected.
|
||||||
*/
|
*/
|
||||||
ChannelFuture connect();
|
ChannelFuture connect();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disconnect from the server
|
* Disconnect from the server
|
||||||
*
|
*
|
||||||
* @return Disconnect future. Fires when disconnected.
|
* @return Disconnect future. Fires when disconnected.
|
||||||
*/
|
*/
|
||||||
ChannelFuture disconnect();
|
ChannelFuture disconnect();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send data to server
|
* Send data to server
|
||||||
*
|
*
|
||||||
* @param frame
|
* @param frame
|
||||||
* Data for sending
|
* Data for sending
|
||||||
* @return Write future. Will fire when the data is sent.
|
* @return Write future. Will fire when the data is sent.
|
||||||
*/
|
*/
|
||||||
ChannelFuture send(WebSocketFrame frame);
|
ChannelFuture send(WebSocketFrame frame);
|
||||||
}
|
}
|
||||||
|
@ -32,57 +32,62 @@ import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
|||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executors;
|
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 connecting a client. Can and should be
|
* A factory for creating WebSocket clients. The entry point for creating and
|
||||||
* used to create multiple instances.
|
* connecting a client. Can and should be used to create multiple instances.
|
||||||
*/
|
*/
|
||||||
public class WebSocketClientFactory {
|
public class WebSocketClientFactory {
|
||||||
|
|
||||||
private final NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
|
private final NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
|
||||||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
|
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new WebSocket client
|
* Create a new WebSocket client
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* URL to connect to.
|
* URL to connect to.
|
||||||
* @param version
|
* @param version
|
||||||
* Web Socket version to support
|
* Web Socket version to support
|
||||||
* @param callback
|
* @param callback
|
||||||
* Callback interface to receive events
|
* Callback interface to receive events
|
||||||
* @return A WebSocket client. Call {@link WebSocketClient#connect()} to connect.
|
* @param customHeaders
|
||||||
*/
|
* Map of custom headers to add to the client request
|
||||||
public WebSocketClient newClient(final URI url,
|
* @return A WebSocket client. Call {@link WebSocketClient#connect()} to
|
||||||
final WebSocketVersion version,
|
* connect.
|
||||||
final WebSocketCallback callback) {
|
*/
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);
|
public WebSocketClient newClient(final URI url, final WebSocketVersion version, final WebSocketCallback callback,
|
||||||
|
final Map<String, String> customHeaders) {
|
||||||
|
ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);
|
||||||
|
|
||||||
String protocol = url.getScheme();
|
String protocol = url.getScheme();
|
||||||
if (!protocol.equals("ws") && !protocol.equals("wss")) {
|
if (!protocol.equals("ws") && !protocol.equals("wss")) {
|
||||||
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
|
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
|
||||||
}
|
}
|
||||||
|
|
||||||
final WebSocketClientHandler clientHandler = new WebSocketClientHandler(bootstrap, url, version, callback);
|
final WebSocketClientHandler clientHandler = new WebSocketClientHandler(bootstrap, url, version, callback,
|
||||||
|
customHeaders);
|
||||||
|
|
||||||
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
||||||
|
|
||||||
public ChannelPipeline getPipeline() throws Exception {
|
public ChannelPipeline getPipeline() throws Exception {
|
||||||
ChannelPipeline pipeline = Channels.pipeline();
|
ChannelPipeline pipeline = Channels.pipeline();
|
||||||
|
|
||||||
// If you wish to support HyBi V00, you need to use WebSocketHttpResponseDecoder instead for
|
|
||||||
// HttpResponseDecoder.
|
|
||||||
pipeline.addLast("decoder", new HttpResponseDecoder());
|
|
||||||
|
|
||||||
pipeline.addLast("encoder", new HttpRequestEncoder());
|
|
||||||
pipeline.addLast("ws-handler", clientHandler);
|
|
||||||
return pipeline;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return clientHandler;
|
// If you wish to support HyBi V00, you need to use
|
||||||
}
|
// WebSocketHttpResponseDecoder instead for
|
||||||
|
// HttpResponseDecoder.
|
||||||
|
pipeline.addLast("decoder", new HttpResponseDecoder());
|
||||||
|
|
||||||
|
pipeline.addLast("encoder", new HttpRequestEncoder());
|
||||||
|
pipeline.addLast("ws-handler", clientHandler);
|
||||||
|
return pipeline;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return clientHandler;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ package org.jboss.netty.example.http.websocketx.client;
|
|||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
@ -43,79 +44,85 @@ 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 for end-users. Please use
|
* Handles socket communication for a connected WebSocket client Not intended
|
||||||
* {@link WebSocketClient} or {@link WebSocketCallback} for controlling your client.
|
* for end-users. Please use {@link WebSocketClient} or
|
||||||
|
* {@link WebSocketCallback} for controlling your client.
|
||||||
*/
|
*/
|
||||||
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler implements WebSocketClient {
|
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler implements WebSocketClient {
|
||||||
|
|
||||||
private final ClientBootstrap bootstrap;
|
private final ClientBootstrap bootstrap;
|
||||||
private URI url;
|
private URI url;
|
||||||
private final WebSocketCallback callback;
|
private final WebSocketCallback callback;
|
||||||
private Channel channel;
|
private Channel channel;
|
||||||
private WebSocketClientHandshaker handshaker = null;
|
private WebSocketClientHandshaker handshaker = null;
|
||||||
private final WebSocketVersion version;
|
private final WebSocketVersion version;
|
||||||
|
private Map<String, String> customHeaders = null;
|
||||||
|
|
||||||
public WebSocketClientHandler(ClientBootstrap bootstrap, URI url, WebSocketVersion version, WebSocketCallback callback) {
|
public WebSocketClientHandler(ClientBootstrap bootstrap, URI url, WebSocketVersion version,
|
||||||
this.bootstrap = bootstrap;
|
WebSocketCallback callback, Map<String, String> customHeaders) {
|
||||||
this.url = url;
|
this.bootstrap = bootstrap;
|
||||||
this.version = version;
|
this.url = url;
|
||||||
this.callback = callback;
|
this.version = version;
|
||||||
}
|
this.callback = callback;
|
||||||
|
this.customHeaders = customHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
||||||
channel = e.getChannel();
|
channel = e.getChannel();
|
||||||
this.handshaker = new WebSocketClientHandshakerFactory().newHandshaker(url, version, null, false);
|
this.handshaker = new WebSocketClientHandshakerFactory()
|
||||||
handshaker.performOpeningHandshake(channel);
|
.newHandshaker(url, version, null, false, customHeaders);
|
||||||
}
|
handshaker.performOpeningHandshake(channel);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
||||||
callback.onDisconnect(this);
|
callback.onDisconnect(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||||
if (!handshaker.isOpeningHandshakeCompleted()) {
|
if (!handshaker.isOpeningHandshakeCompleted()) {
|
||||||
handshaker.performClosingHandshake(ctx.getChannel(), (HttpResponse) e.getMessage());
|
handshaker.performClosingHandshake(ctx.getChannel(), (HttpResponse) e.getMessage());
|
||||||
callback.onConnect(this);
|
callback.onConnect(this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.getMessage() instanceof HttpResponse) {
|
if (e.getMessage() instanceof HttpResponse) {
|
||||||
HttpResponse response = (HttpResponse) e.getMessage();
|
HttpResponse response = (HttpResponse) e.getMessage();
|
||||||
throw new WebSocketException("Unexpected HttpResponse (status=" + response.getStatus() + ", content="
|
throw new WebSocketException("Unexpected HttpResponse (status=" + response.getStatus() + ", content="
|
||||||
+ response.getContent().toString(CharsetUtil.UTF_8) + ")");
|
+ response.getContent().toString(CharsetUtil.UTF_8) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
|
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
|
||||||
callback.onMessage(this, frame);
|
callback.onMessage(this, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
|
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
callback.onError(t);
|
callback.onError(t);
|
||||||
e.getChannel().close();
|
e.getChannel().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChannelFuture connect() {
|
public ChannelFuture connect() {
|
||||||
return bootstrap.connect(new InetSocketAddress(url.getHost(), url.getPort()));
|
return bootstrap.connect(new InetSocketAddress(url.getHost(), url.getPort()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChannelFuture disconnect() {
|
public ChannelFuture disconnect() {
|
||||||
return channel.close();
|
return channel.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChannelFuture send(WebSocketFrame frame) {
|
public ChannelFuture send(WebSocketFrame frame) {
|
||||||
return channel.write(frame);
|
return channel.write(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI getUrl() {
|
public URI getUrl() {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUrl(URI url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
public void setUrl(URI url) {
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -18,6 +18,7 @@ package org.jboss.netty.handler.codec.http.websocketx;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import org.jboss.netty.buffer.ChannelBuffer;
|
||||||
import org.jboss.netty.buffer.ChannelBuffers;
|
import org.jboss.netty.buffer.ChannelBuffers;
|
||||||
@ -31,174 +32,188 @@ import org.jboss.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public abstract class WebSocketClientHandshaker {
|
public abstract class WebSocketClientHandshaker {
|
||||||
|
|
||||||
private URI webSocketURL;
|
private URI webSocketURL;
|
||||||
|
|
||||||
private WebSocketVersion version = WebSocketVersion.UNKNOWN;
|
private WebSocketVersion version = WebSocketVersion.UNKNOWN;
|
||||||
|
|
||||||
private boolean openingHandshakeCompleted = false;
|
private boolean openingHandshakeCompleted = false;
|
||||||
|
|
||||||
private String subProtocolRequest = null;
|
private String subProtocolRequest = null;
|
||||||
|
|
||||||
private String subProtocolResponse = null;
|
private String subProtocolResponse = null;
|
||||||
|
|
||||||
/**
|
protected Map<String, String> customHeaders = null;
|
||||||
*
|
|
||||||
* @param webSocketURL
|
|
||||||
* @param version
|
|
||||||
* @param subProtocol
|
|
||||||
*/
|
|
||||||
public WebSocketClientHandshaker(URI webSocketURL, WebSocketVersion version, String subProtocol) {
|
|
||||||
this.webSocketURL = webSocketURL;
|
|
||||||
this.version = version;
|
|
||||||
this.subProtocolRequest = subProtocol;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the URI to the web socket. e.g. "ws://myhost.com/path"
|
* Base constructor
|
||||||
*/
|
*
|
||||||
public URI getWebSocketURL() {
|
* @param webSocketURL
|
||||||
return webSocketURL;
|
* URL for web socket communications. e.g
|
||||||
}
|
* "ws://myhost.com/mypath". Subsequent web socket frames will be
|
||||||
|
* sent to this URL.
|
||||||
|
* @param version
|
||||||
|
* Version of web socket specification to use to connect to the
|
||||||
|
* server
|
||||||
|
* @param subProtocol
|
||||||
|
* Sub protocol request sent to the server.
|
||||||
|
* @param customHeaders
|
||||||
|
* Map of custom headers to add to the client request
|
||||||
|
*/
|
||||||
|
public WebSocketClientHandshaker(URI webSocketURL, WebSocketVersion version, String subProtocol,
|
||||||
|
Map<String, String> customHeaders) {
|
||||||
|
this.webSocketURL = webSocketURL;
|
||||||
|
this.version = version;
|
||||||
|
this.subProtocolRequest = subProtocol;
|
||||||
|
this.customHeaders = customHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
protected void setWebSocketURL(URI webSocketURL) {
|
/**
|
||||||
this.webSocketURL = webSocketURL;
|
* Returns the URI to the web socket. e.g. "ws://myhost.com/path"
|
||||||
}
|
*/
|
||||||
|
public URI getWebSocketURL() {
|
||||||
|
return webSocketURL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
protected void setWebSocketURL(URI webSocketURL) {
|
||||||
* Version of the web socket specification that is being used
|
this.webSocketURL = webSocketURL;
|
||||||
*/
|
}
|
||||||
public WebSocketVersion getVersion() {
|
|
||||||
return version;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setVersion(WebSocketVersion version) {
|
/**
|
||||||
this.version = version;
|
* Version of the web socket specification that is being used
|
||||||
}
|
*/
|
||||||
|
public WebSocketVersion getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
protected void setVersion(WebSocketVersion version) {
|
||||||
* Flag to indicate if the opening handshake is complete
|
this.version = version;
|
||||||
*/
|
}
|
||||||
public boolean isOpeningHandshakeCompleted() {
|
|
||||||
return openingHandshakeCompleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setOpenningHandshakeCompleted(boolean openningHandshakeCompleted) {
|
/**
|
||||||
this.openingHandshakeCompleted = openningHandshakeCompleted;
|
* Flag to indicate if the opening handshake is complete
|
||||||
}
|
*/
|
||||||
|
public boolean isOpeningHandshakeCompleted() {
|
||||||
|
return openingHandshakeCompleted;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
protected void setOpenningHandshakeCompleted(boolean openningHandshakeCompleted) {
|
||||||
* Returns the sub protocol request sent to the server as specified in the
|
this.openingHandshakeCompleted = openningHandshakeCompleted;
|
||||||
* constructor
|
}
|
||||||
*/
|
|
||||||
public String getSubProtocolRequest() {
|
|
||||||
return subProtocolRequest;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setSubProtocolRequest(String subProtocolRequest) {
|
/**
|
||||||
this.subProtocolRequest = subProtocolRequest;
|
* Returns the sub protocol request sent to the server as specified in the
|
||||||
}
|
* constructor
|
||||||
|
*/
|
||||||
|
public String getSubProtocolRequest() {
|
||||||
|
return subProtocolRequest;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
protected void setSubProtocolRequest(String subProtocolRequest) {
|
||||||
* Returns the sub protocol response and sent by the server. Only available
|
this.subProtocolRequest = subProtocolRequest;
|
||||||
* after end of handshake.
|
}
|
||||||
*/
|
|
||||||
public String getSubProtocolResponse() {
|
|
||||||
return subProtocolResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setSubProtocolResponse(String subProtocolResponse) {
|
/**
|
||||||
this.subProtocolResponse = subProtocolResponse;
|
* Returns the sub protocol response and sent by the server. Only available
|
||||||
}
|
* after end of handshake.
|
||||||
|
*/
|
||||||
|
public String getSubProtocolResponse() {
|
||||||
|
return subProtocolResponse;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
protected void setSubProtocolResponse(String subProtocolResponse) {
|
||||||
* Performs the opening handshake
|
this.subProtocolResponse = subProtocolResponse;
|
||||||
*
|
}
|
||||||
* @param channel
|
|
||||||
* Channel
|
|
||||||
*/
|
|
||||||
public abstract void performOpeningHandshake(Channel channel);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the closing handshake
|
* Performs the opening handshake
|
||||||
*
|
*
|
||||||
* @param channel
|
* @param channel
|
||||||
* Channel
|
* Channel
|
||||||
* @param response
|
*/
|
||||||
* HTTP response containing the closing handshake details
|
public abstract void performOpeningHandshake(Channel channel);
|
||||||
*/
|
|
||||||
public abstract void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs an MD5 hash
|
* Performs the closing handshake
|
||||||
*
|
*
|
||||||
* @param bytes
|
* @param channel
|
||||||
* Data to hash
|
* Channel
|
||||||
* @return Hashed data
|
* @param response
|
||||||
*/
|
* HTTP response containing the closing handshake details
|
||||||
protected byte[] md5(byte[] bytes) {
|
*/
|
||||||
try {
|
public abstract void performClosingHandshake(Channel channel, HttpResponse response)
|
||||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
throws WebSocketHandshakeException;
|
||||||
return md.digest(bytes);
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new InternalError("MD5 not supported on this platform");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs an SHA-1 hash
|
* Performs an MD5 hash
|
||||||
*
|
*
|
||||||
* @param bytes
|
* @param bytes
|
||||||
* Data to hash
|
* Data to hash
|
||||||
* @return Hashed data
|
* @return Hashed data
|
||||||
*/
|
*/
|
||||||
protected byte[] sha1(byte[] bytes) {
|
protected byte[] md5(byte[] bytes) {
|
||||||
try {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance("SHA1");
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
return md.digest(bytes);
|
return md.digest(bytes);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
throw new InternalError("SHA-1 not supported on this platform");
|
throw new InternalError("MD5 not supported on this platform");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base 64 encoding
|
* Performs an SHA-1 hash
|
||||||
*
|
*
|
||||||
* @param bytes
|
* @param bytes
|
||||||
* Bytes to encode
|
* Data to hash
|
||||||
* @return encoded string
|
* @return Hashed data
|
||||||
*/
|
*/
|
||||||
protected String base64Encode(byte[] bytes) {
|
protected byte[] sha1(byte[] bytes) {
|
||||||
ChannelBuffer hashed = ChannelBuffers.wrappedBuffer(bytes);
|
try {
|
||||||
return Base64.encode(hashed).toString(CharsetUtil.UTF_8);
|
MessageDigest md = MessageDigest.getInstance("SHA1");
|
||||||
}
|
return md.digest(bytes);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new InternalError("SHA-1 not supported on this platform");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates some random bytes
|
* Base 64 encoding
|
||||||
*
|
*
|
||||||
* @param size
|
* @param bytes
|
||||||
* Number of random bytes to create
|
* Bytes to encode
|
||||||
* @return random bytes
|
* @return encoded string
|
||||||
*/
|
*/
|
||||||
protected byte[] createRandomBytes(int size) {
|
protected String base64Encode(byte[] bytes) {
|
||||||
byte[] bytes = new byte[size];
|
ChannelBuffer hashed = ChannelBuffers.wrappedBuffer(bytes);
|
||||||
|
return Base64.encode(hashed).toString(CharsetUtil.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < size; i++) {
|
/**
|
||||||
bytes[i] = (byte) createRandomNumber(0, 255);
|
* Creates some random bytes
|
||||||
}
|
*
|
||||||
|
* @param size
|
||||||
|
* Number of random bytes to create
|
||||||
|
* @return random bytes
|
||||||
|
*/
|
||||||
|
protected byte[] createRandomBytes(int size) {
|
||||||
|
byte[] bytes = new byte[size];
|
||||||
|
|
||||||
return bytes;
|
for (int i = 0; i < size; i++) {
|
||||||
}
|
bytes[i] = (byte) createRandomNumber(0, 255);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return bytes;
|
||||||
* Generates a random number
|
}
|
||||||
*
|
|
||||||
* @param min
|
/**
|
||||||
* Minimum value
|
* Generates a random number
|
||||||
* @param max
|
*
|
||||||
* Maximum value
|
* @param min
|
||||||
* @return Random number
|
* Minimum value
|
||||||
*/
|
* @param max
|
||||||
protected int createRandomNumber(int min, int max) {
|
* Maximum value
|
||||||
return (int) (Math.random() * max + min);
|
* @return Random number
|
||||||
}
|
*/
|
||||||
|
protected int createRandomNumber(int min, int max) {
|
||||||
|
return (int) (Math.random() * max + min);
|
||||||
|
}
|
||||||
}
|
}
|
@ -18,6 +18,7 @@ package org.jboss.netty.handler.codec.http.websocketx;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffers;
|
import org.jboss.netty.buffer.ChannelBuffers;
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
@ -45,196 +46,206 @@ import org.jboss.netty.handler.codec.http.HttpVersion;
|
|||||||
*/
|
*/
|
||||||
public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
|
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
|
||||||
public WebSocketClientHandshaker00(URI webSocketURL, WebSocketVersion version, String subProtocol) {
|
* Map of custom headers to add to the client request
|
||||||
super(webSocketURL, version, subProtocol);
|
*/
|
||||||
}
|
public WebSocketClientHandshaker00(URI webSocketURL, WebSocketVersion version, String subProtocol,
|
||||||
|
Map<String, String> customHeaders) {
|
||||||
|
super(webSocketURL, version, subProtocol, customHeaders);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Sends the opening request to the server:
|
* Sends the opening request to the server:
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* GET /demo HTTP/1.1
|
* GET /demo HTTP/1.1
|
||||||
* Upgrade: WebSocket
|
* Upgrade: WebSocket
|
||||||
* Connection: Upgrade
|
* Connection: Upgrade
|
||||||
* Host: example.com
|
* Host: example.com
|
||||||
* Origin: http://example.com
|
* Origin: http://example.com
|
||||||
* Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
|
* Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
|
||||||
* Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
|
* Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
|
||||||
*
|
*
|
||||||
* ^n:ds[4U
|
* ^n:ds[4U
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param channel
|
* @param channel
|
||||||
* Channel into which we can write our request
|
* Channel into which we can write our request
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void performOpeningHandshake(Channel channel) {
|
public void performOpeningHandshake(Channel channel) {
|
||||||
// Make keys
|
// Make keys
|
||||||
int spaces1 = createRandomNumber(1, 12);
|
int spaces1 = createRandomNumber(1, 12);
|
||||||
int spaces2 = createRandomNumber(1, 12);
|
int spaces2 = createRandomNumber(1, 12);
|
||||||
|
|
||||||
int max1 = Integer.MAX_VALUE / spaces1;
|
int max1 = Integer.MAX_VALUE / spaces1;
|
||||||
int max2 = Integer.MAX_VALUE / spaces2;
|
int max2 = Integer.MAX_VALUE / spaces2;
|
||||||
|
|
||||||
int number1 = createRandomNumber(0, max1);
|
int number1 = createRandomNumber(0, max1);
|
||||||
int number2 = createRandomNumber(0, max2);
|
int number2 = createRandomNumber(0, max2);
|
||||||
|
|
||||||
int product1 = number1 * spaces1;
|
int product1 = number1 * spaces1;
|
||||||
int product2 = number2 * spaces2;
|
int product2 = number2 * spaces2;
|
||||||
|
|
||||||
String key1 = Integer.toString(product1);
|
String key1 = Integer.toString(product1);
|
||||||
String key2 = Integer.toString(product2);
|
String key2 = Integer.toString(product2);
|
||||||
|
|
||||||
key1 = insertRandomCharacters(key1);
|
key1 = insertRandomCharacters(key1);
|
||||||
key2 = insertRandomCharacters(key2);
|
key2 = insertRandomCharacters(key2);
|
||||||
|
|
||||||
key1 = insertSpaces(key1, spaces1);
|
key1 = insertSpaces(key1, spaces1);
|
||||||
key2 = insertSpaces(key2, spaces2);
|
key2 = insertSpaces(key2, spaces2);
|
||||||
|
|
||||||
byte[] key3 = createRandomBytes(8);
|
byte[] key3 = createRandomBytes(8);
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(4);
|
ByteBuffer buffer = ByteBuffer.allocate(4);
|
||||||
buffer.putInt(number1);
|
buffer.putInt(number1);
|
||||||
byte[] number1Array = buffer.array();
|
byte[] number1Array = buffer.array();
|
||||||
buffer = ByteBuffer.allocate(4);
|
buffer = ByteBuffer.allocate(4);
|
||||||
buffer.putInt(number2);
|
buffer.putInt(number2);
|
||||||
byte[] number2Array = buffer.array();
|
byte[] number2Array = buffer.array();
|
||||||
|
|
||||||
byte[] challenge = new byte[16];
|
byte[] challenge = new byte[16];
|
||||||
System.arraycopy(number1Array, 0, challenge, 0, 4);
|
System.arraycopy(number1Array, 0, challenge, 0, 4);
|
||||||
System.arraycopy(number2Array, 0, challenge, 4, 4);
|
System.arraycopy(number2Array, 0, challenge, 4, 4);
|
||||||
System.arraycopy(key3, 0, challenge, 8, 8);
|
System.arraycopy(key3, 0, challenge, 8, 8);
|
||||||
this.expectedChallengeResponseBytes = md5(challenge);
|
this.expectedChallengeResponseBytes = md5(challenge);
|
||||||
|
|
||||||
// Get path
|
// Get path
|
||||||
URI wsURL = this.getWebSocketURL();
|
URI wsURL = this.getWebSocketURL();
|
||||||
String path = wsURL.getPath();
|
String path = wsURL.getPath();
|
||||||
if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
|
if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
|
||||||
path = wsURL.getPath() + "?" + wsURL.getQuery();
|
path = wsURL.getPath() + "?" + wsURL.getQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format request
|
// Format request
|
||||||
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
|
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
|
||||||
request.addHeader(Names.UPGRADE, Values.WEBSOCKET);
|
request.addHeader(Names.UPGRADE, Values.WEBSOCKET);
|
||||||
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
||||||
request.addHeader(Names.HOST, wsURL.getHost());
|
request.addHeader(Names.HOST, wsURL.getHost());
|
||||||
request.addHeader(Names.ORIGIN, "http://" + wsURL.getHost());
|
request.addHeader(Names.ORIGIN, "http://" + wsURL.getHost());
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_KEY1, key1);
|
request.addHeader(Names.SEC_WEBSOCKET_KEY1, key1);
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_KEY2, key2);
|
request.addHeader(Names.SEC_WEBSOCKET_KEY2, key2);
|
||||||
if (this.getSubProtocolRequest() != null && !this.getSubProtocolRequest().equals("")) {
|
if (this.getSubProtocolRequest() != null && !this.getSubProtocolRequest().equals("")) {
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.getSubProtocolRequest());
|
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.getSubProtocolRequest());
|
||||||
}
|
}
|
||||||
request.setContent(ChannelBuffers.copiedBuffer(key3));
|
if (customHeaders != null) {
|
||||||
|
for (String header : customHeaders.keySet()) {
|
||||||
|
request.addHeader(header, customHeaders.get(header));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
request.setContent(ChannelBuffers.copiedBuffer(key3));
|
||||||
|
|
||||||
channel.write(request);
|
channel.write(request);
|
||||||
|
|
||||||
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder());
|
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Process server response:
|
* Process server response:
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* HTTP/1.1 101 WebSocket Protocol Handshake
|
* HTTP/1.1 101 WebSocket Protocol Handshake
|
||||||
* Upgrade: WebSocket
|
* Upgrade: WebSocket
|
||||||
* Connection: Upgrade
|
* Connection: Upgrade
|
||||||
* Sec-WebSocket-Origin: http://example.com
|
* Sec-WebSocket-Origin: http://example.com
|
||||||
* Sec-WebSocket-Location: ws://example.com/demo
|
* Sec-WebSocket-Location: ws://example.com/demo
|
||||||
* Sec-WebSocket-Protocol: sample
|
* Sec-WebSocket-Protocol: sample
|
||||||
*
|
*
|
||||||
* 8jKS'y:G*Co,Wxa-
|
* 8jKS'y:G*Co,Wxa-
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @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
|
||||||
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
|
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
|
||||||
final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake");
|
final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake");
|
||||||
|
|
||||||
if (!response.getStatus().equals(status)) {
|
if (!response.getStatus().equals(status)) {
|
||||||
throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
|
throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
String upgrade = response.getHeader(Names.UPGRADE);
|
String upgrade = response.getHeader(Names.UPGRADE);
|
||||||
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET)) {
|
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET)) {
|
||||||
throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + response.getHeader(Names.UPGRADE));
|
throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
|
||||||
}
|
+ response.getHeader(Names.UPGRADE));
|
||||||
|
}
|
||||||
|
|
||||||
String connection = response.getHeader(Names.CONNECTION);
|
String connection = response.getHeader(Names.CONNECTION);
|
||||||
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
||||||
throw new WebSocketHandshakeException("Invalid handshake response connection: " + response.getHeader(Names.CONNECTION));
|
throw new WebSocketHandshakeException("Invalid handshake response connection: "
|
||||||
}
|
+ response.getHeader(Names.CONNECTION));
|
||||||
|
}
|
||||||
|
|
||||||
byte[] challenge = response.getContent().array();
|
byte[] challenge = response.getContent().array();
|
||||||
if (!Arrays.equals(challenge, expectedChallengeResponseBytes)) {
|
if (!Arrays.equals(challenge, expectedChallengeResponseBytes)) {
|
||||||
throw new WebSocketHandshakeException("Invalid challenge");
|
throw new WebSocketHandshakeException("Invalid challenge");
|
||||||
}
|
}
|
||||||
|
|
||||||
String protocol = response.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
|
String protocol = response.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
|
||||||
this.setSubProtocolResponse(protocol);
|
this.setSubProtocolResponse(protocol);
|
||||||
|
|
||||||
channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket00FrameDecoder());
|
channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket00FrameDecoder());
|
||||||
|
|
||||||
this.setOpenningHandshakeCompleted(true);
|
this.setOpenningHandshakeCompleted(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String insertRandomCharacters(String key) {
|
private String insertRandomCharacters(String key) {
|
||||||
int count = createRandomNumber(1, 12);
|
int count = createRandomNumber(1, 12);
|
||||||
|
|
||||||
char[] randomChars = new char[count];
|
char[] randomChars = new char[count];
|
||||||
int randCount = 0;
|
int randCount = 0;
|
||||||
while (randCount < count) {
|
while (randCount < count) {
|
||||||
int rand = (int) (Math.random() * 0x7e + 0x21);
|
int rand = (int) (Math.random() * 0x7e + 0x21);
|
||||||
if (((0x21 < rand) && (rand < 0x2f)) || ((0x3a < rand) && (rand < 0x7e))) {
|
if (((0x21 < rand) && (rand < 0x2f)) || ((0x3a < rand) && (rand < 0x7e))) {
|
||||||
randomChars[randCount] = (char) rand;
|
randomChars[randCount] = (char) rand;
|
||||||
randCount += 1;
|
randCount += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
int split = createRandomNumber(0, key.length());
|
int split = createRandomNumber(0, key.length());
|
||||||
String part1 = key.substring(0, split);
|
String part1 = key.substring(0, split);
|
||||||
String part2 = key.substring(split);
|
String part2 = key.substring(split);
|
||||||
key = part1 + randomChars[i] + part2;
|
key = part1 + randomChars[i] + part2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String insertSpaces(String key, int spaces) {
|
private String insertSpaces(String key, int spaces) {
|
||||||
for (int i = 0; i < spaces; i++) {
|
for (int i = 0; i < spaces; i++) {
|
||||||
int split = createRandomNumber(1, key.length() - 1);
|
int split = createRandomNumber(1, key.length() - 1);
|
||||||
String part1 = key.substring(0, split);
|
String part1 = key.substring(0, split);
|
||||||
String part2 = key.substring(split);
|
String part2 = key.substring(split);
|
||||||
key = part1 + " " + part2;
|
key = part1 + " " + part2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.handler.codec.http.websocketx;
|
package org.jboss.netty.handler.codec.http.websocketx;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
|
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
|
||||||
@ -42,8 +43,7 @@ import org.jboss.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
||||||
|
|
||||||
private static final InternalLogger logger = InternalLoggerFactory
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker08.class);
|
||||||
.getInstance(WebSocketClientHandshaker08.class);
|
|
||||||
|
|
||||||
public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
|
|
||||||
@ -69,11 +69,12 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
|||||||
* @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
|
||||||
|
* Map of custom headers to add to the client request
|
||||||
*/
|
*/
|
||||||
public WebSocketClientHandshaker08(URI webSocketURL,
|
public WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subProtocol,
|
||||||
WebSocketVersion version, String subProtocol,
|
boolean allowExtensions, Map<String, String> customHeaders) {
|
||||||
boolean allowExtensions) {
|
super(webSocketURL, version, subProtocol, customHeaders);
|
||||||
super(webSocketURL, version, subProtocol);
|
|
||||||
this.allowExtensions = allowExtensions;
|
this.allowExtensions = allowExtensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,14 +116,12 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
|||||||
this.expectedChallengeResponseString = base64Encode(sha1);
|
this.expectedChallengeResponseString = base64Encode(sha1);
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String
|
logger.debug(String.format("WS Version 08 Client Handshake key: %s. Expected response: %s.", key,
|
||||||
.format("WS Version 08 Client Handshake key: %s. Expected response: %s.",
|
this.expectedChallengeResponseString));
|
||||||
key, this.expectedChallengeResponseString));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format request
|
// Format request
|
||||||
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
|
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
|
||||||
HttpMethod.GET, path);
|
|
||||||
request.addHeader(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
|
request.addHeader(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
|
||||||
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_KEY, key);
|
request.addHeader(Names.SEC_WEBSOCKET_KEY, key);
|
||||||
@ -132,11 +131,15 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
|||||||
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol);
|
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol);
|
||||||
}
|
}
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_VERSION, "8");
|
request.addHeader(Names.SEC_WEBSOCKET_VERSION, "8");
|
||||||
|
if (customHeaders != null) {
|
||||||
|
for (String header : customHeaders.keySet()) {
|
||||||
|
request.addHeader(header, customHeaders.get(header));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
channel.write(request);
|
channel.write(request);
|
||||||
|
|
||||||
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder",
|
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket08FrameEncoder(true));
|
||||||
new WebSocket08FrameEncoder(true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,36 +163,28 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
|||||||
* @throws WebSocketHandshakeException
|
* @throws WebSocketHandshakeException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void performClosingHandshake(Channel channel, HttpResponse response)
|
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
|
||||||
throws WebSocketHandshakeException {
|
final HttpResponseStatus status = new HttpResponseStatus(101, "Switching Protocols");
|
||||||
final HttpResponseStatus status = new HttpResponseStatus(101,
|
|
||||||
"Switching Protocols");
|
|
||||||
|
|
||||||
if (!response.getStatus().equals(status)) {
|
if (!response.getStatus().equals(status)) {
|
||||||
throw new WebSocketHandshakeException(
|
throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
|
||||||
"Invalid handshake response status: "
|
|
||||||
+ response.getStatus());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String upgrade = response.getHeader(Names.UPGRADE);
|
String upgrade = response.getHeader(Names.UPGRADE);
|
||||||
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET.toLowerCase())) {
|
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET.toLowerCase())) {
|
||||||
throw new WebSocketHandshakeException(
|
throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
|
||||||
"Invalid handshake response upgrade: "
|
+ response.getHeader(Names.UPGRADE));
|
||||||
+ response.getHeader(Names.UPGRADE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String connection = response.getHeader(Names.CONNECTION);
|
String connection = response.getHeader(Names.CONNECTION);
|
||||||
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
||||||
throw new WebSocketHandshakeException(
|
throw new WebSocketHandshakeException("Invalid handshake response connection: "
|
||||||
"Invalid handshake response connection: "
|
+ response.getHeader(Names.CONNECTION));
|
||||||
+ response.getHeader(Names.CONNECTION));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String accept = response.getHeader(Names.SEC_WEBSOCKET_ACCEPT);
|
String accept = response.getHeader(Names.SEC_WEBSOCKET_ACCEPT);
|
||||||
if (accept == null
|
if (accept == null || !accept.equals(this.expectedChallengeResponseString)) {
|
||||||
|| !accept.equals(this.expectedChallengeResponseString)) {
|
throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept,
|
||||||
throw new WebSocketHandshakeException(String.format(
|
|
||||||
"Invalid challenge. Actual: %s. Expected: %s", accept,
|
|
||||||
this.expectedChallengeResponseString));
|
this.expectedChallengeResponseString));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.handler.codec.http.websocketx;
|
package org.jboss.netty.handler.codec.http.websocketx;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
|
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
|
||||||
@ -42,8 +43,7 @@ import org.jboss.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
||||||
|
|
||||||
private static final InternalLogger logger = InternalLoggerFactory
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker13.class);
|
||||||
.getInstance(WebSocketClientHandshaker13.class);
|
|
||||||
|
|
||||||
public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
|
|
||||||
@ -69,11 +69,12 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
|||||||
* @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
|
||||||
|
* Map of custom headers to add to the client request
|
||||||
*/
|
*/
|
||||||
public WebSocketClientHandshaker13(URI webSocketURL,
|
public WebSocketClientHandshaker13(URI webSocketURL, WebSocketVersion version, String subProtocol,
|
||||||
WebSocketVersion version, String subProtocol,
|
boolean allowExtensions, Map<String, String> customHeaders) {
|
||||||
boolean allowExtensions) {
|
super(webSocketURL, version, subProtocol, customHeaders);
|
||||||
super(webSocketURL, version, subProtocol);
|
|
||||||
this.allowExtensions = allowExtensions;
|
this.allowExtensions = allowExtensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,14 +116,12 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
|||||||
this.expectedChallengeResponseString = base64Encode(sha1);
|
this.expectedChallengeResponseString = base64Encode(sha1);
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format(
|
logger.debug(String.format("WS Version 13 Client Handshake key: %s. Expected response: %s.", key,
|
||||||
"WS Version 13 Client Handshake key: %s. Expected response: %s.",
|
this.expectedChallengeResponseString));
|
||||||
key, this.expectedChallengeResponseString));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format request
|
// Format request
|
||||||
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
|
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
|
||||||
HttpMethod.GET, path);
|
|
||||||
request.addHeader(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
|
request.addHeader(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
|
||||||
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_KEY, key);
|
request.addHeader(Names.SEC_WEBSOCKET_KEY, key);
|
||||||
@ -132,11 +131,15 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
|||||||
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol);
|
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol);
|
||||||
}
|
}
|
||||||
request.addHeader(Names.SEC_WEBSOCKET_VERSION, "13");
|
request.addHeader(Names.SEC_WEBSOCKET_VERSION, "13");
|
||||||
|
if (customHeaders != null) {
|
||||||
|
for (String header : customHeaders.keySet()) {
|
||||||
|
request.addHeader(header, customHeaders.get(header));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
channel.write(request);
|
channel.write(request);
|
||||||
|
|
||||||
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder",
|
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket13FrameEncoder(true));
|
||||||
new WebSocket13FrameEncoder(true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,36 +163,28 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
|||||||
* @throws WebSocketHandshakeException
|
* @throws WebSocketHandshakeException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void performClosingHandshake(Channel channel, HttpResponse response)
|
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
|
||||||
throws WebSocketHandshakeException {
|
final HttpResponseStatus status = new HttpResponseStatus(101, "Switching Protocols");
|
||||||
final HttpResponseStatus status = new HttpResponseStatus(101,
|
|
||||||
"Switching Protocols");
|
|
||||||
|
|
||||||
if (!response.getStatus().equals(status)) {
|
if (!response.getStatus().equals(status)) {
|
||||||
throw new WebSocketHandshakeException(
|
throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
|
||||||
"Invalid handshake response status: "
|
|
||||||
+ response.getStatus());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String upgrade = response.getHeader(Names.UPGRADE);
|
String upgrade = response.getHeader(Names.UPGRADE);
|
||||||
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET.toLowerCase())) {
|
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET.toLowerCase())) {
|
||||||
throw new WebSocketHandshakeException(
|
throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
|
||||||
"Invalid handshake response upgrade: "
|
+ response.getHeader(Names.UPGRADE));
|
||||||
+ response.getHeader(Names.UPGRADE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String connection = response.getHeader(Names.CONNECTION);
|
String connection = response.getHeader(Names.CONNECTION);
|
||||||
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
||||||
throw new WebSocketHandshakeException(
|
throw new WebSocketHandshakeException("Invalid handshake response connection: "
|
||||||
"Invalid handshake response connection: "
|
+ response.getHeader(Names.CONNECTION));
|
||||||
+ response.getHeader(Names.CONNECTION));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String accept = response.getHeader(Names.SEC_WEBSOCKET_ACCEPT);
|
String accept = response.getHeader(Names.SEC_WEBSOCKET_ACCEPT);
|
||||||
if (accept == null
|
if (accept == null || !accept.equals(this.expectedChallengeResponseString)) {
|
||||||
|| !accept.equals(this.expectedChallengeResponseString)) {
|
throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept,
|
||||||
throw new WebSocketHandshakeException(String.format(
|
|
||||||
"Invalid challenge. Actual: %s. Expected: %s", accept,
|
|
||||||
this.expectedChallengeResponseString));
|
this.expectedChallengeResponseString));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,42 +16,46 @@
|
|||||||
package org.jboss.netty.handler.codec.http.websocketx;
|
package org.jboss.netty.handler.codec.http.websocketx;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instances the appropriate handshake class to use for clients
|
* Instances the appropriate handshake class to use for clients
|
||||||
*/
|
*/
|
||||||
public class WebSocketClientHandshakerFactory {
|
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
|
||||||
* @throws WebSocketHandshakeException
|
* @param customHeaders
|
||||||
*/
|
* custom HTTP headers
|
||||||
public WebSocketClientHandshaker newHandshaker(URI webSocketURL, WebSocketVersion version, String subProtocol, boolean allowExtensions) throws WebSocketHandshakeException {
|
* @throws WebSocketHandshakeException
|
||||||
if (version == WebSocketVersion.V13) {
|
*/
|
||||||
return new WebSocketClientHandshaker13(webSocketURL, version, subProtocol, allowExtensions);
|
public WebSocketClientHandshaker newHandshaker(URI webSocketURL, WebSocketVersion version, String subProtocol,
|
||||||
}
|
boolean allowExtensions, Map<String, String> customHeaders) throws WebSocketHandshakeException {
|
||||||
if (version == WebSocketVersion.V08) {
|
if (version == WebSocketVersion.V13) {
|
||||||
return new WebSocketClientHandshaker08(webSocketURL, version, subProtocol, allowExtensions);
|
return new WebSocketClientHandshaker13(webSocketURL, version, subProtocol, allowExtensions, customHeaders);
|
||||||
}
|
}
|
||||||
if (version == WebSocketVersion.V00) {
|
if (version == WebSocketVersion.V08) {
|
||||||
return new WebSocketClientHandshaker00(webSocketURL, version, subProtocol);
|
return new WebSocketClientHandshaker08(webSocketURL, version, subProtocol, allowExtensions, customHeaders);
|
||||||
}
|
}
|
||||||
|
if (version == WebSocketVersion.V00) {
|
||||||
|
return new WebSocketClientHandshaker00(webSocketURL, version, subProtocol, customHeaders);
|
||||||
|
}
|
||||||
|
|
||||||
throw new WebSocketHandshakeException("Protocol version " + version.toString() + " not supported.");
|
throw new WebSocketHandshakeException("Protocol " + version.toString() + " not supported.");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user