[#1025] Allow to add extra headers to the websocket handshake response

This commit is contained in:
Norman Maurer 2013-02-08 19:54:05 +01:00
parent 3a12a2db46
commit 42b21222dc
5 changed files with 29 additions and 29 deletions

View File

@ -19,6 +19,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.util.internal.StringUtil;
import java.util.Collections;
@ -113,10 +114,7 @@ public abstract class WebSocketServerHandshaker {
* HTTP Request
*/
public ChannelFuture handshake(Channel channel, FullHttpRequest req) {
if (channel == null) {
throw new NullPointerException("channel");
}
return handshake(channel, req, channel.newPromise());
return handshake(channel, req, null, channel.newPromise());
}
/**
@ -126,10 +124,13 @@ public abstract class WebSocketServerHandshaker {
* Channel
* @param req
* HTTP Request
* @param responseHeaders
* Extra headers to add to the handshake response or {@code null} if no extra headers should be added
* @param promise
* the {@link ChannelPromise} to be notified when the opening handshake is done
*/
public abstract ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise);
public abstract ChannelFuture handshake(Channel channel, FullHttpRequest req,
HttpHeaders responseHeaders, ChannelPromise promise);
/**
* Performs the closing handshake

View File

@ -25,6 +25,7 @@ import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.handler.codec.http.HttpObjectAggregator;
@ -112,14 +113,9 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
*
* 8jKS'y:G*Co,Wxa-
* </pre>
*
* @param channel
* Channel
* @param req
* HTTP request
*/
@Override
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s WS Version 00 server handshake", channel.id()));
@ -137,6 +133,10 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
// Create the WebSocket handshake response.
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, new HttpResponseStatus(101,
isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake"));
if (headers != null) {
res.headers().add(headers);
}
res.headers().add(Names.UPGRADE, WEBSOCKET);
res.headers().add(CONNECTION, Values.UPGRADE);

View File

@ -23,6 +23,7 @@ import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
@ -102,14 +103,9 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param channel
* Channel
* @param req
* HTTP request
*/
@Override
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s WS Version 7 server handshake", channel.id()));
@ -118,6 +114,10 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
FullHttpResponse res =
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
if (headers != null) {
res.headers().add(headers);
}
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
if (key == null) {
throw new WebSocketHandshakeException("not a WebSocket request: missing key");

View File

@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
@ -103,14 +104,9 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param channel
* Channel
* @param req
* HTTP request
*/
@Override
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s WS Version 8 server handshake", channel.id()));
@ -118,6 +114,10 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
if (headers != null) {
res.headers().add(headers);
}
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
if (key == null) {
throw new WebSocketHandshakeException("not a WebSocket request: missing key");

View File

@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
@ -102,20 +103,18 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param channel
* Channel
* @param req
* HTTP request
*/
@Override
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.id()));
}
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
if (headers != null) {
res.headers().add(headers);
}
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
if (key == null) {