Search in sources :

Example 56 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project netty by netty.

the class WebSocketClientHandshaker00 method newHandshakeRequest.

/**
 * <p>
 * Sends the opening request to the server:
 * </p>
 *
 * <pre>
 * GET /demo HTTP/1.1
 * Upgrade: WebSocket
 * Connection: Upgrade
 * Host: example.com
 * Origin: http://example.com
 * Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
 * Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
 *
 * ^n:ds[4U
 * </pre>
 */
@Override
protected FullHttpRequest newHandshakeRequest() {
    // Make keys
    int spaces1 = WebSocketUtil.randomNumber(1, 12);
    int spaces2 = WebSocketUtil.randomNumber(1, 12);
    int max1 = Integer.MAX_VALUE / spaces1;
    int max2 = Integer.MAX_VALUE / spaces2;
    int number1 = WebSocketUtil.randomNumber(0, max1);
    int number2 = WebSocketUtil.randomNumber(0, max2);
    int product1 = number1 * spaces1;
    int product2 = number2 * spaces2;
    String key1 = Integer.toString(product1);
    String key2 = Integer.toString(product2);
    key1 = insertRandomCharacters(key1);
    key2 = insertRandomCharacters(key2);
    key1 = insertSpaces(key1, spaces1);
    key2 = insertSpaces(key2, spaces2);
    byte[] key3 = WebSocketUtil.randomBytes(8);
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(number1);
    byte[] number1Array = buffer.array();
    buffer = ByteBuffer.allocate(4);
    buffer.putInt(number2);
    byte[] number2Array = buffer.array();
    byte[] challenge = new byte[16];
    System.arraycopy(number1Array, 0, challenge, 0, 4);
    System.arraycopy(number2Array, 0, challenge, 4, 4);
    System.arraycopy(key3, 0, challenge, 8, 8);
    expectedChallengeResponseBytes = Unpooled.wrappedBuffer(WebSocketUtil.md5(challenge));
    URI wsURL = uri();
    // Format request
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, upgradeUrl(wsURL), Unpooled.wrappedBuffer(key3));
    HttpHeaders headers = request.headers();
    if (customHeaders != null) {
        headers.add(customHeaders);
    }
    headers.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.HOST, websocketHostValue(wsURL)).set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, key1).set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, key2);
    if (!headers.contains(HttpHeaderNames.ORIGIN)) {
        headers.set(HttpHeaderNames.ORIGIN, websocketOriginValue(wsURL));
    }
    String expectedSubprotocol = expectedSubprotocol();
    if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
        headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
    }
    // Set Content-Length to workaround some known defect.
    // See also: https://www.ietf.org/mail-archive/web/hybi/current/msg02149.html
    headers.set(HttpHeaderNames.CONTENT_LENGTH, key3.length);
    return request;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI)

Example 57 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest 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;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) URI(java.net.URI)

Example 58 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest 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();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ByteBuf(io.netty.buffer.ByteBuf)

Example 59 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest 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();
        }
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.jupiter.api.Test)

Example 60 with FullHttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest 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);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)287 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)180 Test (org.junit.jupiter.api.Test)74 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)69 Test (org.junit.Test)64 ByteBuf (io.netty.buffer.ByteBuf)54 HttpResponse (io.netty.handler.codec.http.HttpResponse)49 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)43 URI (java.net.URI)35 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)31 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)30 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)30 AsciiString (io.netty.util.AsciiString)25 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)23 Map (java.util.Map)22 ChannelPromise (io.netty.channel.ChannelPromise)21 HttpMethod (io.netty.handler.codec.http.HttpMethod)20 IOException (java.io.IOException)19 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 ResponseParts (com.github.ambry.rest.NettyClient.ResponseParts)16