2011-12-15 06:09:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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.Channel;
|
2012-01-19 05:12:45 +01:00
|
|
|
import io.netty.channel.ChannelFuture;
|
2011-12-15 06:09:09 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultHttpRequest;
|
|
|
|
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.HttpRequest;
|
|
|
|
import io.netty.handler.codec.http.HttpRequestEncoder;
|
|
|
|
import io.netty.handler.codec.http.HttpResponse;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseDecoder;
|
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
|
|
|
import io.netty.handler.codec.http.HttpVersion;
|
|
|
|
import io.netty.logging.InternalLogger;
|
|
|
|
import io.netty.logging.InternalLoggerFactory;
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
2012-05-31 00:57:39 +02:00
|
|
|
import java.net.URI;
|
|
|
|
import java.util.Map;
|
|
|
|
|
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-10" >draft-ietf-hybi-thewebsocketprotocol-
|
|
|
|
* 10</a>
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker {
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketClientHandshaker08.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
|
|
|
|
|
|
|
private static final String protocol = null;
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final boolean allowExtensions;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor specifying the destination web socket location and version to initiate
|
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-01-19 05:12:45 +01:00
|
|
|
public WebSocketClientHandshaker08(URI webSocketURL, WebSocketVersion version, String subprotocol,
|
2011-12-15 12:25:40 +01:00
|
|
|
boolean allowExtensions, Map<String, String> customHeaders) {
|
2012-01-19 05:12:45 +01:00
|
|
|
super(webSocketURL, version, subprotocol, customHeaders);
|
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: 8
|
|
|
|
* </pre>
|
2012-05-31 00:57:39 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel into which we can write our request
|
|
|
|
*/
|
|
|
|
@Override
|
2012-01-19 05:12:45 +01:00
|
|
|
public ChannelFuture handshake(Channel channel) {
|
2011-12-15 12:25:40 +01:00
|
|
|
// Get path
|
2012-01-19 05:12:45 +01:00
|
|
|
URI wsURL = getWebSocketUrl();
|
2011-12-15 12:25:40 +01:00
|
|
|
String path = wsURL.getPath();
|
|
|
|
if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
|
|
|
|
path = wsURL.getPath() + "?" + wsURL.getQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 08 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
|
|
|
|
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
|
|
|
|
request.addHeader(Names.UPGRADE, Values.WEBSOCKET.toLowerCase());
|
|
|
|
request.addHeader(Names.CONNECTION, Values.UPGRADE);
|
|
|
|
request.addHeader(Names.SEC_WEBSOCKET_KEY, key);
|
|
|
|
request.addHeader(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
|
|
|
|
originValue = originValue + ":" + wsPort;
|
|
|
|
}
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
if (protocol != null && !protocol.equals("")) {
|
|
|
|
request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol);
|
|
|
|
}
|
|
|
|
request.addHeader(Names.SEC_WEBSOCKET_VERSION, "8");
|
|
|
|
|
|
|
|
if (customHeaders != null) {
|
|
|
|
for (String header : customHeaders.keySet()) {
|
|
|
|
request.addHeader(header, customHeaders.get(header));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
ChannelFuture future = channel.write(request);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-05-16 16:02:06 +02:00
|
|
|
channel.pipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket08FrameEncoder(true));
|
2012-01-19 05:12:45 +01:00
|
|
|
|
|
|
|
return future;
|
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 channel
|
|
|
|
* Channel
|
|
|
|
* @param response
|
|
|
|
* HTTP response returned from the server for the request sent by beginOpeningHandshake00().
|
|
|
|
* @throws WebSocketHandshakeException
|
|
|
|
*/
|
|
|
|
@Override
|
2012-01-19 05:12:45 +01:00
|
|
|
public void finishHandshake(Channel channel, HttpResponse response) {
|
|
|
|
final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
if (!response.getStatus().equals(status)) {
|
|
|
|
throw new WebSocketHandshakeException("Invalid handshake response status: " + response.getStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
String upgrade = response.getHeader(Names.UPGRADE);
|
|
|
|
if (upgrade == null || !upgrade.equals(Values.WEBSOCKET.toLowerCase())) {
|
|
|
|
throw new WebSocketHandshakeException("Invalid handshake response upgrade: "
|
|
|
|
+ response.getHeader(Names.UPGRADE));
|
|
|
|
}
|
|
|
|
|
|
|
|
String connection = response.getHeader(Names.CONNECTION);
|
|
|
|
if (connection == null || !connection.equals(Values.UPGRADE)) {
|
|
|
|
throw new WebSocketHandshakeException("Invalid handshake response connection: "
|
|
|
|
+ response.getHeader(Names.CONNECTION));
|
|
|
|
}
|
|
|
|
|
|
|
|
String accept = response.getHeader(Names.SEC_WEBSOCKET_ACCEPT);
|
2012-01-19 05:12:45 +01:00
|
|
|
if (accept == null || !accept.equals(expectedChallengeResponseString)) {
|
2011-12-15 12:25:40 +01:00
|
|
|
throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept,
|
2012-01-19 05:12:45 +01:00
|
|
|
expectedChallengeResponseString));
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-05-16 16:02:06 +02:00
|
|
|
channel.pipeline().replace(HttpResponseDecoder.class, "ws-decoder",
|
2012-01-19 05:12:45 +01:00
|
|
|
new WebSocket08FrameDecoder(false, allowExtensions));
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
setHandshakeComplete();
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2011-12-15 06:09:09 +01:00
|
|
|
}
|