List of usage examples for io.netty.handler.codec.http HttpServerUpgradeHandler HttpServerUpgradeHandler
public HttpServerUpgradeHandler(SourceCodec sourceCodec, UpgradeCodecFactory upgradeCodecFactory)
From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.server.Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 *//*from w ww . j av a2 s. com*/ private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); p.addLast(sourceCodec); p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); ChannelHandlerContext thisCtx = pipeline.context(this); pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(ReferenceCountUtil.retain(msg)); } }); p.addLast(new UserEventLogger()); }
From source file:com.hop.hhxx.example.http2.helloworld.multiplex.server.Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 *///from ww w.ja va 2 s .c o m private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); p.addLast(sourceCodec); p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); ChannelHandlerContext thisCtx = pipeline.context(this); pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(msg); } }); p.addLast(new UserEventLogger()); }
From source file:com.vmware.xenon.common.http.netty.NettyHttpServerInitializer.java
License:Open Source License
/** * initChannel is called by Netty when a channel is first used. */// w w w.j a v a 2 s. c o m @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); ch.config().setAllocator(NettyChannelContext.ALLOCATOR); ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE); ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE); SslHandler sslHandler = null; if (this.sslContext != null) { sslHandler = this.sslContext.newHandler(ch.alloc()); SslClientAuthMode mode = this.host.getState().sslClientAuthMode; if (mode != null) { switch (mode) { case NEED: sslHandler.engine().setNeedClientAuth(true); break; case WANT: sslHandler.engine().setWantClientAuth(true); break; default: break; } } p.addLast(SSL_HANDLER, sslHandler); } // The HttpServerCodec combines the HttpRequestDecoder and the HttpResponseEncoder, and it // also provides a method for upgrading the protocol, which we use to support HTTP/2. It // also supports a couple other minor features (support for HEAD and CONNECT), which // probably don't matter to us. HttpServerCodec http1_codec = new HttpServerCodec(NettyChannelContext.MAX_INITIAL_LINE_LENGTH, NettyChannelContext.MAX_HEADER_SIZE, NettyChannelContext.MAX_CHUNK_SIZE, false); p.addLast(HTTP1_CODEC, http1_codec); if (this.sslContext == null) { // Today we only use HTTP/2 when SSL is disabled final HttpToHttp2ConnectionHandler connectionHandler = makeHttp2ConnectionHandler(); UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() { @Override public UpgradeCodec newUpgradeCodec(CharSequence protocol) { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(connectionHandler); } else { return null; } } }; // On upgrade, the upgradeHandler will remove the http1_codec and replace it // with the connectionHandler. Ideally we'd remove the aggregator (chunked transfer // isn't allowed in HTTP/2) and the WebSocket handler (we don't support it over HTTP/2 yet) // but we're not doing that yet. HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(http1_codec, upgradeCodecFactory); p.addLast(HTTP2_UPGRADE_HANDLER, upgradeHandler); } p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(this.responsePayloadSizeLimit)); p.addLast(WEBSOCKET_HANDLER, new NettyWebSocketRequestHandler(this.host, ServiceUriPaths.CORE_WEB_SOCKET_ENDPOINT, ServiceUriPaths.WEB_SOCKET_SERVICE_PREFIX)); p.addLast(HTTP_REQUEST_HANDLER, new NettyHttpClientRequestHandler(this.host, sslHandler, this.responsePayloadSizeLimit)); }
From source file:example.http2.helloworld.server.Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 *///from w w w . java 2s.c om private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory); final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler( sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build()); p.addLast(cleartextHttp2ServerUpgradeHandler); p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); ChannelHandlerContext thisCtx = pipeline.context(this); pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(ReferenceCountUtil.retain(msg)); } }); p.addLast(new UserEventLogger()); }
From source file:http2.server.Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 *///from w w w .j a v a 2 s . c o m private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); p.addLast(sourceCodec); p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); // ? p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); ChannelHandlerContext thisCtx = pipeline.context(this); pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(ReferenceCountUtil.retain(msg)); } }); p.addLast(new UserEventLogger()); }
From source file:io.bsoa.rpc.grpc.server12.Http2ServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); p.addLast(sourceCodec);/*from ww w .ja v a 2 s. co m*/ p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); // ? p.addLast(new Http1InboundHandler(maxHttpContentLength)); // HTTP/1.1 p.addLast(new UserEventLogger()); }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void plaintextUpgradeNegotiator() throws Exception { LocalAddress addr = new LocalAddress("plaintextUpgradeNegotiator"); UpgradeCodecFactory ucf = new UpgradeCodecFactory() { @Override/*from w w w . jav a 2 s.c o m*/ public UpgradeCodec newUpgradeCodec(CharSequence protocol) { return new Http2ServerUpgradeCodec(FakeGrpcHttp2ConnectionHandler.newHandler()); } }; final HttpServerCodec serverCodec = new HttpServerCodec(); final HttpServerUpgradeHandler serverUpgradeHandler = new HttpServerUpgradeHandler(serverCodec, ucf); Channel serverChannel = new ServerBootstrap().group(group).channel(LocalServerChannel.class) .childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(serverCodec, serverUpgradeHandler); } }).bind(addr).sync().channel(); FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler(); ProtocolNegotiator nego = ProtocolNegotiators.plaintextUpgrade(); ChannelHandler ch = nego.newHandler(gh); WriteBufferingAndExceptionHandler wbaeh = new WriteBufferingAndExceptionHandler(ch); Channel channel = new Bootstrap().group(group).channel(LocalChannel.class).handler(wbaeh).register().sync() .channel(); ChannelFuture write = channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE); channel.connect(serverChannel.localAddress()); boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS); if (!completed) { assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)); // sync should fail if we are in this block. write.sync(); throw new AssertionError("neither wrote nor negotiated"); } channel.close().sync(); serverChannel.close(); assertThat(gh.securityInfo).isNull(); assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.NONE); assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr); }
From source file:io.netty.example.http2.helloworld.frame.server.Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 *///from w ww . ja va 2 s.com private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); p.addLast(sourceCodec); p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(ReferenceCountUtil.retain(msg)); } }); p.addLast(new UserEventLogger()); }
From source file:io.netty.example.http2.helloworld.server.Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0 *//*w ww.j ava 2s .c o m*/ private void configureClearText(SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); final HttpServerCodec sourceCodec = new HttpServerCodec(); final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory); final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler( sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build()); p.addLast(cleartextHttp2ServerUpgradeHandler); p.addLast(new SimpleChannelInboundHandler<HttpMessage>() { @Override protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP. System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)"); ChannelPipeline pipeline = ctx.pipeline(); pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted.")); pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength)); ctx.fireChannelRead(ReferenceCountUtil.retain(msg)); } }); p.addLast(new UserEventLogger()); }
From source file:io.vertx.core.http.Http2ClientTest.java
License:Open Source License
private ServerBootstrap createH2CServer( BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler, boolean upgrade) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(NioServerSocketChannel.class); bootstrap.group(new NioEventLoopGroup()); bootstrap.childHandler(new ChannelInitializer<Channel>() { @Override/*from w w w.java 2s .co m*/ protected void initChannel(Channel ch) throws Exception { if (upgrade) { HttpServerCodec sourceCodec = new HttpServerCodec(); HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { Http2ConnectionHandler httpConnectionHandler = createHttpConnectionHandler((a, b) -> { return new Http2FrameListenerDecorator(handler.apply(a, b)) { @Override public void onSettingsRead(ChannelHandlerContext ctx, io.netty.handler.codec.http2.Http2Settings settings) throws Http2Exception { super.onSettingsRead(ctx, settings); Http2Connection conn = a.connection(); Http2Stream stream = conn.stream(1); DefaultHttp2Headers blah = new DefaultHttp2Headers(); blah.status("200"); b.frameWriter().writeHeaders(ctx, 1, blah, 0, true, ctx.voidPromise()); } }; }); return new Http2ServerUpgradeCodec(httpConnectionHandler); } else { return null; } }; ch.pipeline().addLast(sourceCodec); ch.pipeline().addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); } else { Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler); ch.pipeline().addLast("handler", clientHandler); } } }); return bootstrap; }