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;
|
2016-06-16 21:22:15 +02:00
|
|
|
|
import io.netty.handler.codec.http.HttpScheme;
|
2017-02-27 17:56:42 +01:00
|
|
|
|
import io.netty.util.NetUtil;
|
2015-01-06 06:55:01 +01:00
|
|
|
|
import io.netty.util.ReferenceCountUtil;
|
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;
|
2017-10-22 18:39:36 +02:00
|
|
|
|
import java.util.Locale;
|
2019-04-10 15:25:34 +02:00
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
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 {
|
|
|
|
|
|
2017-10-22 18:39:36 +02:00
|
|
|
|
private static final String HTTP_SCHEME_PREFIX = HttpScheme.HTTP + "://";
|
|
|
|
|
private static final String HTTPS_SCHEME_PREFIX = HttpScheme.HTTPS + "://";
|
2019-04-10 15:25:34 +02:00
|
|
|
|
protected static final int DEFAULT_FORCE_CLOSE_TIMEOUT_MILLIS = 10000;
|
2017-10-22 18:39:36 +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
|
|
|
|
|
2019-04-10 15:25:34 +02:00
|
|
|
|
private volatile long forceCloseTimeoutMillis = DEFAULT_FORCE_CLOSE_TIMEOUT_MILLIS;
|
|
|
|
|
|
|
|
|
|
private volatile int forceCloseInit;
|
|
|
|
|
|
|
|
|
|
private static final AtomicIntegerFieldUpdater<WebSocketClientHandshaker> FORCE_CLOSE_INIT_UPDATER =
|
|
|
|
|
AtomicIntegerFieldUpdater.newUpdater(WebSocketClientHandshaker.class, "forceCloseInit");
|
|
|
|
|
|
|
|
|
|
private volatile boolean forceCloseComplete;
|
|
|
|
|
|
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
|
|
|
|
|
2019-06-08 01:01:10 +02:00
|
|
|
|
private final boolean absoluteUpgradeUrl;
|
|
|
|
|
|
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) {
|
2019-04-10 15:25:34 +02:00
|
|
|
|
this(uri, version, subprotocol, customHeaders, maxFramePayloadLength, DEFAULT_FORCE_CLOSE_TIMEOUT_MILLIS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base constructor
|
|
|
|
|
*
|
|
|
|
|
* @param uri
|
|
|
|
|
* 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
|
|
|
|
|
* @param maxFramePayloadLength
|
|
|
|
|
* Maximum length of a frame's payload
|
|
|
|
|
* @param forceCloseTimeoutMillis
|
|
|
|
|
* Close the connection if it was not closed by the server after timeout specified
|
|
|
|
|
*/
|
|
|
|
|
protected WebSocketClientHandshaker(URI uri, WebSocketVersion version, String subprotocol,
|
|
|
|
|
HttpHeaders customHeaders, int maxFramePayloadLength,
|
|
|
|
|
long forceCloseTimeoutMillis) {
|
2019-06-08 01:01:10 +02:00
|
|
|
|
this(uri, version, subprotocol, customHeaders, maxFramePayloadLength, forceCloseTimeoutMillis, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base constructor
|
|
|
|
|
*
|
|
|
|
|
* @param uri
|
|
|
|
|
* 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
|
|
|
|
|
* @param maxFramePayloadLength
|
|
|
|
|
* Maximum length of a frame's payload
|
|
|
|
|
* @param forceCloseTimeoutMillis
|
|
|
|
|
* Close the connection if it was not closed by the server after timeout specified
|
|
|
|
|
* @param absoluteUpgradeUrl
|
|
|
|
|
* Use an absolute url for the Upgrade request, typically when connecting through an HTTP proxy over
|
|
|
|
|
* clear HTTP
|
|
|
|
|
*/
|
|
|
|
|
protected WebSocketClientHandshaker(URI uri, WebSocketVersion version, String subprotocol,
|
|
|
|
|
HttpHeaders customHeaders, int maxFramePayloadLength,
|
|
|
|
|
long forceCloseTimeoutMillis, boolean absoluteUpgradeUrl) {
|
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;
|
2019-04-10 15:25:34 +02:00
|
|
|
|
this.forceCloseTimeoutMillis = forceCloseTimeoutMillis;
|
2019-06-08 01:01:10 +02:00
|
|
|
|
this.absoluteUpgradeUrl = absoluteUpgradeUrl;
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 15:25:34 +02:00
|
|
|
|
public long forceCloseTimeoutMillis() {
|
|
|
|
|
return forceCloseTimeoutMillis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flag to indicate if the closing handshake was initiated because of timeout.
|
|
|
|
|
* For testing only.
|
|
|
|
|
*/
|
|
|
|
|
protected boolean isForceCloseComplete() {
|
|
|
|
|
return forceCloseComplete;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets timeout to close the connection if it was not closed by the server.
|
|
|
|
|
*
|
|
|
|
|
* @param forceCloseTimeoutMillis
|
|
|
|
|
* Close the connection if it was not closed by the server after timeout specified
|
|
|
|
|
*/
|
|
|
|
|
public WebSocketClientHandshaker setForceCloseTimeoutMillis(long forceCloseTimeoutMillis) {
|
|
|
|
|
this.forceCloseTimeoutMillis = forceCloseTimeoutMillis;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2019-07-16 13:12:17 +02:00
|
|
|
|
ChannelPipeline pipeline = channel.pipeline();
|
|
|
|
|
HttpResponseDecoder decoder = pipeline.get(HttpResponseDecoder.class);
|
2013-03-07 10:57:27 +01:00
|
|
|
|
if (decoder == null) {
|
2019-07-16 13:12:17 +02:00
|
|
|
|
HttpClientCodec codec = pipeline.get(HttpClientCodec.class);
|
2013-03-07 10:57:27 +01:00
|
|
|
|
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
|
|
|
|
|
2019-07-16 13:12:17 +02:00
|
|
|
|
FullHttpRequest request = newHandshakeRequest();
|
|
|
|
|
|
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
|
2016-08-01 11:17:05 +02:00
|
|
|
|
for (String protocol : expectedProtocol.split(",")) {
|
2014-09-26 21:56:44 +02:00
|
|
|
|
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
|
2016-03-27 14:25:39 +02:00
|
|
|
|
channel.eventLoop().execute(new Runnable() {
|
2016-02-02 10:39:41 +01:00
|
|
|
|
@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
|
2016-03-27 14:25:39 +02:00
|
|
|
|
channel.eventLoop().execute(new Runnable() {
|
2016-02-02 10:39:41 +01:00
|
|
|
|
@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
|
2019-05-17 22:23:02 +02:00
|
|
|
|
if (!promise.isDone()) {
|
|
|
|
|
promise.tryFailure(new ClosedChannelException());
|
|
|
|
|
}
|
2015-01-06 06:55:01 +01:00
|
|
|
|
ctx.fireChannelInactive();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
ctx.fireChannelRead(ReferenceCountUtil.retain(response));
|
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
|
promise.setFailure(cause);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
|
/**
|
2017-04-19 22:37:03 +02:00
|
|
|
|
* Verify the {@link FullHttpResponse} and throws a {@link WebSocketHandshakeException} if something is wrong.
|
2013-03-07 10:57:27 +01:00
|
|
|
|
*/
|
|
|
|
|
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");
|
|
|
|
|
}
|
2019-04-10 15:25:34 +02:00
|
|
|
|
channel.writeAndFlush(frame, promise);
|
|
|
|
|
applyForceCloseTimeout(channel, promise);
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void applyForceCloseTimeout(final Channel channel, ChannelFuture flushFuture) {
|
|
|
|
|
final long forceCloseTimeoutMillis = this.forceCloseTimeoutMillis;
|
|
|
|
|
final WebSocketClientHandshaker handshaker = this;
|
|
|
|
|
if (forceCloseTimeoutMillis <= 0 || !channel.isActive() || forceCloseInit != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flushFuture.addListener(new ChannelFutureListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
|
|
// If flush operation failed, there is no reason to expect
|
|
|
|
|
// a server to receive CloseFrame. Thus this should be handled
|
|
|
|
|
// by the application separately.
|
|
|
|
|
// Also, close might be called twice from different threads.
|
|
|
|
|
if (future.isSuccess() && channel.isActive() &&
|
|
|
|
|
FORCE_CLOSE_INIT_UPDATER.compareAndSet(handshaker, 0, 1)) {
|
|
|
|
|
final Future<?> forceCloseFuture = channel.eventLoop().schedule(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (channel.isActive()) {
|
|
|
|
|
channel.close();
|
|
|
|
|
forceCloseComplete = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
|
|
channel.closeFuture().addListener(new ChannelFutureListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
|
|
forceCloseFuture.cancel(false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
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}.
|
|
|
|
|
*/
|
2019-06-08 01:01:10 +02:00
|
|
|
|
protected String upgradeUrl(URI wsURL) {
|
|
|
|
|
if (absoluteUpgradeUrl) {
|
|
|
|
|
return wsURL.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-26 22:29:28 +01:00
|
|
|
|
String path = wsURL.getRawPath();
|
2016-11-09 22:37:51 +01:00
|
|
|
|
String query = wsURL.getRawQuery();
|
2015-11-26 22:29:28 +01:00
|
|
|
|
if (query != null && !query.isEmpty()) {
|
|
|
|
|
path = path + '?' + query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path == null || path.isEmpty() ? "/" : path;
|
|
|
|
|
}
|
2016-06-16 21:22:15 +02:00
|
|
|
|
|
2017-02-27 17:56:42 +01:00
|
|
|
|
static CharSequence websocketHostValue(URI wsURL) {
|
|
|
|
|
int port = wsURL.getPort();
|
|
|
|
|
if (port == -1) {
|
|
|
|
|
return wsURL.getHost();
|
|
|
|
|
}
|
|
|
|
|
String host = wsURL.getHost();
|
2019-07-16 13:12:17 +02:00
|
|
|
|
String scheme = wsURL.getScheme();
|
2017-02-27 17:56:42 +01:00
|
|
|
|
if (port == HttpScheme.HTTP.port()) {
|
2019-07-16 13:12:17 +02:00
|
|
|
|
return HttpScheme.HTTP.name().contentEquals(scheme)
|
|
|
|
|
|| WebSocketScheme.WS.name().contentEquals(scheme) ?
|
2017-02-27 17:56:42 +01:00
|
|
|
|
host : NetUtil.toSocketAddressString(host, port);
|
|
|
|
|
}
|
|
|
|
|
if (port == HttpScheme.HTTPS.port()) {
|
2019-07-16 13:12:17 +02:00
|
|
|
|
return HttpScheme.HTTPS.name().contentEquals(scheme)
|
|
|
|
|
|| WebSocketScheme.WSS.name().contentEquals(scheme) ?
|
2017-02-27 17:56:42 +01:00
|
|
|
|
host : NetUtil.toSocketAddressString(host, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if the port is not standard (80/443) its needed to add the port to the header.
|
|
|
|
|
// See http://tools.ietf.org/html/rfc6454#section-6.2
|
2017-03-01 20:05:00 +01:00
|
|
|
|
return NetUtil.toSocketAddressString(host, port);
|
2017-02-27 17:56:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 18:39:36 +02:00
|
|
|
|
static CharSequence websocketOriginValue(URI wsURL) {
|
|
|
|
|
String scheme = wsURL.getScheme();
|
|
|
|
|
final String schemePrefix;
|
|
|
|
|
int port = wsURL.getPort();
|
|
|
|
|
final int defaultPort;
|
|
|
|
|
if (WebSocketScheme.WSS.name().contentEquals(scheme)
|
|
|
|
|
|| HttpScheme.HTTPS.name().contentEquals(scheme)
|
|
|
|
|
|| (scheme == null && port == WebSocketScheme.WSS.port())) {
|
|
|
|
|
|
|
|
|
|
schemePrefix = HTTPS_SCHEME_PREFIX;
|
|
|
|
|
defaultPort = WebSocketScheme.WSS.port();
|
|
|
|
|
} else {
|
|
|
|
|
schemePrefix = HTTP_SCHEME_PREFIX;
|
|
|
|
|
defaultPort = WebSocketScheme.WS.port();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert uri-host to lower case (by RFC 6454, chapter 4 "Origin of a URI")
|
|
|
|
|
String host = wsURL.getHost().toLowerCase(Locale.US);
|
|
|
|
|
|
|
|
|
|
if (port != defaultPort && port != -1) {
|
2016-06-16 21:22:15 +02:00
|
|
|
|
// if the port is not standard (80/443) its needed to add the port to the header.
|
|
|
|
|
// See http://tools.ietf.org/html/rfc6454#section-6.2
|
2017-10-22 18:39:36 +02:00
|
|
|
|
return schemePrefix + NetUtil.toSocketAddressString(host, port);
|
2016-06-16 21:22:15 +02:00
|
|
|
|
}
|
2017-10-22 18:39:36 +02:00
|
|
|
|
return schemePrefix + host;
|
2016-06-16 21:22:15 +02:00
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
|
}
|