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.
142 lines
5.4 KiB
Java
142 lines
5.4 KiB
Java
/*
|
|
* 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.ChannelInboundHandler;
|
|
import io.netty.channel.ChannelOutboundHandler;
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
|
import io.netty.handler.codec.http.HttpHeaders.Names;
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
import static io.netty.handler.codec.http.HttpHeaders.Values.*;
|
|
import static io.netty.handler.codec.http.HttpVersion.*;
|
|
|
|
/**
|
|
* <p>
|
|
* Performs server side opening and closing handshakes for <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>
|
|
* (originally web socket specification <a href="http://goo.gl/zVBkL">draft-ietf-hybi-thewebsocketprotocol-17</a>).
|
|
* </p>
|
|
*/
|
|
public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
|
|
|
|
public static final String WEBSOCKET_13_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
private final boolean allowExtensions;
|
|
|
|
/**
|
|
* Constructor specifying the destination web socket location
|
|
*
|
|
* @param webSocketURL
|
|
* URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web
|
|
* socket frames will be sent to this URL.
|
|
* @param subprotocols
|
|
* CSV of supported protocols
|
|
* @param allowExtensions
|
|
* Allow extensions to be used in the reserved bits of the web socket frame
|
|
* @param maxFramePayloadLength
|
|
* Maximum allowable frame payload length. Setting this value to your application's
|
|
* requirement may reduce denial of service attacks using long data frames.
|
|
*/
|
|
public WebSocketServerHandshaker13(
|
|
String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength) {
|
|
super(WebSocketVersion.V13, webSocketURL, subprotocols, maxFramePayloadLength);
|
|
this.allowExtensions = allowExtensions;
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Handle the web socket handshake for the web socket specification <a href=
|
|
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">HyBi versions 13-17</a>. Versions 13-17
|
|
* share the same wire protocol.
|
|
* </p>
|
|
*
|
|
* <p>
|
|
* Browser request to the server:
|
|
* </p>
|
|
*
|
|
* <pre>
|
|
* GET /chat HTTP/1.1
|
|
* Host: server.example.com
|
|
* Upgrade: websocket
|
|
* Connection: Upgrade
|
|
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
|
|
* Sec-WebSocket-Origin: http://example.com
|
|
* Sec-WebSocket-Protocol: chat, superchat
|
|
* Sec-WebSocket-Version: 13
|
|
* </pre>
|
|
*
|
|
* <p>
|
|
* Server response:
|
|
* </p>
|
|
*
|
|
* <pre>
|
|
* HTTP/1.1 101 Switching Protocols
|
|
* Upgrade: websocket
|
|
* Connection: Upgrade
|
|
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
|
* Sec-WebSocket-Protocol: chat
|
|
* </pre>
|
|
*/
|
|
@Override
|
|
protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
|
|
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
|
|
if (headers != null) {
|
|
res.headers().add(headers);
|
|
}
|
|
|
|
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
|
|
if (key == null) {
|
|
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
|
|
}
|
|
String acceptSeed = key + WEBSOCKET_13_ACCEPT_GUID;
|
|
byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
|
|
String accept = WebSocketUtil.base64(sha1);
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
logger.debug(String.format("WS Version 13 Server Handshake key: %s. Response: %s.", key, accept));
|
|
}
|
|
|
|
res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase());
|
|
res.headers().add(Names.CONNECTION, Names.UPGRADE);
|
|
res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept);
|
|
String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL);
|
|
if (subprotocols != null) {
|
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
|
if (selectedSubprotocol == null) {
|
|
throw new WebSocketHandshakeException(
|
|
"Requested subprotocol(s) not supported: " + subprotocols);
|
|
} else {
|
|
res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
@Override
|
|
protected ChannelInboundHandler newWebsocketDecoder() {
|
|
return new WebSocket13FrameDecoder(true, allowExtensions, maxFramePayloadLength());
|
|
}
|
|
|
|
@Override
|
|
protected ChannelOutboundHandler newWebSocketEncoder() {
|
|
return new WebSocket13FrameEncoder(false);
|
|
}
|
|
}
|