use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketClientExtensionHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
if (WebSocketExtensionUtil.isWebsocketUpgrade(response.headers())) {
String extensionsHeader = response.headers().getAsString(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
if (extensionsHeader != null) {
List<WebSocketExtensionData> extensions = WebSocketExtensionUtil.extractExtensions(extensionsHeader);
List<WebSocketClientExtension> validExtensions = new ArrayList<WebSocketClientExtension>(extensions.size());
int rsv = 0;
for (WebSocketExtensionData extensionData : extensions) {
Iterator<WebSocketClientExtensionHandshaker> extensionHandshakersIterator = extensionHandshakers.iterator();
WebSocketClientExtension validExtension = null;
while (validExtension == null && extensionHandshakersIterator.hasNext()) {
WebSocketClientExtensionHandshaker extensionHandshaker = extensionHandshakersIterator.next();
validExtension = extensionHandshaker.handshakeExtension(extensionData);
}
if (validExtension != null && ((validExtension.rsv() & rsv) == 0)) {
rsv = rsv | validExtension.rsv();
validExtensions.add(validExtension);
} else {
throw new CodecException("invalid WebSocket Extension handhshake for \"" + extensionsHeader + "\"");
}
}
for (WebSocketClientExtension validExtension : validExtensions) {
WebSocketExtensionDecoder decoder = validExtension.newExtensionDecoder();
WebSocketExtensionEncoder encoder = validExtension.newExtensionEncoder();
ctx.pipeline().addAfter(ctx.name(), decoder.getClass().getName(), decoder);
ctx.pipeline().addAfter(ctx.name(), encoder.getClass().getName(), encoder);
}
}
ctx.pipeline().remove(ctx.name());
}
}
super.channelRead(ctx, msg);
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketServerHandshaker13Test method testPerformOpeningHandshake0.
private static void testPerformOpeningHandshake0(boolean subProtocol) {
EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat");
req.headers().set(HttpHeaderNames.HOST, "server.example.com");
req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==");
req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com");
req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "13");
if (subProtocol) {
new WebSocketServerHandshaker13("ws://example.com/chat", "chat", false, Integer.MAX_VALUE, false).handshake(ch, req);
} else {
new WebSocketServerHandshaker13("ws://example.com/chat", null, false, Integer.MAX_VALUE, false).handshake(ch, req);
}
ByteBuf resBuf = ch.readOutbound();
EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
ch2.writeInbound(resBuf);
HttpResponse res = ch2.readInbound();
Assert.assertEquals("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT));
if (subProtocol) {
Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
} else {
Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
}
ReferenceCountUtil.release(res);
req.release();
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketClientExtensionHandlerTest method testMainSuccess.
@Test
public void testMainSuccess() {
// initialize
when(mainHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
when(mainHandshakerMock.handshakeExtension(any(WebSocketExtensionData.class))).thenReturn(mainExtensionMock);
when(fallbackHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
when(mainExtensionMock.newExtensionEncoder()).thenReturn(new DummyEncoder());
when(mainExtensionMock.newExtensionDecoder()).thenReturn(new DummyDecoder());
// execute
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketClientExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
HttpRequest req = newUpgradeRequest(null);
ch.writeOutbound(req);
HttpRequest req2 = ch.readOutbound();
List<WebSocketExtensionData> reqExts = WebSocketExtensionUtil.extractExtensions(req2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
HttpResponse res = newUpgradeResponse("main");
ch.writeInbound(res);
HttpResponse res2 = ch.readInbound();
List<WebSocketExtensionData> resExts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
// test
assertEquals(2, reqExts.size());
assertEquals("main", reqExts.get(0).name());
assertEquals("fallback", reqExts.get(1).name());
assertEquals(1, resExts.size());
assertEquals("main", resExts.get(0).name());
assertTrue(resExts.get(0).parameters().isEmpty());
assertNotNull(ch.pipeline().get(DummyDecoder.class));
assertNotNull(ch.pipeline().get(DummyEncoder.class) != null);
verify(mainHandshakerMock).newRequestData();
verify(mainHandshakerMock).handshakeExtension(any(WebSocketExtensionData.class));
verify(fallbackHandshakerMock).newRequestData();
verify(mainExtensionMock, atLeastOnce()).rsv();
verify(mainExtensionMock).newExtensionEncoder();
verify(mainExtensionMock).newExtensionDecoder();
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketClientExtensionHandlerTest method testIfMainAndFallbackUseRSV1WillFail.
@Test(expected = CodecException.class)
public void testIfMainAndFallbackUseRSV1WillFail() {
// initialize
when(mainHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(null);
when(fallbackHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(null);
when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(fallbackExtensionMock);
when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
// execute
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketClientExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
HttpRequest req = newUpgradeRequest(null);
ch.writeOutbound(req);
HttpRequest req2 = ch.readOutbound();
List<WebSocketExtensionData> reqExts = WebSocketExtensionUtil.extractExtensions(req2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
HttpResponse res = newUpgradeResponse("main, fallback");
ch.writeInbound(res);
// test
assertEquals(2, reqExts.size());
assertEquals("main", reqExts.get(0).name());
assertEquals("fallback", reqExts.get(1).name());
verify(mainHandshakerMock).newRequestData();
verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("main"));
verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
verify(fallbackHandshakerMock).newRequestData();
verify(fallbackHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("main"));
verify(fallbackHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
verify(mainExtensionMock, atLeastOnce()).rsv();
verify(fallbackExtensionMock, atLeastOnce()).rsv();
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketExtensionTestUtil method newUpgradeResponse.
public static HttpResponse newUpgradeResponse(String ext) {
HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
res.headers().set(HttpHeaderNames.HOST, "server.example.com");
res.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET.toString().toLowerCase());
res.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
res.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
if (ext != null) {
res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS, ext);
}
return res;
}
Aggregations