use of io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.
the class WebSocketClientHandshaker08 method newHandshakeRequest.
/**
* /**
* <p>
* Sends the opening request to the server:
* </p>
*
* <pre>
* GET /chat HTTP/1.1
* Host: server.example.com
* Upgrade: websocket
* Connection: Upgrade
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
* Sec-WebSocket-Origin: http://example.com
* Sec-WebSocket-Protocol: chat, superchat
* Sec-WebSocket-Version: 8
* </pre>
*/
@Override
protected FullHttpRequest newHandshakeRequest() {
URI wsURL = uri();
// Get 16 bit nonce and base 64 encode it
byte[] nonce = WebSocketUtil.randomBytes(16);
String key = WebSocketUtil.base64(nonce);
String acceptSeed = key + MAGIC_GUID;
byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
expectedChallengeResponseString = WebSocketUtil.base64(sha1);
if (logger.isDebugEnabled()) {
logger.debug("WebSocket version 08 client handshake key: {}, expected response: {}", key, expectedChallengeResponseString);
}
// Format request
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, upgradeUrl(wsURL), Unpooled.EMPTY_BUFFER);
HttpHeaders headers = request.headers();
if (customHeaders != null) {
headers.add(customHeaders);
if (!headers.contains(HttpHeaderNames.HOST)) {
// Only add HOST header if customHeaders did not contain it.
//
// See https://github.com/netty/netty/issues/10101
headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
}
} else {
headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
}
headers.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key);
if (!headers.contains(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN)) {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(wsURL));
}
String expectedSubprotocol = expectedSubprotocol();
if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
}
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
return request;
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.
the class WebSocketServerHandshaker13Test method testUpgrade0.
private static void testUpgrade0(EmbeddedChannel ch, WebSocketServerHandshaker13 handshaker) {
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");
handshaker.handshake(ch, req);
ByteBuf resBuf = ch.readOutbound();
EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
ch2.writeInbound(resBuf);
HttpResponse res = ch2.readInbound();
assertEquals("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT));
Iterator<String> subProtocols = handshaker.subprotocols().iterator();
if (subProtocols.hasNext()) {
assertEquals(subProtocols.next(), res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
} else {
assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
}
ReferenceCountUtil.release(res);
req.release();
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.
the class WebSocketServerHandshakerTest method testDuplicateHandshakeResponseHeaders.
@Test
public void testDuplicateHandshakeResponseHeaders() {
WebSocketServerHandshaker serverHandshaker = newHandshaker("ws://example.com/chat", "chat", WebSocketDecoderConfig.DEFAULT);
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/chat");
request.headers().set(HttpHeaderNames.HOST, "example.com").set(HttpHeaderNames.ORIGIN, "example.com").set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==").set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com").set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat").set(HttpHeaderNames.WEBSOCKET_PROTOCOL, "chat, superchat").set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, webSocketVersion().toAsciiString());
HttpHeaders customResponseHeaders = new DefaultHttpHeaders();
// set duplicate required headers and one custom
customResponseHeaders.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "superchat").set(HttpHeaderNames.WEBSOCKET_PROTOCOL, "superchat").set("custom", "header");
if (webSocketVersion() != WebSocketVersion.V00) {
customResponseHeaders.set(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, "12345");
}
FullHttpResponse response = null;
try {
response = serverHandshaker.newHandshakeResponse(request, customResponseHeaders);
HttpHeaders responseHeaders = response.headers();
assertEquals(1, responseHeaders.getAll(HttpHeaderNames.CONNECTION).size());
assertEquals(1, responseHeaders.getAll(HttpHeaderNames.UPGRADE).size());
assertTrue(responseHeaders.containsValue("custom", "header", true));
if (webSocketVersion() != WebSocketVersion.V00) {
assertFalse(responseHeaders.containsValue(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, "12345", false));
assertEquals(1, responseHeaders.getAll(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL).size());
assertEquals("chat", responseHeaders.get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
} else {
assertEquals(1, responseHeaders.getAll(HttpHeaderNames.WEBSOCKET_PROTOCOL).size());
assertEquals("chat", responseHeaders.get(HttpHeaderNames.WEBSOCKET_PROTOCOL));
}
} finally {
request.release();
if (response != null) {
response.release();
}
}
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.
the class RtspEncoderTest method testSendGetParameterRequest.
/**
* Test of a GET_PARAMETER request, with body.
*/
@Test
public void testSendGetParameterRequest() {
String expected = "GET_PARAMETER rtsp://172.10.20.30:554 RTSP/1.0\r\n" + "session: 2547019973447939919\r\n" + "cseq: 3\r\n" + "content-length: 31\r\n" + "content-type: text/parameters\r\n" + "\r\n" + "stream_state\r\n" + "position\r\n" + "scale\r\n";
byte[] content = ("stream_state\r\n" + "position\r\n" + "scale\r\n").getBytes(CharsetUtil.UTF_8);
FullHttpRequest request = new DefaultFullHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.GET_PARAMETER, "rtsp://172.10.20.30:554");
request.headers().add(RtspHeaderNames.SESSION, "2547019973447939919");
request.headers().add(RtspHeaderNames.CSEQ, "3");
request.headers().add(RtspHeaderNames.CONTENT_LENGTH, "" + content.length);
request.headers().add(RtspHeaderNames.CONTENT_TYPE, "text/parameters");
request.content().writeBytes(content);
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(request);
ByteBuf buf = ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
use of io.netty.handler.codec.http.DefaultFullHttpRequest in project netty by netty.
the class HttpToHttp2ConnectionHandlerTest method testOriginFormRequestTargetHandledFromUrlencodedUri.
@Test
public void testOriginFormRequestTargetHandledFromUrlencodedUri() throws Exception {
bootstrapEnv(2, 1, 0);
final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/where%2B0?q=now%2B0&f=then%2B0#section1%2B0");
final HttpHeaders httpHeaders = request.headers();
httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "http");
final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/where%2B0?q=now%2B0&f=then%2B0#section1%2B0")).scheme(new AsciiString("http"));
ChannelPromise writePromise = newPromise();
verifyHeadersOnly(http2Headers, writePromise, clientChannel.writeAndFlush(request, writePromise));
}
Aggregations