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
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2011-12-11 00:47:07 +01:00
|
|
|
import io.netty.channel.Channel;
|
2012-01-19 05:12:45 +01:00
|
|
|
import io.netty.channel.ChannelFuture;
|
2012-12-30 17:40:24 +01: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;
|
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2014-10-31 08:48:28 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaderValues;
|
2013-02-08 19:54:05 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2012-12-14 11:42:58 +01:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2015-08-13 04:05:37 +02:00
|
|
|
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
2012-12-14 11:42:58 +01:00
|
|
|
|
2011-09-26 14:51:15 +02:00
|
|
|
/**
|
|
|
|
* <p>
|
2011-12-15 06:09:09 +01:00
|
|
|
* Performs server side opening and closing handshakes for web socket specification version <a
|
|
|
|
* href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00" >draft-ietf-hybi-thewebsocketprotocol-
|
|
|
|
* 00</a>
|
2011-09-26 14:51:15 +02:00
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* A very large portion of this code was taken from the Netty 3.2 HTTP example.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
|
|
|
|
|
2012-12-14 11:42:58 +01:00
|
|
|
private static final Pattern BEGINNING_DIGIT = Pattern.compile("[^0-9]");
|
|
|
|
private static final Pattern BEGINNING_SPACE = Pattern.compile("[^ ]");
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
/**
|
|
|
|
* Constructor specifying the destination web socket location
|
2012-05-23 20:42:10 +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.
|
2012-01-19 05:12:45 +01:00
|
|
|
* @param subprotocols
|
2011-12-15 12:25:40 +01:00
|
|
|
* CSV of supported protocols
|
2012-05-31 02:13:00 +02:00
|
|
|
* @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.
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
2012-05-31 02:21:51 +02:00
|
|
|
public WebSocketServerHandshaker00(String webSocketURL, String subprotocols, int maxFramePayloadLength) {
|
2012-05-31 02:13:00 +02:00
|
|
|
super(WebSocketVersion.V00, webSocketURL, subprotocols, maxFramePayloadLength);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>
|
|
|
|
* Handle the web socket handshake for the web socket specification <a href=
|
|
|
|
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">HyBi version 0</a> and lower. This standard
|
|
|
|
* is really a rehash of <a href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76" >hixie-76</a> and
|
|
|
|
* <a href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75" >hixie-75</a>.
|
|
|
|
* </p>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <p>
|
|
|
|
* Browser request to the server:
|
|
|
|
* </p>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <pre>
|
|
|
|
* GET /demo HTTP/1.1
|
|
|
|
* Upgrade: WebSocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Host: example.com
|
|
|
|
* Origin: http://example.com
|
2011-12-17 00:39:11 +01:00
|
|
|
* Sec-WebSocket-Protocol: chat, sample
|
2011-12-15 12:25:40 +01:00
|
|
|
* Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
|
|
|
|
* Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* ^n:ds[4U
|
|
|
|
* </pre>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <p>
|
|
|
|
* Server response:
|
|
|
|
* </p>
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* <pre>
|
|
|
|
* HTTP/1.1 101 WebSocket Protocol Handshake
|
|
|
|
* Upgrade: WebSocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Origin: http://example.com
|
|
|
|
* Sec-WebSocket-Location: ws://example.com/demo
|
|
|
|
* Sec-WebSocket-Protocol: sample
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* 8jKS'y:G*Co,Wxa-
|
|
|
|
* </pre>
|
|
|
|
*/
|
|
|
|
@Override
|
2013-03-07 10:57:27 +01:00
|
|
|
protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
// Serve the WebSocket handshake request.
|
2016-01-27 14:26:33 +01:00
|
|
|
if (!req.headers().containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)
|
2015-08-13 04:05:37 +02:00
|
|
|
|| !HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(req.headers().get(HttpHeaderNames.UPGRADE))) {
|
2012-01-19 05:12:45 +01:00
|
|
|
throw new WebSocketHandshakeException("not a WebSocket handshake request: missing upgrade");
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hixie 75 does not contain these headers while Hixie 76 does
|
2014-10-31 08:48:28 +01:00
|
|
|
boolean isHixie76 = req.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_KEY1) &&
|
|
|
|
req.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_KEY2);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
// Create the WebSocket handshake response.
|
2013-01-16 05:22:50 +01:00
|
|
|
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, new HttpResponseStatus(101,
|
2011-12-15 12:25:40 +01:00
|
|
|
isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake"));
|
2013-02-08 19:54:05 +01:00
|
|
|
if (headers != null) {
|
|
|
|
res.headers().add(headers);
|
|
|
|
}
|
|
|
|
|
2014-10-31 08:48:28 +01:00
|
|
|
res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
|
|
|
|
res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2013-01-30 07:42:18 +01:00
|
|
|
// Fill in the headers and contents depending on handshake getMethod.
|
2011-12-15 12:25:40 +01:00
|
|
|
if (isHixie76) {
|
2013-01-30 07:42:18 +01:00
|
|
|
// New handshake getMethod with a challenge:
|
2014-10-31 08:48:28 +01:00
|
|
|
res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, req.headers().get(HttpHeaderNames.ORIGIN));
|
|
|
|
res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_LOCATION, uri());
|
2015-08-13 04:05:37 +02:00
|
|
|
|
2014-10-31 08:48:28 +01:00
|
|
|
String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
|
2012-05-12 13:05:15 +02:00
|
|
|
if (subprotocols != null) {
|
|
|
|
String selectedSubprotocol = selectSubprotocol(subprotocols);
|
|
|
|
if (selectedSubprotocol == null) {
|
2014-01-23 15:02:38 +01:00
|
|
|
if (logger.isDebugEnabled()) {
|
2014-02-14 04:31:17 +01:00
|
|
|
logger.debug("Requested subprotocol(s) not supported: {}", subprotocols);
|
2014-01-23 15:02:38 +01:00
|
|
|
}
|
2012-05-12 13:05:15 +02:00
|
|
|
} else {
|
2014-10-31 08:48:28 +01:00
|
|
|
res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
|
2012-05-12 13:05:15 +02:00
|
|
|
}
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the answer of the challenge.
|
2014-10-31 08:48:28 +01:00
|
|
|
String key1 = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY1);
|
|
|
|
String key2 = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY2);
|
2012-12-14 11:42:58 +01:00
|
|
|
int a = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key1).replaceAll("")) /
|
|
|
|
BEGINNING_SPACE.matcher(key1).replaceAll("").length());
|
|
|
|
int b = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key2).replaceAll("")) /
|
|
|
|
BEGINNING_SPACE.matcher(key2).replaceAll("").length());
|
2013-05-01 10:04:43 +02:00
|
|
|
long c = req.content().readLong();
|
2012-06-11 10:02:00 +02:00
|
|
|
ByteBuf input = Unpooled.buffer(16);
|
2011-12-15 12:25:40 +01:00
|
|
|
input.writeInt(a);
|
|
|
|
input.writeInt(b);
|
|
|
|
input.writeLong(c);
|
2013-05-01 10:04:43 +02:00
|
|
|
res.content().writeBytes(WebSocketUtil.md5(input.array()));
|
2011-12-15 12:25:40 +01:00
|
|
|
} else {
|
2013-01-30 07:42:18 +01:00
|
|
|
// Old Hixie 75 handshake getMethod with no challenge:
|
2014-10-31 08:48:28 +01:00
|
|
|
res.headers().add(HttpHeaderNames.WEBSOCKET_ORIGIN, req.headers().get(HttpHeaderNames.ORIGIN));
|
|
|
|
res.headers().add(HttpHeaderNames.WEBSOCKET_LOCATION, uri());
|
2015-08-13 04:05:37 +02:00
|
|
|
|
2014-10-31 08:48:28 +01:00
|
|
|
String protocol = req.headers().get(HttpHeaderNames.WEBSOCKET_PROTOCOL);
|
2011-12-15 12:25:40 +01:00
|
|
|
if (protocol != null) {
|
2014-10-31 08:48:28 +01:00
|
|
|
res.headers().add(HttpHeaderNames.WEBSOCKET_PROTOCOL, selectSubprotocol(protocol));
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-07 10:57:27 +01:00
|
|
|
return res;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echo back the closing frame
|
2012-05-23 20:42:10 +02:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
* @param frame
|
|
|
|
* Web Socket frame that was received
|
|
|
|
*/
|
|
|
|
@Override
|
2012-12-30 17:40:24 +01:00
|
|
|
public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) {
|
2013-07-10 13:00:42 +02:00
|
|
|
return channel.writeAndFlush(frame, promise);
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2013-03-07 10:57:27 +01:00
|
|
|
|
|
|
|
@Override
|
2013-07-04 06:41:22 +02:00
|
|
|
protected WebSocketFrameDecoder newWebsocketDecoder() {
|
2013-03-07 10:57:27 +01:00
|
|
|
return new WebSocket00FrameDecoder(maxFramePayloadLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-04 06:41:22 +02:00
|
|
|
protected WebSocketFrameEncoder newWebSocketEncoder() {
|
2013-03-07 10:57:27 +01:00
|
|
|
return new WebSocket00FrameEncoder();
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
}
|