2011-09-26 14:51:15 +02:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 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
|
|
|
*
|
2011-12-09 06:18:34 +01: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
|
|
|
|
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;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-09-26 14:51:15 +02:00
|
|
|
/**
|
|
|
|
* Base class for server side web socket opening and closing handshakes
|
|
|
|
*/
|
|
|
|
public abstract class WebSocketServerHandshaker {
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final String webSocketUrl;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final String[] subprotocols;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final WebSocketVersion version;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
private final long maxFramePayloadLength;
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
/**
|
|
|
|
* Constructor specifying the destination web socket location
|
2012-02-21 03:06:26 +01:00
|
|
|
*
|
2012-01-19 05:12:45 +01:00
|
|
|
* @param version
|
|
|
|
* the protocol version
|
|
|
|
* @param webSocketUrl
|
2011-12-15 12:25:40 +01:00
|
|
|
* 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. Null if sub protocols not supported.
|
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
|
|
|
protected WebSocketServerHandshaker(
|
2012-05-31 02:13:00 +02:00
|
|
|
WebSocketVersion version, String webSocketUrl, String subprotocols,
|
|
|
|
long maxFramePayloadLength) {
|
2012-01-19 05:12:45 +01:00
|
|
|
this.version = version;
|
|
|
|
this.webSocketUrl = webSocketUrl;
|
|
|
|
if (subprotocols != null) {
|
|
|
|
String[] subprotocolArray = subprotocols.split(",");
|
|
|
|
for (int i = 0; i < subprotocolArray.length; i++) {
|
|
|
|
subprotocolArray[i] = subprotocolArray[i].trim();
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2012-01-19 05:12:45 +01:00
|
|
|
this.subprotocols = subprotocolArray;
|
|
|
|
} else {
|
|
|
|
this.subprotocols = new String[0];
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
2012-05-31 02:13:00 +02:00
|
|
|
this.maxFramePayloadLength = maxFramePayloadLength;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL of the web socket
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public String getWebSocketUrl() {
|
|
|
|
return webSocketUrl;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the CSV of supported sub protocols
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public Set<String> getSubprotocols() {
|
|
|
|
Set<String> ret = new LinkedHashSet<String>();
|
2012-05-31 02:13:00 +02:00
|
|
|
for (String p: subprotocols) {
|
2012-01-19 05:12:45 +01:00
|
|
|
ret.add(p);
|
|
|
|
}
|
|
|
|
return ret;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the version of the specification being supported
|
|
|
|
*/
|
|
|
|
public WebSocketVersion getVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
/**
|
|
|
|
* Returns the max length for any frame's payload.
|
|
|
|
*/
|
|
|
|
public long getMaxFramePayloadLength() {
|
|
|
|
return maxFramePayloadLength;
|
|
|
|
}
|
|
|
|
|
2011-12-15 12:25:40 +01:00
|
|
|
/**
|
|
|
|
* Performs the opening handshake
|
2012-02-21 03:06:26 +01:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
* @param req
|
|
|
|
* HTTP Request
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public abstract ChannelFuture handshake(Channel channel, HttpRequest req);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the closing handshake
|
2012-02-21 03:06:26 +01:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
* @param frame
|
|
|
|
* Closing Frame that was received
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public abstract ChannelFuture close(Channel channel, CloseWebSocketFrame frame);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects the first matching supported sub protocol
|
2012-02-21 03:06:26 +01:00
|
|
|
*
|
2012-01-19 05:12:45 +01:00
|
|
|
* @param requestedSubprotocols
|
2011-12-15 12:25:40 +01:00
|
|
|
* CSV of protocols to be supported. e.g. "chat, superchat"
|
|
|
|
* @return First matching supported sub protocol. Null if not found.
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
protected String selectSubprotocol(String requestedSubprotocols) {
|
|
|
|
if (requestedSubprotocols == null || subprotocols.length == 0) {
|
2011-12-15 12:25:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
String[] requesteSubprotocolArray = requestedSubprotocols.split(",");
|
|
|
|
for (String p: requesteSubprotocolArray) {
|
|
|
|
String requestedSubprotocol = p.trim();
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
for (String supportedSubprotocol: subprotocols) {
|
|
|
|
if (requestedSubprotocol.equals(supportedSubprotocol)) {
|
|
|
|
return requestedSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match found
|
|
|
|
return null;
|
|
|
|
}
|
2011-09-26 14:51:15 +02:00
|
|
|
}
|