2011-12-15 06:09:09 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2011-12-15 06:09:09 +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:
|
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-12-15 06:09:09 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
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.ChannelInboundHandler;
|
|
|
|
import io.netty.channel.ChannelOutboundHandler;
|
2013-03-07 10:57:27 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpRequest;
|
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2013-01-16 16:33:40 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
2011-12-15 06:09:09 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders.Names;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaders.Values;
|
|
|
|
import io.netty.handler.codec.http.HttpMethod;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.handler.codec.http.HttpVersion;
|
|
|
|
import io.netty.util.CharsetUtil;
|
2013-02-26 23:54:25 +01:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2011-12-15 06:09:09 +01:00
|
|
|
|
2012-05-31 00:57:39 +02:00
|
|
|
import java.net.URI;
|
|
|
|
|
2011-12-15 06:09:09 +01:00
|
|
|
/**
|
|
|
|
* <p>
|
|
|
|
* Performs client side opening and closing handshakes for web socket specification version <a
|
|
|
|
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17" >draft-ietf-hybi-thewebsocketprotocol-
|
|
|
|
* 17</a>
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker {
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker13.class);
|
|
|
|
|
|
|
|
public static final String MAGIC_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
|
2012-01-11 12:16:14 +01:00
|
|
|
private String expectedChallengeResponseString;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final boolean allowExtensions;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-31 02:13:00 +02:00
|
|
|
* Creates a new instance.
|
2012-05-31 00:57:39 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param 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
|
2012-01-19 05:12:45 +01:00
|
|
|
* @param subprotocol
|
2011-12-15 12:25:40 +01:00
|
|
|
* Sub protocol request sent to the server.
|
|
|
|
* @param allowExtensions
|
|
|
|
* Allow extensions to be used in the reserved bits of the web socket frame
|
|
|
|
* @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
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public WebSocketClientHandshaker13(URI webSocketURL, WebSocketVersion version, String subprotocol,
|
2013-01-16 16:33:40 +01:00
|
|
|
boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength) {
|
2012-05-31 02:13:00 +02:00
|
|
|
super(webSocketURL, version, subprotocol, customHeaders, maxFramePayloadLength);
|
2011-12-15 12:25:40 +01:00
|
|
|
this.allowExtensions = allowExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* /**
|
|
|
|
* <p>
|
|
|
|
* Sends the opening request to the server:
|
|
|
|
* </p>
|
2012-05-31 00:57:39 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <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>
|
2012-05-31 00:57:39 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2013-03-07 10:57:27 +01:00
|
|
|
protected FullHttpRequest newHandshakeRequest() {
|
2011-12-15 12:25:40 +01:00
|
|
|
// Get path
|
2013-01-17 07:06:46 +01:00
|
|
|
URI wsURL = uri();
|
2011-12-15 12:25:40 +01:00
|
|
|
String path = wsURL.getPath();
|
2012-11-09 17:24:04 +01:00
|
|
|
if (wsURL.getQuery() != null && !wsURL.getQuery().isEmpty()) {
|
2012-11-10 00:03:52 +01:00
|
|
|
path = wsURL.getPath() + '?' + wsURL.getQuery();
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-11-12 15:29:02 +01:00
|
|
|
if (path == null || path.isEmpty()) {
|
|
|
|
path = "/";
|
|
|
|
}
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
// Get 16 bit nonce and base 64 encode it
|
2012-01-19 05:12:45 +01:00
|
|
|
byte[] nonce = WebSocketUtil.randomBytes(16);
|
|
|
|
String key = WebSocketUtil.base64(nonce);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
String acceptSeed = key + MAGIC_GUID;
|
2012-01-19 05:12:45 +01:00
|
|
|
byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
|
|
|
|
expectedChallengeResponseString = WebSocketUtil.base64(sha1);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug(String.format("WS Version 13 Client Handshake key: %s. Expected response: %s.", key,
|
2012-01-19 05:12:45 +01:00
|
|
|
expectedChallengeResponseString));
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format request
|
2013-03-07 10:57:27 +01:00
|
|
|
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
|
2013-01-16 16:33:40 +01:00
|
|
|
HttpHeaders headers = request.headers();
|
|
|
|
|
|
|
|
headers.add(Names.UPGRADE, Values.WEBSOCKET.toLowerCase())
|
|
|
|
.add(Names.CONNECTION, Values.UPGRADE)
|
|
|
|
.add(Names.SEC_WEBSOCKET_KEY, key)
|
|
|
|
.add(Names.HOST, wsURL.getHost());
|
2012-05-31 00:57:39 +02:00
|
|
|
|
|
|
|
int wsPort = wsURL.getPort();
|
|
|
|
String originValue = "http://" + wsURL.getHost();
|
|
|
|
if (wsPort != 80 && wsPort != 443) {
|
|
|
|
// 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
|
2012-11-10 00:03:52 +01:00
|
|
|
originValue = originValue + ':' + wsPort;
|
2012-05-31 00:57:39 +02:00
|
|
|
}
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue);
|
2012-05-31 00:57:39 +02:00
|
|
|
|
2013-01-17 07:06:46 +01:00
|
|
|
String expectedSubprotocol = expectedSubprotocol();
|
2012-11-10 00:03:52 +01:00
|
|
|
if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2012-05-12 13:05:15 +02:00
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.add(Names.SEC_WEBSOCKET_VERSION, "13");
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
if (customHeaders != null) {
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.add(customHeaders);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2013-03-07 10:57:27 +01:00
|
|
|
return request;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>
|
|
|
|
* Process server response:
|
|
|
|
* </p>
|
2012-05-31 00:57:39 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <pre>
|
|
|
|
* HTTP/1.1 101 Switching Protocols
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
|
|
|
* Sec-WebSocket-Protocol: chat
|
|
|
|
* </pre>
|
2012-05-31 00:57:39 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param response
|
|
|
|
* HTTP response returned from the server for the request sent by beginOpeningHandshake00().
|
|
|
|
* @throws WebSocketHandshakeException
|
|
|
|
*/
|
|
|
|
@Override
|
2013-03-07 10:57:27 +01:00
|
|
|
protected void verify(FullHttpResponse response) {
|
2012-01-19 05:12:45 +01:00
|
|
|
final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS;
|
2013-01-16 16:33:40 +01:00
|
|
|
final HttpHeaders headers = response.headers();
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2013-01-30 07:42:18 +01:00
|
|
|
if (!response.getStatus().equals(status)) {
|
|
|
|
throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.getStatus());
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
String upgrade = headers.get(Names.UPGRADE);
|
2012-12-07 20:14:11 +01:00
|
|
|
if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) {
|
2013-01-16 16:33:40 +01:00
|
|
|
throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
String connection = headers.get(Names.CONNECTION);
|
2012-12-07 20:14:11 +01:00
|
|
|
if (!Values.UPGRADE.equalsIgnoreCase(connection)) {
|
2013-01-16 16:33:40 +01:00
|
|
|
throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT);
|
2012-01-19 05:12:45 +01:00
|
|
|
if (accept == null || !accept.equals(expectedChallengeResponseString)) {
|
2013-01-16 16:33:40 +01:00
|
|
|
throw new WebSocketHandshakeException(String.format(
|
|
|
|
"Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString));
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2013-03-07 10:57:27 +01:00
|
|
|
}
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
@Override
|
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
|
|
|
protected ChannelInboundHandler newWebsocketDecoder() {
|
2013-03-07 10:57:27 +01:00
|
|
|
return new WebSocket13FrameDecoder(false, allowExtensions, maxFramePayloadLength());
|
|
|
|
}
|
2012-05-31 11:02:02 +02:00
|
|
|
|
2013-03-07 10:57:27 +01:00
|
|
|
@Override
|
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
|
|
|
protected ChannelOutboundHandler newWebSocketEncoder() {
|
2013-03-07 10:57:27 +01:00
|
|
|
return new WebSocket13FrameEncoder(true);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-12-15 06:09:09 +01:00
|
|
|
}
|