2012-07-24 14:38:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.handler.codec.http.websocketx;
|
|
|
|
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2012-09-09 19:48:53 +02:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2019-05-22 12:37:28 +02:00
|
|
|
import io.netty.channel.ChannelPromise;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
2014-10-31 08:48:28 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
2012-07-24 14:38:39 +02:00
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.HttpResponse;
|
2019-05-22 12:37:28 +02:00
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.ServerHandshakeStateEvent;
|
2012-09-09 19:48:53 +02:00
|
|
|
import io.netty.handler.ssl.SslHandler;
|
2019-05-22 12:37:28 +02:00
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.FutureListener;
|
|
|
|
import io.netty.util.concurrent.ScheduledFuture;
|
|
|
|
import io.netty.util.internal.ThrowableUtil;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2012-07-24 14:38:39 +02:00
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
import static io.netty.handler.codec.http.HttpMethod.*;
|
|
|
|
import static io.netty.handler.codec.http.HttpResponseStatus.*;
|
2019-05-22 12:37:28 +02:00
|
|
|
import static io.netty.handler.codec.http.HttpUtil.*;
|
2013-01-16 05:22:50 +01:00
|
|
|
import static io.netty.handler.codec.http.HttpVersion.*;
|
2019-05-22 12:37:28 +02:00
|
|
|
import static io.netty.util.internal.ObjectUtil.*;
|
2013-01-16 05:22:50 +01:00
|
|
|
|
2012-07-24 14:38:39 +02:00
|
|
|
/**
|
2012-12-12 07:43:57 +01:00
|
|
|
* Handles the HTTP handshake (the HTTP Upgrade request) for {@link WebSocketServerProtocolHandler}.
|
2012-07-24 14:38:39 +02:00
|
|
|
*/
|
2014-04-25 07:00:04 +02:00
|
|
|
class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapter {
|
2019-05-22 12:37:28 +02:00
|
|
|
private static final WebSocketHandshakeException HANDSHAKE_TIMED_OUT_EXCEPTION = ThrowableUtil.unknownStackTrace(
|
|
|
|
new WebSocketHandshakeException("handshake timed out"),
|
|
|
|
WebSocketServerProtocolHandshakeHandler.class,
|
|
|
|
"channelRead(...)");
|
|
|
|
|
|
|
|
private static final long DEFAULT_HANDSHAKE_TIMEOUT_MS = 10000L;
|
2012-07-24 14:38:39 +02:00
|
|
|
|
|
|
|
private final String websocketPath;
|
|
|
|
private final String subprotocols;
|
|
|
|
private final boolean allowExtensions;
|
2014-05-04 10:29:30 +02:00
|
|
|
private final int maxFramePayloadSize;
|
2014-10-22 21:59:45 +02:00
|
|
|
private final boolean allowMaskMismatch;
|
2016-12-16 00:59:11 +01:00
|
|
|
private final boolean checkStartsWith;
|
2019-05-22 12:37:28 +02:00
|
|
|
private final long handshakeTimeoutMillis;
|
|
|
|
private ChannelHandlerContext ctx;
|
|
|
|
private ChannelPromise handshakePromise;
|
2012-07-24 14:38:39 +02:00
|
|
|
|
2014-02-07 06:22:01 +01:00
|
|
|
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
2014-10-22 21:59:45 +02:00
|
|
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
|
2019-05-22 12:37:28 +02:00
|
|
|
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch,
|
|
|
|
DEFAULT_HANDSHAKE_TIMEOUT_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
|
|
|
boolean allowExtensions, int maxFrameSize,
|
|
|
|
boolean allowMaskMismatch, long handshakeTimeoutMillis) {
|
|
|
|
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch,
|
|
|
|
false, handshakeTimeoutMillis);
|
2016-12-16 00:59:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
|
|
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) {
|
2019-05-22 12:37:28 +02:00
|
|
|
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch,
|
|
|
|
checkStartsWith, DEFAULT_HANDSHAKE_TIMEOUT_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketServerProtocolHandshakeHandler(String websocketPath, String subprotocols,
|
|
|
|
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch,
|
|
|
|
boolean checkStartsWith, long handshakeTimeoutMillis) {
|
2012-07-24 14:38:39 +02:00
|
|
|
this.websocketPath = websocketPath;
|
|
|
|
this.subprotocols = subprotocols;
|
|
|
|
this.allowExtensions = allowExtensions;
|
2014-06-24 10:39:46 +02:00
|
|
|
maxFramePayloadSize = maxFrameSize;
|
2014-10-22 21:59:45 +02:00
|
|
|
this.allowMaskMismatch = allowMaskMismatch;
|
2016-12-16 00:59:11 +01:00
|
|
|
this.checkStartsWith = checkStartsWith;
|
2019-05-22 12:37:28 +02:00
|
|
|
this.handshakeTimeoutMillis = checkPositive(handshakeTimeoutMillis, "handshakeTimeoutMillis");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
this.ctx = ctx;
|
|
|
|
handshakePromise = ctx.newPromise();
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-09 16:09:28 +02:00
|
|
|
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
|
2016-08-25 18:41:34 +02:00
|
|
|
final FullHttpRequest req = (FullHttpRequest) msg;
|
2017-02-27 17:40:02 +01:00
|
|
|
if (isNotWebSocketPath(req)) {
|
2016-02-27 22:50:11 +01:00
|
|
|
ctx.fireChannelRead(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:22:01 +01:00
|
|
|
try {
|
2019-01-30 21:17:00 +01:00
|
|
|
if (!GET.equals(req.method())) {
|
2014-02-07 06:22:01 +01:00
|
|
|
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
|
|
|
|
return;
|
|
|
|
}
|
2012-07-24 14:38:39 +02:00
|
|
|
|
2014-02-07 06:22:01 +01:00
|
|
|
final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
|
2014-05-04 10:29:30 +02:00
|
|
|
getWebSocketLocation(ctx.pipeline(), req, websocketPath), subprotocols,
|
2014-10-22 21:59:45 +02:00
|
|
|
allowExtensions, maxFramePayloadSize, allowMaskMismatch);
|
2014-02-07 06:22:01 +01:00
|
|
|
final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
|
2019-05-22 12:37:28 +02:00
|
|
|
final ChannelPromise localHandshakePromise = handshakePromise;
|
2014-02-07 06:22:01 +01:00
|
|
|
if (handshaker == null) {
|
|
|
|
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
|
|
|
|
} else {
|
|
|
|
final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req);
|
|
|
|
handshakeFuture.addListener(new ChannelFutureListener() {
|
|
|
|
@Override
|
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
|
if (!future.isSuccess()) {
|
2019-05-22 12:37:28 +02:00
|
|
|
localHandshakePromise.tryFailure(future.cause());
|
2014-02-07 06:22:01 +01:00
|
|
|
ctx.fireExceptionCaught(future.cause());
|
|
|
|
} else {
|
2019-05-22 12:37:28 +02:00
|
|
|
localHandshakePromise.trySuccess();
|
2016-08-25 18:41:34 +02:00
|
|
|
// Kept for compatibility
|
2014-02-07 06:22:01 +01:00
|
|
|
ctx.fireUserEventTriggered(
|
|
|
|
WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
|
2016-08-25 18:41:34 +02:00
|
|
|
ctx.fireUserEventTriggered(
|
|
|
|
new WebSocketServerProtocolHandler.HandshakeComplete(
|
|
|
|
req.uri(), req.headers(), handshaker.selectedSubprotocol()));
|
2014-02-07 06:22:01 +01:00
|
|
|
}
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
2014-02-07 06:22:01 +01:00
|
|
|
});
|
2019-05-22 12:37:28 +02:00
|
|
|
applyHandshakeTimeout();
|
2015-09-09 14:25:32 +02:00
|
|
|
WebSocketServerProtocolHandler.setHandshaker(ctx.channel(), handshaker);
|
2014-02-07 06:22:01 +01:00
|
|
|
ctx.pipeline().replace(this, "WS403Responder",
|
|
|
|
WebSocketServerProtocolHandler.forbiddenHttpRequestResponder());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
req.release();
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:40:02 +01:00
|
|
|
private boolean isNotWebSocketPath(FullHttpRequest req) {
|
|
|
|
return checkStartsWith ? !req.uri().startsWith(websocketPath) : !req.uri().equals(websocketPath);
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
|
2013-07-10 13:00:42 +02:00
|
|
|
ChannelFuture f = ctx.channel().writeAndFlush(res);
|
2014-06-24 10:39:46 +02:00
|
|
|
if (!isKeepAlive(req) || res.status().code() != 200) {
|
2012-07-24 14:38:39 +02:00
|
|
|
f.addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
|
2012-09-09 19:48:53 +02:00
|
|
|
String protocol = "ws";
|
|
|
|
if (cp.get(SslHandler.class) != null) {
|
|
|
|
// SSL in use so use Secure WebSockets
|
|
|
|
protocol = "wss";
|
|
|
|
}
|
2017-02-27 17:40:02 +01:00
|
|
|
String host = req.headers().get(HttpHeaderNames.HOST);
|
|
|
|
return protocol + "://" + host + path;
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|
2019-05-22 12:37:28 +02:00
|
|
|
|
|
|
|
private void applyHandshakeTimeout() {
|
|
|
|
final ChannelPromise localHandshakePromise = handshakePromise;
|
|
|
|
final long handshakeTimeoutMillis = this.handshakeTimeoutMillis;
|
|
|
|
if (handshakeTimeoutMillis <= 0 || localHandshakePromise.isDone()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Future<?> timeoutFuture = ctx.executor().schedule(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (localHandshakePromise.tryFailure(HANDSHAKE_TIMED_OUT_EXCEPTION)) {
|
|
|
|
ctx.flush()
|
|
|
|
.fireUserEventTriggered(ServerHandshakeStateEvent.HANDSHAKE_TIMEOUT)
|
|
|
|
.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
// Cancel the handshake timeout when handshake is finished.
|
|
|
|
localHandshakePromise.addListener(new FutureListener<Void>() {
|
|
|
|
@Override
|
|
|
|
public void operationComplete(Future<Void> f) throws Exception {
|
|
|
|
timeoutFuture.cancel(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-07-24 14:38:39 +02:00
|
|
|
}
|