Search in sources :

Example 21 with HttpClientCodec

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

the class WebSocketServerProtocolHandlerTest method createClient.

private EmbeddedChannel createClient(ChannelHandler... handlers) throws Exception {
    WebSocketClientProtocolConfig clientConfig = WebSocketClientProtocolConfig.newBuilder().webSocketUri("http://test/test").dropPongFrames(false).handleCloseFrames(false).build();
    EmbeddedChannel ch = new EmbeddedChannel(false, false, new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(clientConfig));
    ch.pipeline().addLast(handlers);
    ch.register();
    return ch;
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec)

Example 22 with HttpClientCodec

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

the class WebSocketClient method main.

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    final int port;
    if (uri.getPort() == -1) {
        if ("ws".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("wss".equalsIgnoreCase(scheme)) {
            port = 443;
        } else {
            port = -1;
        }
    } else {
        port = uri.getPort();
    }
    if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
        System.err.println("Only WS(S) is supported.");
        return;
    }
    final boolean ssl = "wss".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
        // If you change it to V00, ping is not supported and remember to change
        // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
        final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                }
                p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, handler);
            }
        });
        Channel ch = b.connect(uri.getHost(), port).sync().channel();
        handler.handshakeFuture().sync();
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String msg = console.readLine();
            if (msg == null) {
                break;
            } else if ("bye".equals(msg.toLowerCase())) {
                ch.writeAndFlush(new CloseWebSocketFrame());
                ch.closeFuture().sync();
                break;
            } else if ("ping".equals(msg.toLowerCase())) {
                WebSocketFrame frame = new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
                ch.writeAndFlush(frame);
            } else {
                WebSocketFrame frame = new TextWebSocketFrame(msg);
                ch.writeAndFlush(frame);
            }
        }
    } finally {
        group.shutdownGracefully();
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InputStreamReader(java.io.InputStreamReader) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) URI(java.net.URI) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) BufferedReader(java.io.BufferedReader) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) Bootstrap(io.netty.bootstrap.Bootstrap) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 23 with HttpClientCodec

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

the class WebSocketTestClient method connect.

/**
 * Connect the WebSocket client
 *
 * @throws Exception
 */
public WebSocketTestClient connect() throws Exception {
    String protocol = uri.getScheme();
    if (!"ws".equals(protocol)) {
        throw new IllegalArgumentException("Unsupported protocol: " + protocol);
    }
    final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, version, null, false, new DefaultHttpHeaders());
    WSClientHandler handler = new WSClientHandler(handshaker);
    EventLoopGroup group = new NioEventLoopGroup();
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            ChannelPipeline p = channel.pipeline();
            p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler);
        }
    });
    // Connect
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
    future.syncUninterruptibly();
    handler.handshakeFuture.syncUninterruptibly();
    ch = future.channel();
    return this;
}
Also used : WebSocketClientHandshaker(io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker) ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ExecutionException(java.util.concurrent.ExecutionException) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 24 with HttpClientCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.

the class HttpChannelInitializer method initChannel.

@Override
protected void initChannel(NioSocketChannel channel) {
    if (_ssl) {
        channel.pipeline().addLast(SessionResumptionSslHandler.PIPELINE_SESSION_RESUMPTION_HANDLER, new SessionResumptionSslHandler(_sslContext, _sslParameters, _enableSSLSessionResumption, _sslHandShakeTimeout));
    }
    channel.pipeline().addLast("codec", new HttpClientCodec(_maxInitialLineLength, _maxHeaderSize, _maxChunkSize));
    channel.pipeline().addLast("outboundRestRequestEncoder", HttpMessageEncoders.newRestRequestEncoder());
    channel.pipeline().addLast("outboundStreamDataEncoder", HttpMessageEncoders.newDataEncoder());
    channel.pipeline().addLast("outboundStreamRequestEncoder", HttpMessageEncoders.newStreamRequestEncoder());
    channel.pipeline().addLast("inboundDataDecoder", HttpMessageDecoders.newDataDecoder());
    channel.pipeline().addLast("inboundRequestDecoder", HttpMessageDecoders.newResponseDecoder());
    channel.pipeline().addLast("schemeHandler", new SchemeHandler(_ssl ? HttpScheme.HTTPS.toString() : HttpScheme.HTTP.toString()));
    channel.pipeline().addLast("streamDuplexHandler", new ClientEntityStreamHandler(_maxContentLength));
    channel.pipeline().addLast("timeoutHandler", new CancelTimeoutHandler());
    channel.pipeline().addLast("channelPoolHandler", new ChannelLifecycleHandler(RECYCLE_CHANNEL));
}
Also used : ChannelLifecycleHandler(com.linkedin.r2.netty.handler.common.ChannelLifecycleHandler) SessionResumptionSslHandler(com.linkedin.r2.netty.handler.common.SessionResumptionSslHandler) ClientEntityStreamHandler(com.linkedin.r2.netty.handler.common.ClientEntityStreamHandler) CancelTimeoutHandler(com.linkedin.r2.netty.handler.common.CancelTimeoutHandler) SchemeHandler(com.linkedin.r2.netty.handler.common.SchemeHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec)

Example 25 with HttpClientCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.

the class RAPStreamClientPipelineInitializer method initChannel.

@Override
protected void initChannel(NioSocketChannel ch) {
    if (_sslContext != null) {
        ch.pipeline().addLast(SessionResumptionSslHandler.PIPELINE_SESSION_RESUMPTION_HANDLER, new SessionResumptionSslHandler(_sslContext, _sslParameters, _enableSSLSessionResumption, _sslHandShakeTimeout));
    }
    ch.pipeline().addLast("codec", new HttpClientCodec(4096, _maxHeaderSize, _maxChunkSize));
    ch.pipeline().addLast("rapFullRequestEncoder", new RAPStreamFullRequestEncoder());
    ch.pipeline().addLast("rapEncoder", new RAPStreamRequestEncoder());
    ch.pipeline().addLast("rapDecoder", new RAPStreamResponseDecoder(_maxResponseSize));
    // the response handler catches the exceptions thrown by other layers. By consequence no handlers that throw exceptions
    // should be after this one, otherwise the exception won't be caught and managed by R2
    ch.pipeline().addLast("responseHandler", new RAPStreamResponseHandler());
    ch.pipeline().addLast("channelManager", new ChannelPoolStreamHandler());
}
Also used : SessionResumptionSslHandler(com.linkedin.r2.netty.handler.common.SessionResumptionSslHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec)

Aggregations

HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)61 ChannelPipeline (io.netty.channel.ChannelPipeline)30 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)26 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)25 Bootstrap (io.netty.bootstrap.Bootstrap)19 Channel (io.netty.channel.Channel)19 SocketChannel (io.netty.channel.socket.SocketChannel)17 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)16 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)15 IOException (java.io.IOException)13 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 SslContext (io.netty.handler.ssl.SslContext)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 URISyntaxException (java.net.URISyntaxException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 ChannelInitializer (io.netty.channel.ChannelInitializer)9 URI (java.net.URI)9 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)8 InetSocketAddress (java.net.InetSocketAddress)8