[#1025] Allow to add extra headers to the websocket handshake response
This commit is contained in:
parent
3a12a2db46
commit
42b21222dc
@ -19,6 +19,7 @@ import io.netty.channel.Channel;
|
|||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelPromise;
|
import io.netty.channel.ChannelPromise;
|
||||||
import io.netty.handler.codec.http.FullHttpRequest;
|
import io.netty.handler.codec.http.FullHttpRequest;
|
||||||
|
import io.netty.handler.codec.http.HttpHeaders;
|
||||||
import io.netty.util.internal.StringUtil;
|
import io.netty.util.internal.StringUtil;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -113,10 +114,7 @@ public abstract class WebSocketServerHandshaker {
|
|||||||
* HTTP Request
|
* HTTP Request
|
||||||
*/
|
*/
|
||||||
public ChannelFuture handshake(Channel channel, FullHttpRequest req) {
|
public ChannelFuture handshake(Channel channel, FullHttpRequest req) {
|
||||||
if (channel == null) {
|
return handshake(channel, req, null, channel.newPromise());
|
||||||
throw new NullPointerException("channel");
|
|
||||||
}
|
|
||||||
return handshake(channel, req, channel.newPromise());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,10 +124,13 @@ public abstract class WebSocketServerHandshaker {
|
|||||||
* Channel
|
* Channel
|
||||||
* @param req
|
* @param req
|
||||||
* HTTP Request
|
* HTTP Request
|
||||||
|
* @param responseHeaders
|
||||||
|
* Extra headers to add to the handshake response or {@code null} if no extra headers should be added
|
||||||
* @param promise
|
* @param promise
|
||||||
* the {@link ChannelPromise} to be notified when the opening handshake is done
|
* 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
|
* Performs the closing handshake
|
||||||
|
@ -25,6 +25,7 @@ import io.netty.channel.ChannelPromise;
|
|||||||
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
||||||
import io.netty.handler.codec.http.FullHttpRequest;
|
import io.netty.handler.codec.http.FullHttpRequest;
|
||||||
import io.netty.handler.codec.http.FullHttpResponse;
|
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.Names;
|
||||||
import io.netty.handler.codec.http.HttpHeaders.Values;
|
import io.netty.handler.codec.http.HttpHeaders.Values;
|
||||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||||
@ -112,14 +113,9 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
|
|||||||
*
|
*
|
||||||
* 8jKS'y:G*Co,Wxa-
|
* 8jKS'y:G*Co,Wxa-
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
|
||||||
* @param channel
|
|
||||||
* Channel
|
|
||||||
* @param req
|
|
||||||
* HTTP request
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
|
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Channel %s WS Version 00 server handshake", channel.id()));
|
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.
|
// Create the WebSocket handshake response.
|
||||||
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, new HttpResponseStatus(101,
|
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, new HttpResponseStatus(101,
|
||||||
isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake"));
|
isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake"));
|
||||||
|
if (headers != null) {
|
||||||
|
res.headers().add(headers);
|
||||||
|
}
|
||||||
|
|
||||||
res.headers().add(Names.UPGRADE, WEBSOCKET);
|
res.headers().add(Names.UPGRADE, WEBSOCKET);
|
||||||
res.headers().add(CONNECTION, Values.UPGRADE);
|
res.headers().add(CONNECTION, Values.UPGRADE);
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import io.netty.channel.ChannelPromise;
|
|||||||
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
||||||
import io.netty.handler.codec.http.FullHttpRequest;
|
import io.netty.handler.codec.http.FullHttpRequest;
|
||||||
import io.netty.handler.codec.http.FullHttpResponse;
|
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.Names;
|
||||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||||
import io.netty.handler.codec.http.HttpRequestDecoder;
|
import io.netty.handler.codec.http.HttpRequestDecoder;
|
||||||
@ -102,14 +103,9 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
|
|||||||
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
||||||
* Sec-WebSocket-Protocol: chat
|
* Sec-WebSocket-Protocol: chat
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
|
||||||
* @param channel
|
|
||||||
* Channel
|
|
||||||
* @param req
|
|
||||||
* HTTP request
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
|
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Channel %s WS Version 7 server handshake", channel.id()));
|
logger.debug(String.format("Channel %s WS Version 7 server handshake", channel.id()));
|
||||||
@ -118,6 +114,10 @@ public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
|
|||||||
FullHttpResponse res =
|
FullHttpResponse res =
|
||||||
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
|
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
|
||||||
|
|
||||||
|
if (headers != null) {
|
||||||
|
res.headers().add(headers);
|
||||||
|
}
|
||||||
|
|
||||||
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
|
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
|
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
|
||||||
|
@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline;
|
|||||||
import io.netty.channel.ChannelPromise;
|
import io.netty.channel.ChannelPromise;
|
||||||
import io.netty.handler.codec.http.DefaultHttpResponse;
|
import io.netty.handler.codec.http.DefaultHttpResponse;
|
||||||
import io.netty.handler.codec.http.FullHttpRequest;
|
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.HttpHeaders.Names;
|
||||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||||
import io.netty.handler.codec.http.HttpRequestDecoder;
|
import io.netty.handler.codec.http.HttpRequestDecoder;
|
||||||
@ -103,14 +104,9 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
|
|||||||
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
||||||
* Sec-WebSocket-Protocol: chat
|
* Sec-WebSocket-Protocol: chat
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
|
||||||
* @param channel
|
|
||||||
* Channel
|
|
||||||
* @param req
|
|
||||||
* HTTP request
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
|
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Channel %s WS Version 8 server handshake", channel.id()));
|
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);
|
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);
|
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
|
throw new WebSocketHandshakeException("not a WebSocket request: missing key");
|
||||||
|
@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline;
|
|||||||
import io.netty.channel.ChannelPromise;
|
import io.netty.channel.ChannelPromise;
|
||||||
import io.netty.handler.codec.http.DefaultHttpResponse;
|
import io.netty.handler.codec.http.DefaultHttpResponse;
|
||||||
import io.netty.handler.codec.http.FullHttpRequest;
|
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.HttpHeaders.Names;
|
||||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||||
import io.netty.handler.codec.http.HttpRequestDecoder;
|
import io.netty.handler.codec.http.HttpRequestDecoder;
|
||||||
@ -102,20 +103,18 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
|
|||||||
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
|
||||||
* Sec-WebSocket-Protocol: chat
|
* Sec-WebSocket-Protocol: chat
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
|
||||||
* @param channel
|
|
||||||
* Channel
|
|
||||||
* @param req
|
|
||||||
* HTTP request
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) {
|
public ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders headers, ChannelPromise promise) {
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.id()));
|
logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.id()));
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
|
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);
|
String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user