2011-09-26 14:51:15 +02:00
|
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
|
* Copyright 2012 The Netty Project
|
2011-09-26 14:51:15 +02:00
|
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2011-09-26 14:51:15 +02:00
|
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-26 14:51:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2011-09-26 14:51:15 +02:00
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
|
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
|
package io.netty.handler.codec.http.websocketx;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
|
import io.netty.channel.Channel;
|
2012-01-19 05:12:45 +01:00
|
|
|
|
import io.netty.channel.ChannelFuture;
|
2013-03-07 10:57:27 +01:00
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
2012-12-30 17:40:24 +01:00
|
|
|
|
import io.netty.channel.ChannelPromise;
|
2015-01-06 06:55:01 +01:00
|
|
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
2013-03-07 10:57:27 +01:00
|
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
2013-01-16 05:22:50 +01:00
|
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2013-03-07 10:57:27 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpClientCodec;
|
2013-09-09 11:55:25 +02:00
|
|
|
|
import io.netty.handler.codec.http.HttpContentDecompressor;
|
2014-10-31 08:48:28 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
2013-01-16 16:33:40 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
2015-01-06 06:55:01 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
2013-03-07 10:57:27 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpRequestEncoder;
|
2015-01-06 06:55:01 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpResponse;
|
2013-03-07 10:57:27 +01:00
|
|
|
|
import io.netty.handler.codec.http.HttpResponseDecoder;
|
2015-01-06 06:55:01 +01:00
|
|
|
|
import io.netty.util.ReferenceCountUtil;
|
|
|
|
|
import io.netty.util.internal.EmptyArrays;
|
2016-02-02 10:39:41 +01:00
|
|
|
|
import io.netty.util.internal.OneTimeTask;
|
2014-09-26 21:56:44 +02:00
|
|
|
|
import io.netty.util.internal.StringUtil;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
|
import java.net.URI;
|
2015-01-06 06:55:01 +01:00
|
|
|
|
import java.nio.channels.ClosedChannelException;
|
2012-05-31 02:13:00 +02:00
|
|
|
|
|
2011-09-26 14:51:15 +02:00
|
|
|
|
/**
|
|
|
|
|
* Base class for web socket client handshake implementations
|
|
|
|
|
*/
|
|
|
|
|
public abstract class WebSocketClientHandshaker {
|
2015-01-06 06:55:01 +01:00
|
|
|
|
private static final ClosedChannelException CLOSED_CHANNEL_EXCEPTION = new ClosedChannelException();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
CLOSED_CHANNEL_EXCEPTION.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
|
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
|
|
2013-01-17 07:06:46 +01:00
|
|
|
|
private final URI uri;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
|
private final WebSocketVersion version;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
2013-10-24 10:39:50 +02:00
|
|
|
|
private volatile boolean handshakeComplete;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
|
private final String expectedSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
2013-10-24 10:39:50 +02:00
|
|
|
|
private volatile String actualSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
|
protected final HttpHeaders customHeaders;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
2012-05-31 02:21:51 +02:00
|
|
|
|
private final int maxFramePayloadLength;
|
2012-05-31 02:13:00 +02:00
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
|
/**
|
|
|
|
|
* Base constructor
|
|
|
|
|
*
|
2013-01-17 07:06:46 +01:00
|
|
|
|
* @param uri
|
2012-01-19 05:12:45 +01:00
|
|
|
|
* 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
|
2012-05-31 02:13:00 +02:00
|
|
|
|
* @param maxFramePayloadLength
|
|
|
|
|
* Maximum length of a frame's payload
|
2012-01-19 05:12:45 +01:00
|
|
|
|
*/
|
2013-01-17 07:06:46 +01:00
|
|
|
|
protected WebSocketClientHandshaker(URI uri, WebSocketVersion version, String subprotocol,
|
2013-01-16 16:33:40 +01:00
|
|
|
|
HttpHeaders customHeaders, int maxFramePayloadLength) {
|
2013-01-17 07:06:46 +01:00
|
|
|
|
this.uri = uri;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
this.version = version;
|
2012-01-19 05:12:45 +01:00
|
|
|
|
expectedSubprotocol = subprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
this.customHeaders = customHeaders;
|
2012-05-31 02:13:00 +02:00
|
|
|
|
this.maxFramePayloadLength = maxFramePayloadLength;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the URI to the web socket. e.g. "ws://myhost.com/path"
|
|
|
|
|
*/
|
2013-01-17 07:06:46 +01:00
|
|
|
|
public URI uri() {
|
|
|
|
|
return uri;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Version of the web socket specification that is being used
|
|
|
|
|
*/
|
2013-01-17 07:06:46 +01:00
|
|
|
|
public WebSocketVersion version() {
|
2011-12-15 12:25:40 +01:00
|
|
|
|
return version;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
|
/**
|
|
|
|
|
* Returns the max length for any frame's payload
|
|
|
|
|
*/
|
2013-01-17 07:06:46 +01:00
|
|
|
|
public int maxFramePayloadLength() {
|
2012-05-31 02:13:00 +02:00
|
|
|
|
return maxFramePayloadLength;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
|
/**
|
|
|
|
|
* Flag to indicate if the opening handshake is complete
|
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
|
public boolean isHandshakeComplete() {
|
|
|
|
|
return handshakeComplete;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
|
private void setHandshakeComplete() {
|
2012-01-19 05:12:45 +01:00
|
|
|
|
handshakeComplete = true;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-12 13:05:15 +02:00
|
|
|
|
* Returns the CSV of requested subprotocol(s) sent to the server as specified in the constructor
|
2011-12-15 12:25:40 +01:00
|
|
|
|
*/
|
2013-01-17 07:06:46 +01:00
|
|
|
|
public String expectedSubprotocol() {
|
2012-01-19 05:12:45 +01:00
|
|
|
|
return expectedSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-12 13:05:15 +02:00
|
|
|
|
* Returns the subprotocol response sent by the server. Only available after end of handshake.
|
|
|
|
|
* Null if no subprotocol was requested or confirmed by the server.
|
2011-12-15 12:25:40 +01:00
|
|
|
|
*/
|
2013-01-17 07:06:46 +01:00
|
|
|
|
public String actualSubprotocol() {
|
2012-01-19 05:12:45 +01:00
|
|
|
|
return actualSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
|
private void setActualSubprotocol(String actualSubprotocol) {
|
2012-01-19 05:12:45 +01:00
|
|
|
|
this.actualSubprotocol = actualSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-19 05:12:45 +01:00
|
|
|
|
* Begins the opening handshake
|
2012-02-21 03:06:26 +01:00
|
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
|
|
|
|
*/
|
2012-12-14 11:42:58 +01:00
|
|
|
|
public ChannelFuture handshake(Channel channel) {
|
|
|
|
|
if (channel == null) {
|
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
|
}
|
2012-12-30 17:40:24 +01:00
|
|
|
|
return handshake(channel, channel.newPromise());
|
2012-12-14 11:42:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Begins the opening handshake
|
|
|
|
|
*
|
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
2012-12-30 17:40:24 +01:00
|
|
|
|
* @param promise
|
|
|
|
|
* the {@link ChannelPromise} to be notified when the opening handshake is sent
|
2012-12-14 11:42:58 +01:00
|
|
|
|
*/
|
2013-03-07 10:57:27 +01:00
|
|
|
|
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
|
|
|
|
|
FullHttpRequest request = newHandshakeRequest();
|
2013-09-09 11:55:25 +02:00
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
|
HttpResponseDecoder decoder = channel.pipeline().get(HttpResponseDecoder.class);
|
|
|
|
|
if (decoder == null) {
|
|
|
|
|
HttpClientCodec codec = channel.pipeline().get(HttpClientCodec.class);
|
|
|
|
|
if (codec == null) {
|
|
|
|
|
promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " +
|
|
|
|
|
"a HttpResponseDecoder or HttpClientCodec"));
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-18 12:29:13 +01:00
|
|
|
|
|
2013-07-10 13:00:42 +02:00
|
|
|
|
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
|
2013-03-07 10:57:27 +01:00
|
|
|
|
@Override
|
|
|
|
|
public void operationComplete(ChannelFuture future) {
|
|
|
|
|
if (future.isSuccess()) {
|
|
|
|
|
ChannelPipeline p = future.channel().pipeline();
|
|
|
|
|
ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
|
|
|
|
|
if (ctx == null) {
|
|
|
|
|
ctx = p.context(HttpClientCodec.class);
|
|
|
|
|
}
|
|
|
|
|
if (ctx == null) {
|
|
|
|
|
promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " +
|
|
|
|
|
"a HttpRequestEncoder or HttpClientCodec"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
|
|
|
|
|
|
|
|
|
|
promise.setSuccess();
|
|
|
|
|
} else {
|
|
|
|
|
promise.setFailure(future.cause());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a new {@link FullHttpRequest) which will be used for the handshake.
|
|
|
|
|
*/
|
|
|
|
|
protected abstract FullHttpRequest newHandshakeRequest();
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
|
|
/**
|
2012-01-19 05:12:45 +01:00
|
|
|
|
* Validates and finishes the opening handshake initiated by {@link #handshake}}.
|
2012-02-21 03:06:26 +01:00
|
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
|
|
|
|
* @param response
|
|
|
|
|
* HTTP response containing the closing handshake details
|
|
|
|
|
*/
|
2013-03-07 10:57:27 +01:00
|
|
|
|
public final void finishHandshake(Channel channel, FullHttpResponse response) {
|
|
|
|
|
verify(response);
|
2014-09-26 21:56:44 +02:00
|
|
|
|
|
|
|
|
|
// Verify the subprotocol that we received from the server.
|
|
|
|
|
// This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
|
2014-10-31 08:48:28 +01:00
|
|
|
|
String receivedProtocol = response.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
|
2014-09-26 21:56:44 +02:00
|
|
|
|
receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
|
|
|
|
|
String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
|
|
|
|
|
boolean protocolValid = false;
|
|
|
|
|
|
|
|
|
|
if (expectedProtocol.isEmpty() && receivedProtocol == null) {
|
|
|
|
|
// No subprotocol required and none received
|
|
|
|
|
protocolValid = true;
|
|
|
|
|
setActualSubprotocol(expectedSubprotocol); // null or "" - we echo what the user requested
|
|
|
|
|
} else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
|
|
|
|
|
// We require a subprotocol and received one -> verify it
|
|
|
|
|
for (String protocol : StringUtil.split(expectedSubprotocol, ',')) {
|
|
|
|
|
if (protocol.trim().equals(receivedProtocol)) {
|
|
|
|
|
protocolValid = true;
|
|
|
|
|
setActualSubprotocol(receivedProtocol);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // else mixed cases - which are all errors
|
|
|
|
|
|
|
|
|
|
if (!protocolValid) {
|
|
|
|
|
throw new WebSocketHandshakeException(String.format(
|
|
|
|
|
"Invalid subprotocol. Actual: %s. Expected one of: %s",
|
|
|
|
|
receivedProtocol, expectedSubprotocol));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
|
setHandshakeComplete();
|
|
|
|
|
|
2016-02-02 10:39:41 +01:00
|
|
|
|
final ChannelPipeline p = channel.pipeline();
|
2013-09-09 11:55:25 +02:00
|
|
|
|
// Remove decompressor from pipeline if its in use
|
|
|
|
|
HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
|
|
|
|
|
if (decompressor != null) {
|
|
|
|
|
p.remove(decompressor);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 06:55:01 +01:00
|
|
|
|
// Remove aggregator if present before
|
|
|
|
|
HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
|
|
|
|
|
if (aggregator != null) {
|
|
|
|
|
p.remove(aggregator);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 14:39:18 +01:00
|
|
|
|
ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
|
2013-03-07 10:57:27 +01:00
|
|
|
|
if (ctx == null) {
|
|
|
|
|
ctx = p.context(HttpClientCodec.class);
|
|
|
|
|
if (ctx == null) {
|
|
|
|
|
throw new IllegalStateException("ChannelPipeline does not contain " +
|
|
|
|
|
"a HttpRequestEncoder or HttpClientCodec");
|
|
|
|
|
}
|
2016-02-02 10:39:41 +01:00
|
|
|
|
final HttpClientCodec codec = (HttpClientCodec) ctx.handler();
|
|
|
|
|
// Remove the encoder part of the codec as the user may start writing frames after this method returns.
|
|
|
|
|
codec.removeOutboundHandler();
|
|
|
|
|
|
|
|
|
|
p.addAfter(ctx.name(), "ws-decoder", newWebsocketDecoder());
|
|
|
|
|
|
|
|
|
|
// Delay the removal of the decoder so the user can setup the pipeline if needed to handle
|
|
|
|
|
// WebSocketFrame messages.
|
|
|
|
|
// See https://github.com/netty/netty/issues/4533
|
|
|
|
|
channel.eventLoop().execute(new OneTimeTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
p.remove(codec);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-11 14:39:18 +01:00
|
|
|
|
} else {
|
|
|
|
|
if (p.get(HttpRequestEncoder.class) != null) {
|
2016-02-02 10:39:41 +01:00
|
|
|
|
// Remove the encoder part of the codec as the user may start writing frames after this method returns.
|
2013-03-11 14:39:18 +01:00
|
|
|
|
p.remove(HttpRequestEncoder.class);
|
|
|
|
|
}
|
2016-02-02 10:39:41 +01:00
|
|
|
|
final ChannelHandlerContext context = ctx;
|
|
|
|
|
p.addAfter(context.name(), "ws-decoder", newWebsocketDecoder());
|
|
|
|
|
|
|
|
|
|
// Delay the removal of the decoder so the user can setup the pipeline if needed to handle
|
|
|
|
|
// WebSocketFrame messages.
|
|
|
|
|
// See https://github.com/netty/netty/issues/4533
|
|
|
|
|
channel.eventLoop().execute(new OneTimeTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
p.remove(context.handler());
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-07 10:57:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 06:55:01 +01:00
|
|
|
|
/**
|
|
|
|
|
* Process the opening handshake initiated by {@link #handshake}}.
|
|
|
|
|
*
|
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
|
|
|
|
* @param response
|
|
|
|
|
* HTTP response containing the closing handshake details
|
|
|
|
|
* @return future
|
|
|
|
|
* the {@link ChannelFuture}Â which is notified once the handshake completes.
|
|
|
|
|
*/
|
|
|
|
|
public final ChannelFuture processHandshake(final Channel channel, HttpResponse response) {
|
|
|
|
|
return processHandshake(channel, response, channel.newPromise());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Process the opening handshake initiated by {@link #handshake}}.
|
|
|
|
|
*
|
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
|
|
|
|
* @param response
|
|
|
|
|
* HTTP response containing the closing handshake details
|
|
|
|
|
* @param promise
|
|
|
|
|
* the {@link ChannelPromise} to notify once the handshake completes.
|
|
|
|
|
* @return future
|
|
|
|
|
* the {@link ChannelFuture}Â which is notified once the handshake completes.
|
|
|
|
|
*/
|
|
|
|
|
public final ChannelFuture processHandshake(final Channel channel, HttpResponse response,
|
|
|
|
|
final ChannelPromise promise) {
|
|
|
|
|
if (response instanceof FullHttpResponse) {
|
|
|
|
|
try {
|
|
|
|
|
finishHandshake(channel, (FullHttpResponse) response);
|
|
|
|
|
promise.setSuccess();
|
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
|
promise.setFailure(cause);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ChannelPipeline p = channel.pipeline();
|
|
|
|
|
ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
|
|
|
|
|
if (ctx == null) {
|
|
|
|
|
ctx = p.context(HttpClientCodec.class);
|
|
|
|
|
if (ctx == null) {
|
|
|
|
|
return promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " +
|
|
|
|
|
"a HttpResponseDecoder or HttpClientCodec"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Add aggregator and ensure we feed the HttpResponse so it is aggregated. A limit of 8192 should be more
|
|
|
|
|
// then enough for the websockets handshake payload.
|
|
|
|
|
//
|
|
|
|
|
// TODO: Make handshake work without HttpObjectAggregator at all.
|
|
|
|
|
String aggregatorName = "httpAggregator";
|
|
|
|
|
p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
|
|
|
|
|
p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpResponse>() {
|
|
|
|
|
@Override
|
|
|
|
|
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
|
|
|
|
|
// Remove ourself and do the actual handshake
|
|
|
|
|
ctx.pipeline().remove(this);
|
|
|
|
|
try {
|
|
|
|
|
finishHandshake(channel, msg);
|
|
|
|
|
promise.setSuccess();
|
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
|
promise.setFailure(cause);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
|
// Remove ourself and fail the handshake promise.
|
|
|
|
|
ctx.pipeline().remove(this);
|
|
|
|
|
promise.setFailure(cause);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
|
// Fail promise if Channel was closed
|
|
|
|
|
promise.tryFailure(CLOSED_CHANNEL_EXCEPTION);
|
|
|
|
|
ctx.fireChannelInactive();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
ctx.fireChannelRead(ReferenceCountUtil.retain(response));
|
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
|
promise.setFailure(cause);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
|
/**
|
|
|
|
|
* Verfiy the {@link FullHttpResponse} and throws a {@link WebSocketHandshakeException} if something is wrong.
|
|
|
|
|
*/
|
|
|
|
|
protected abstract void verify(FullHttpResponse response);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the decoder to use after handshake is complete.
|
|
|
|
|
*/
|
2013-07-04 06:41:22 +02:00
|
|
|
|
protected abstract WebSocketFrameDecoder newWebsocketDecoder();
|
2013-03-07 10:57:27 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the encoder to use after the handshake is complete.
|
|
|
|
|
*/
|
2013-07-04 06:41:22 +02:00
|
|
|
|
protected abstract WebSocketFrameEncoder newWebSocketEncoder();
|
2013-03-07 10:57:27 +01:00
|
|
|
|
|
2013-06-25 11:52:27 +02:00
|
|
|
|
/**
|
|
|
|
|
* Performs the closing handshake
|
|
|
|
|
*
|
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
|
|
|
|
* @param frame
|
|
|
|
|
* Closing Frame that was received
|
|
|
|
|
*/
|
|
|
|
|
public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) {
|
|
|
|
|
if (channel == null) {
|
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
|
}
|
|
|
|
|
return close(channel, frame, channel.newPromise());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs the closing handshake
|
|
|
|
|
*
|
|
|
|
|
* @param channel
|
|
|
|
|
* Channel
|
|
|
|
|
* @param frame
|
|
|
|
|
* Closing Frame that was received
|
|
|
|
|
* @param promise
|
|
|
|
|
* the {@link ChannelPromise} to be notified when the closing handshake is done
|
|
|
|
|
*/
|
|
|
|
|
public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) {
|
|
|
|
|
if (channel == null) {
|
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
|
}
|
2013-07-10 13:00:42 +02:00
|
|
|
|
return channel.writeAndFlush(frame, promise);
|
2013-06-25 11:52:27 +02:00
|
|
|
|
}
|
2015-11-26 22:29:28 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the constructed raw path for the give {@link URI}.
|
|
|
|
|
*/
|
|
|
|
|
static String rawPath(URI wsURL) {
|
|
|
|
|
String path = wsURL.getRawPath();
|
|
|
|
|
String query = wsURL.getQuery();
|
|
|
|
|
if (query != null && !query.isEmpty()) {
|
|
|
|
|
path = path + '?' + query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path == null || path.isEmpty() ? "/" : path;
|
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
|
}
|