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
|
|
|
|
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;
|
2012-11-09 16:41:22 +01:00
|
|
|
import io.netty.util.internal.StringUtil;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2012-06-11 15:54:28 +02:00
|
|
|
import java.util.Collections;
|
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:21:51 +02:00
|
|
|
private final int maxFramePayloadLength;
|
2012-05-31 02:13:00 +02:00
|
|
|
|
2012-05-31 04:09:23 +02:00
|
|
|
private String selectedSubprotocol;
|
2012-05-12 13:05:15 +02:00
|
|
|
|
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,
|
2012-05-31 02:21:51 +02:00
|
|
|
int maxFramePayloadLength) {
|
2012-01-19 05:12:45 +01:00
|
|
|
this.version = version;
|
|
|
|
this.webSocketUrl = webSocketUrl;
|
|
|
|
if (subprotocols != null) {
|
2012-11-09 16:41:22 +01:00
|
|
|
String[] subprotocolArray = StringUtil.split(subprotocols, ',');
|
2012-01-19 05:12:45 +01:00
|
|
|
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-06-11 15:54:28 +02:00
|
|
|
Collections.addAll(ret, subprotocols);
|
2012-01-19 05:12:45 +01:00
|
|
|
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
|
|
|
/**
|
2012-06-29 07:03:41 +02:00
|
|
|
* Gets the maximum length for any frame's payload.
|
|
|
|
*
|
|
|
|
* @return The maximum length for a frame's payload
|
2012-05-31 02:13:00 +02:00
|
|
|
*/
|
2012-05-31 02:21:51 +02:00
|
|
|
public int getMaxFramePayloadLength() {
|
2012-05-31 02:13:00 +02:00
|
|
|
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-11-09 16:41:22 +01:00
|
|
|
String[] requestedSubprotocolArray = StringUtil.split(requestedSubprotocols, ',');
|
2012-05-12 13:05:15 +02:00
|
|
|
for (String p: requestedSubprotocolArray) {
|
2012-01-19 05:12:45 +01:00
|
|
|
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;
|
|
|
|
}
|
2012-05-12 13:05:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the selected subprotocol. Null if no subprotocol has been selected.
|
|
|
|
* <p>
|
|
|
|
* This is only available AFTER <tt>handshake()</tt> has been called.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public String getSelectedSubprotocol() {
|
|
|
|
return selectedSubprotocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setSelectedSubprotocol(String value) {
|
|
|
|
selectedSubprotocol = value;
|
|
|
|
}
|
|
|
|
|
2011-09-26 14:51:15 +02:00
|
|
|
}
|