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-09 04:38:59 +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.FullHttpResponse;
|
2013-01-16 16:33:40 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaders;
|
2011-09-26 14:51:15 +02:00
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
import java.net.URI;
|
|
|
|
|
2011-09-26 14:51:15 +02:00
|
|
|
/**
|
|
|
|
* Base class for web socket client handshake implementations
|
|
|
|
*/
|
|
|
|
public abstract class WebSocketClientHandshaker {
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final URI webSocketUrl;
|
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-01-19 05:12:45 +01:00
|
|
|
private boolean handshakeComplete;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private final String expectedSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
private String actualSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
protected final HttpHeaders customHeaders;
|
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-01-19 05:12:45 +01:00
|
|
|
/**
|
|
|
|
* Base constructor
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @param subprotocol
|
|
|
|
* Sub protocol request sent to the server.
|
|
|
|
* @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
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
2012-11-09 23:10:30 +01:00
|
|
|
protected WebSocketClientHandshaker(URI webSocketUrl, WebSocketVersion version, String subprotocol,
|
2013-01-16 16:33:40 +01:00
|
|
|
HttpHeaders customHeaders, int maxFramePayloadLength) {
|
2012-01-19 05:12:45 +01:00
|
|
|
this.webSocketUrl = webSocketUrl;
|
2011-12-15 12:25:40 +01:00
|
|
|
this.version = version;
|
2012-01-19 05:12:45 +01:00
|
|
|
expectedSubprotocol = subprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
this.customHeaders = customHeaders;
|
2012-05-31 02:13:00 +02:00
|
|
|
this.maxFramePayloadLength = maxFramePayloadLength;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URI to the web socket. e.g. "ws://myhost.com/path"
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public URI getWebSocketUrl() {
|
|
|
|
return webSocketUrl;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Version of the web socket specification that is being used
|
|
|
|
*/
|
|
|
|
public WebSocketVersion getVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2012-05-31 02:13:00 +02:00
|
|
|
/**
|
|
|
|
* Returns the max length for any frame's payload
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Flag to indicate if the opening handshake is complete
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public boolean isHandshakeComplete() {
|
|
|
|
return handshakeComplete;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
protected void setHandshakeComplete() {
|
|
|
|
handshakeComplete = true;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-12 13:05:15 +02:00
|
|
|
* Returns the CSV of requested subprotocol(s) sent to the server as specified in the constructor
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public String getExpectedSubprotocol() {
|
|
|
|
return expectedSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-12 13:05:15 +02:00
|
|
|
* Returns the subprotocol response sent by the server. Only available after end of handshake.
|
|
|
|
* Null if no subprotocol was requested or confirmed by the server.
|
2011-12-15 12:25:40 +01:00
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
public String getActualSubprotocol() {
|
|
|
|
return actualSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
protected void setActualSubprotocol(String actualSubprotocol) {
|
|
|
|
this.actualSubprotocol = actualSubprotocol;
|
2011-12-15 12:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-19 05:12:45 +01:00
|
|
|
* Begins the opening handshake
|
2012-02-21 03:06:26 +01:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
*/
|
2012-12-14 11:42:58 +01:00
|
|
|
public ChannelFuture handshake(Channel channel) {
|
|
|
|
if (channel == null) {
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
}
|
2012-12-30 17:40:24 +01:00
|
|
|
return handshake(channel, channel.newPromise());
|
2012-12-14 11:42:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins the opening handshake
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
* Channel
|
2012-12-30 17:40:24 +01:00
|
|
|
* @param promise
|
|
|
|
* the {@link ChannelPromise} to be notified when the opening handshake is sent
|
2012-12-14 11:42:58 +01:00
|
|
|
*/
|
2012-12-30 17:40:24 +01:00
|
|
|
public abstract ChannelFuture handshake(Channel channel, ChannelPromise promise);
|
2011-12-15 12:25:40 +01:00
|
|
|
|
|
|
|
/**
|
2012-01-19 05:12:45 +01:00
|
|
|
* Validates and finishes the opening handshake initiated by {@link #handshake}}.
|
2012-02-21 03:06:26 +01:00
|
|
|
*
|
2011-12-15 12:25:40 +01:00
|
|
|
* @param channel
|
|
|
|
* Channel
|
|
|
|
* @param response
|
|
|
|
* HTTP response containing the closing handshake details
|
|
|
|
*/
|
2013-01-16 05:22:50 +01:00
|
|
|
public abstract void finishHandshake(Channel channel, FullHttpResponse response);
|
2011-09-26 14:51:15 +02:00
|
|
|
}
|