Search in sources :

Example 11 with HttpClientCodec

use of 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());
    EventLoopGroup group = new NioEventLoopGroup();
    final CountDownLatch handshakeLatch = new CountDownLatch(1);
    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), new WSClientHandler(handshaker, handshakeLatch));
        }
    });
    // Connect
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
    future.syncUninterruptibly();
    ch = future.channel();
    handshaker.handshake(ch).syncUninterruptibly();
    handshakeLatch.await();
    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) CountDownLatch(java.util.concurrent.CountDownLatch) 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 12 with HttpClientCodec

use of io.netty.handler.codec.http.HttpClientCodec in project apn-proxy by apn-proxy.

the class ApnProxyRemoteForwardChannelInitializer method initChannel.

@Override
public void initChannel(SocketChannel channel) throws Exception {
    ApnProxyRemote apnProxyRemote = uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote();
    channel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).set(uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get());
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3, TimeUnit.MINUTES));
    pipeline.addLast("idlehandler", new ApnProxyIdleHandler());
    if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.SSL) {
        SSLEngine engine = ApnProxySSLContextFactory.createClientSSLEnginForRemoteAddress(apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort());
        engine.setUseClientMode(true);
        pipeline.addLast("ssl", new SslHandler(engine));
    } else if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.AES) {
        byte[] key = ((ApnProxyAESRemote) apnProxyRemote).getKey();
        byte[] iv = ((ApnProxyAESRemote) apnProxyRemote).getIv();
        pipeline.addLast("apnproxy.encrypt", new ApnProxyAESEncoder(key, iv));
        pipeline.addLast("apnproxy.decrypt", new ApnProxyAESDecoder(key, iv));
    }
    pipeline.addLast("codec", new HttpClientCodec());
    pipeline.addLast(ApnProxyRemoteForwardHandler.HANDLER_NAME, new ApnProxyRemoteForwardHandler(uaChannel, remoteChannelInactiveCallback));
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ApnProxyRemote(com.xx_dev.apn.proxy.remotechooser.ApnProxyRemote) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 13 with HttpClientCodec

use of io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.

the class Http2InitializerHandler method configureHttpPipeline.

/**
   * Sets up HTTP/2 over TCP through protocol upgrade (h2c) pipeline
   */
private void configureHttpPipeline(ChannelHandlerContext ctx, Request request) throws Exception {
    Http2StreamCodec http2Codec = new Http2StreamCodecBuilder().connection(_connection).maxContentLength(_maxResponseSize).maxHeaderSize(_maxHeaderSize).gracefulShutdownTimeoutMillis(_gracefulShutdownTimeout).streamingTimeout(_streamingTimeout).scheduler(_scheduler).build();
    HttpClientCodec sourceCodec = new HttpClientCodec(MAX_INITIAL_LINE_LENGTH, _maxHeaderSize, _maxChunkSize);
    Http2ClientUpgradeCodec targetCodec = new Http2ClientUpgradeCodec(http2Codec);
    HttpClientUpgradeHandler upgradeCodec = new HttpClientUpgradeHandler(sourceCodec, targetCodec, MAX_CLIENT_UPGRADE_CONTENT_LENGTH);
    Http2SchemeHandler schemeHandler = new Http2SchemeHandler(HttpScheme.HTTP.toString());
    String host = request.getURI().getAuthority();
    int port = request.getURI().getPort();
    String path = request.getURI().getPath();
    Http2UpgradeHandler upgradeHandler = new Http2UpgradeHandler(host, port, path);
    Http2StreamResponseHandler responseHandler = new Http2StreamResponseHandler();
    Http2ChannelPoolHandler channelPoolHandler = new Http2ChannelPoolHandler();
    ctx.pipeline().addBefore(ctx.name(), "sourceCodec", sourceCodec);
    ctx.pipeline().addBefore(ctx.name(), "upgradeCodec", upgradeCodec);
    ctx.pipeline().addBefore(ctx.name(), "upgradeHandler", upgradeHandler);
    ctx.pipeline().addBefore(ctx.name(), "schemeHandler", schemeHandler);
    ctx.pipeline().addBefore(ctx.name(), "responseHandler", responseHandler);
    ctx.pipeline().addBefore(ctx.name(), "channelHandler", channelPoolHandler);
    _setupComplete = true;
}
Also used : Http2ClientUpgradeCodec(io.netty.handler.codec.http2.Http2ClientUpgradeCodec) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) HttpClientUpgradeHandler(io.netty.handler.codec.http.HttpClientUpgradeHandler)

Example 14 with HttpClientCodec

use of io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.

the class RAPClientPipelineInitializer method initChannel.

@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
    ch.pipeline().addLast("codec", new HttpClientCodec(4096, _maxHeaderSize, _maxChunkSize));
    ch.pipeline().addLast("rapFullRequestEncoder", new RAPFullRequestEncoder());
    ch.pipeline().addLast("rapEncoder", new RAPRequestEncoder());
    ch.pipeline().addLast("rapDecoder", new RAPResponseDecoder(_maxResponseSize));
    ch.pipeline().addLast("responseHandler", new RAPStreamResponseHandler());
    if (_sslContext != null) {
        ch.pipeline().addLast("sslRequestHandler", new SslRequestHandler(_sslContext, _sslParameters));
    }
    ch.pipeline().addLast("channelManager", new ChannelPoolStreamHandler());
}
Also used : HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec)

Example 15 with HttpClientCodec

use of io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.

the class TestRAPClientCodec method testDecodeException.

@Test
public void testDecodeException() {
    final EmbeddedChannel ch = new EmbeddedChannel(new HttpClientCodec(), new HttpObjectAggregator(65536), new RAPClientCodec());
    // When we received an invalid message, a decode exception should be thrown out of the
    // end of netty pipeline.
    String junk = "Not a HTTP message\r\n";
    try {
        ch.writeInbound(Unpooled.copiedBuffer(junk, CHARSET));
        Assert.fail("Should have thrown decode exception");
    } catch (Exception ex) {
    // expected.
    }
    ch.finish();
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteString(com.linkedin.data.ByteString) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) Test(org.testng.annotations.Test)

Aggregations

HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)22 ChannelPipeline (io.netty.channel.ChannelPipeline)16 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)10 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)8 Bootstrap (io.netty.bootstrap.Bootstrap)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 SocketChannel (io.netty.channel.socket.SocketChannel)7 Channel (io.netty.channel.Channel)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)6 EventLoopGroup (io.netty.channel.EventLoopGroup)5 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)5 HttpRequestEncoder (io.netty.handler.codec.http.HttpRequestEncoder)4 IOException (java.io.IOException)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ExecutionException (java.util.concurrent.ExecutionException)4 Pair (com.nike.internal.util.Pair)3 ChannelFuture (io.netty.channel.ChannelFuture)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 SslHandler (io.netty.handler.ssl.SslHandler)3