Search in sources :

Example 1 with HttpRequestEncoder

use of io.netty.handler.codec.http.HttpRequestEncoder in project netty by netty.

the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.

private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
    String url = "ws://localhost:9999/ws";
    final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
    final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE) {

        @Override
        protected FullHttpRequest newHandshakeRequest() {
            return shaker.newHandshakeRequest();
        }

        @Override
        protected void verify(FullHttpResponse response) {
        // Not do any verification, so we not need to care sending the correct headers etc in the test,
        // which would just make things more complicated.
        }

        @Override
        protected WebSocketFrameDecoder newWebsocketDecoder() {
            return shaker.newWebsocketDecoder();
        }

        @Override
        protected WebSocketFrameEncoder newWebSocketEncoder() {
            return shaker.newWebSocketEncoder();
        }
    };
    byte[] data = new byte[24];
    PlatformDependent.threadLocalRandom().nextBytes(data);
    // Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
    // to test the actual handshaker.
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
    WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(shaker.newHandshakeRequest());
    EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
    assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
    byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
    CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
    compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
    for (; ; ) {
        ByteBuf frameBytes = websocketChannel.readOutbound();
        if (frameBytes == null) {
            break;
        }
        compositeByteBuf.addComponent(true, frameBytes);
    }
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
            handshaker.finishHandshake(ctx.channel(), msg);
            ctx.pipeline().remove(this);
        }
    });
    if (codec) {
        ch.pipeline().addFirst(new HttpClientCodec());
    } else {
        ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
    }
    // We need to first write the request as HttpClientCodec will fail if we receive a response before a request
    // was written.
    shaker.handshake(ch).syncUninterruptibly();
    for (; ; ) {
        // Just consume the bytes, we are not interested in these.
        ByteBuf buf = ch.readOutbound();
        if (buf == null) {
            break;
        }
        buf.release();
    }
    assertTrue(ch.writeInbound(compositeByteBuf));
    assertTrue(ch.finish());
    BinaryWebSocketFrame frame = ch.readInbound();
    ByteBuf expect = Unpooled.wrappedBuffer(data);
    try {
        assertEquals(expect, frame.content());
        assertTrue(frame.isFinalFragment());
        assertEquals(0, frame.rsv());
    } finally {
        expect.release();
        frame.release();
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder)

Example 2 with HttpRequestEncoder

use of io.netty.handler.codec.http.HttpRequestEncoder in project netty by netty.

the class WebSocketClientHandshaker method handshake.

/**
     * Begins the opening handshake
     *
     * @param channel
     *            Channel
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is sent
     */
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
    FullHttpRequest request = newHandshakeRequest();
    HttpResponseDecoder decoder = channel.pipeline().get(HttpResponseDecoder.class);
    if (decoder == null) {
        HttpClientCodec codec = channel.pipeline().get(HttpClientCodec.class);
        if (codec == null) {
            promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpResponseDecoder or HttpClientCodec"));
            return promise;
        }
    }
    channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
                if (ctx == null) {
                    ctx = p.context(HttpClientCodec.class);
                }
                if (ctx == null) {
                    promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "a HttpRequestEncoder or HttpClientCodec"));
                    return;
                }
                p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 3 with HttpRequestEncoder

use of io.netty.handler.codec.http.HttpRequestEncoder in project netty by netty.

the class WebSocketClientHandshaker method finishHandshake.

/**
     * Validates and finishes the opening handshake initiated by {@link #handshake}}.
     *
     * @param channel
     *            Channel
     * @param response
     *            HTTP response containing the closing handshake details
     */
public final void finishHandshake(Channel channel, FullHttpResponse response) {
    verify(response);
    // Verify the subprotocol that we received from the server.
    // This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
    String receivedProtocol = response.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
    receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
    String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
    boolean protocolValid = false;
    if (expectedProtocol.isEmpty() && receivedProtocol == null) {
        // No subprotocol required and none received
        protocolValid = true;
        // null or "" - we echo what the user requested
        setActualSubprotocol(expectedSubprotocol);
    } else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
        // We require a subprotocol and received one -> verify it
        for (String protocol : expectedProtocol.split(",")) {
            if (protocol.trim().equals(receivedProtocol)) {
                protocolValid = true;
                setActualSubprotocol(receivedProtocol);
                break;
            }
        }
    }
    if (!protocolValid) {
        throw new WebSocketHandshakeException(String.format("Invalid subprotocol. Actual: %s. Expected one of: %s", receivedProtocol, expectedSubprotocol));
    }
    setHandshakeComplete();
    final ChannelPipeline p = channel.pipeline();
    // Remove decompressor from pipeline if its in use
    HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
    if (decompressor != null) {
        p.remove(decompressor);
    }
    // Remove aggregator if present before
    HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
    if (aggregator != null) {
        p.remove(aggregator);
    }
    ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
    if (ctx == null) {
        ctx = p.context(HttpClientCodec.class);
        if (ctx == null) {
            throw new IllegalStateException("ChannelPipeline does not contain " + "a HttpRequestEncoder or HttpClientCodec");
        }
        final HttpClientCodec codec = (HttpClientCodec) ctx.handler();
        // Remove the encoder part of the codec as the user may start writing frames after this method returns.
        codec.removeOutboundHandler();
        p.addAfter(ctx.name(), "ws-decoder", newWebsocketDecoder());
        // Delay the removal of the decoder so the user can setup the pipeline if needed to handle
        // WebSocketFrame messages.
        // See https://github.com/netty/netty/issues/4533
        channel.eventLoop().execute(new Runnable() {

            @Override
            public void run() {
                p.remove(codec);
            }
        });
    } else {
        if (p.get(HttpRequestEncoder.class) != null) {
            // Remove the encoder part of the codec as the user may start writing frames after this method returns.
            p.remove(HttpRequestEncoder.class);
        }
        final ChannelHandlerContext context = ctx;
        p.addAfter(context.name(), "ws-decoder", newWebsocketDecoder());
        // Delay the removal of the decoder so the user can setup the pipeline if needed to handle
        // WebSocketFrame messages.
        // See https://github.com/netty/netty/issues/4533
        channel.eventLoop().execute(new Runnable() {

            @Override
            public void run() {
                p.remove(context.handler());
            }
        });
    }
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 4 with HttpRequestEncoder

use of io.netty.handler.codec.http.HttpRequestEncoder in project hadoop by apache.

the class SimpleHttpProxyHandler method channelRead0.

@Override
public void channelRead0(final ChannelHandlerContext ctx, final HttpRequest req) {
    uri = req.getUri();
    final Channel client = ctx.channel();
    Bootstrap proxiedServer = new Bootstrap().group(client.eventLoop()).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpRequestEncoder(), new Forwarder(uri, client));
        }
    });
    ChannelFuture f = proxiedServer.connect(host);
    proxiedChannel = f.channel();
    f.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ctx.channel().pipeline().remove(HttpResponseEncoder.class);
                HttpRequest newReq = new DefaultFullHttpRequest(HTTP_1_1, req.getMethod(), req.getUri());
                newReq.headers().add(req.headers());
                newReq.headers().set(CONNECTION, Values.CLOSE);
                future.channel().writeAndFlush(newReq);
            } else {
                DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR);
                resp.headers().set(CONNECTION, Values.CLOSE);
                LOG.info("Proxy " + uri + " failed. Cause: ", future.cause());
                ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
                client.close();
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Bootstrap(io.netty.bootstrap.Bootstrap) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder)

Aggregations

HttpRequestEncoder (io.netty.handler.codec.http.HttpRequestEncoder)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 ChannelPipeline (io.netty.channel.ChannelPipeline)3 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)2 HttpResponseDecoder (io.netty.handler.codec.http.HttpResponseDecoder)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 Channel (io.netty.channel.Channel)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)1