io.netty.handler.codec.http.HttpServerUpgradeHandler Java Examples

The following examples show how to use io.netty.handler.codec.http.HttpServerUpgradeHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Http2FrameCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void upgradeEventNoRefCntError() throws Exception {
    frameListener.onHeadersRead(http2HandlerCtx, Http2CodecUtil.HTTP_UPGRADE_STREAM_ID, request, 31, false);

    // Using reflect as the constructor is package-private and the class is final.
    Constructor<UpgradeEvent> constructor =
            UpgradeEvent.class.getDeclaredConstructor(CharSequence.class, FullHttpRequest.class);

    // Check if we could make it accessible which may fail on java9.
    Assume.assumeTrue(ReflectionUtil.trySetAccessible(constructor, true) == null);

    HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = constructor.newInstance(
            "HTTP/2", new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
    channel.pipeline().fireUserEventTriggered(upgradeEvent);
    assertEquals(1, upgradeEvent.refCnt());
}
 
Example #2
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via HTTP/2
 * on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) {
        HttpServerUpgradeHandler.UpgradeEvent upgradeEvent =
                (HttpServerUpgradeHandler.UpgradeEvent) evt;
        onHeadersRead(ctx, 1, http1HeadersToHttp2Headers(upgradeEvent.upgradeRequest()), 0 , true);
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #3
Source File: CleartextHttp2ServerUpgradeHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void setUpServerChannel() {
    frameListener = mock(Http2FrameListener.class);

    http2ConnectionHandler = new Http2ConnectionHandlerBuilder()
            .frameListener(frameListener).build();

    UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {
        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(http2ConnectionHandler);
        }
    };

    userEvents = new ArrayList<Object>();

    HttpServerCodec httpServerCodec = new HttpServerCodec();
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, upgradeCodecFactory);

    CleartextHttp2ServerUpgradeHandler handler = new CleartextHttp2ServerUpgradeHandler(
            httpServerCodec, upgradeHandler, http2ConnectionHandler);
    channel = new EmbeddedChannel(handler, new ChannelInboundHandlerAdapter() {
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            userEvents.add(evt);
        }
    });
}
 
Example #4
Source File: CleartextHttp2ServerUpgradeHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void usedHttp2MultiplexCodec() throws Exception {
    final Http2MultiplexCodec http2Codec = new Http2MultiplexCodecBuilder(true, new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
        }
    }).build();
    UpgradeCodecFactory upgradeCodecFactory = new UpgradeCodecFactory() {
        @Override
        public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
            return new Http2ServerUpgradeCodec(http2Codec);
        }
    };
    http2ConnectionHandler = http2Codec;

    userEvents = new ArrayList<Object>();

    HttpServerCodec httpServerCodec = new HttpServerCodec();
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, upgradeCodecFactory);

    CleartextHttp2ServerUpgradeHandler handler = new CleartextHttp2ServerUpgradeHandler(
            httpServerCodec, upgradeHandler, http2Codec);
    channel = new EmbeddedChannel(handler, new ChannelInboundHandlerAdapter() {
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            userEvents.add(evt);
        }
    });

    assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()));

    ByteBuf settingsFrame = settingsFrameBuf();

    assertTrue(channel.writeInbound(settingsFrame));

    assertEquals(1, userEvents.size());
    assertTrue(userEvents.get(0) instanceof PriorKnowledgeUpgradeEvent);
}
 
Example #5
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via HTTP/2
 * on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) {
        HttpServerUpgradeHandler.UpgradeEvent upgradeEvent =
                (HttpServerUpgradeHandler.UpgradeEvent) evt;
        onHeadersRead(ctx, 1, http1HeadersToHttp2Headers(upgradeEvent.upgradeRequest()), 0 , true);
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #6
Source File: Http2Handler.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via HTTP/2
 * on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).
 *
 * @param ctx - Context of the ChannelHandler
 * @param evt - HTTP upgrade event
 * @throws java.lang.Exception If an error occurs while upgrading the event
 */
@Override
public void userEventTriggered
(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) {
        HttpServerUpgradeHandler.UpgradeEvent upgradeEvent =
                (HttpServerUpgradeHandler.UpgradeEvent) evt;

        onHeadersRead(ctx, 1, http1HeadersToHttp2Headers(upgradeEvent.upgradeRequest()), 0, true);
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #7
Source File: Http2ServerChannelHandler.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    /*
     * Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via HTTP/2
     * on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).
     */
    if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) {
        HttpServerUpgradeHandler.UpgradeEvent upgradeEvent =
                (HttpServerUpgradeHandler.UpgradeEvent) evt;
        this.isUpgradeH2cMode = true;
        onHeadersRead(ctx, 1, http1HeadersToHttp2Headers(upgradeEvent.upgradeRequest()), 0, true);
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #8
Source File: HttpServerConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
	if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
		return new Http2ServerUpgradeCodec(http2FrameCodec, new Http2MultiplexHandler(this));
	}
	else {
		return null;
	}
}
 
Example #9
Source File: CleartextHttp2ServerUpgradeHandler.java    From glowroot with Apache License 2.0 5 votes vote down vote up
public CleartextHttp2ServerUpgradeHandler(HttpServerCodec httpServerCodec,
        HttpServerUpgradeHandler httpServerUpgradeHandler,
        ChannelHandler http2ServerHandler) {
    this.httpServerCodec = checkNotNull(httpServerCodec, "httpServerCodec");
    this.httpServerUpgradeHandler =
            checkNotNull(httpServerUpgradeHandler, "httpServerUpgradeHandler");
    this.http2ServerHandler = checkNotNull(http2ServerHandler, "http2ServerHandler");
}
 
Example #10
Source File: Http2ServerInitializer.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.
 */
private static void configureClearText(SocketChannel ch) {
    HttpServerCodec sourceCodec = new HttpServerCodec();
    HelloWorldHttp2Handler http2Handler = new HelloWorldHttp2Handler();
    HttpServerUpgradeHandler.UpgradeCodec upgradeCodec = new Http2ServerUpgradeCodec(http2Handler);
    HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, Collections.singletonList(upgradeCodec), 65536);

    ch.pipeline().addLast(sourceCodec);
    ch.pipeline().addLast(upgradeHandler);
    ch.pipeline().addLast(new UserEventLogger());
}
 
Example #11
Source File: HttpServerConfig.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
static void configureHttp11OrH2CleartextPipeline(ChannelPipeline p,
		@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compressPredicate,
		ServerCookieDecoder cookieDecoder,
		ServerCookieEncoder cookieEncoder,
		HttpRequestDecoderSpec decoder,
		boolean forwarded,
		Http2Settings http2Settings,
		ConnectionObserver listener,
		@Nullable Supplier<? extends ChannelMetricsRecorder> metricsRecorder,
		int minCompressionSize,
		ChannelOperations.OnSetup opsFactory,
		@Nullable Function<String, String> uriTagValue) {
	HttpServerCodec httpServerCodec =
			new HttpServerCodec(decoder.maxInitialLineLength(), decoder.maxHeaderSize(),
					decoder.maxChunkSize(), decoder.validateHeaders(), decoder.initialBufferSize());

	Http11OrH2CleartextCodec
			upgrader = new Http11OrH2CleartextCodec(cookieDecoder, cookieEncoder, p.get(NettyPipeline.LoggingHandler) != null,
					forwarded, http2Settings, listener, opsFactory, decoder.validateHeaders());

	ChannelHandler http2ServerHandler = new H2CleartextCodec(upgrader);
	CleartextHttp2ServerUpgradeHandler h2cUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(
			httpServerCodec,
			new HttpServerUpgradeHandler(httpServerCodec, upgrader, decoder.h2cMaxContentLength()),
			http2ServerHandler);

	p.addBefore(NettyPipeline.ReactiveBridge,
	            NettyPipeline.H2CUpgradeHandler, h2cUpgradeHandler)
	 .addBefore(NettyPipeline.ReactiveBridge,
	            NettyPipeline.HttpTrafficHandler,
	            new HttpTrafficHandler(listener, forwarded, compressPredicate, cookieEncoder, cookieDecoder));

	if (ACCESS_LOG) {
		p.addAfter(NettyPipeline.H2CUpgradeHandler, NettyPipeline.AccessLogHandler, new AccessLogHandler());
	}

	boolean alwaysCompress = compressPredicate == null && minCompressionSize == 0;

	if (alwaysCompress) {
		p.addBefore(NettyPipeline.HttpTrafficHandler, NettyPipeline.CompressionHandler, new SimpleCompressionHandler());
	}

	if (metricsRecorder != null) {
		ChannelMetricsRecorder channelMetricsRecorder = metricsRecorder.get();
		if (channelMetricsRecorder instanceof HttpServerMetricsRecorder) {
			p.addAfter(NettyPipeline.HttpTrafficHandler, NettyPipeline.HttpMetricsHandler,
			           new HttpServerMetricsHandler((HttpServerMetricsRecorder) channelMetricsRecorder, uriTagValue));
			if (channelMetricsRecorder instanceof MicrometerHttpServerMetricsRecorder) {
				// MicrometerHttpServerMetricsRecorder does not implement metrics on protocol level
				// ChannelMetricsHandler will be removed from the pipeline
				p.remove(NettyPipeline.ChannelMetricsHandler);
			}
		}
	}
}
 
Example #12
Source File: CleartextHttp2ServerUpgradeHandler.java    From netty-4.1.22 with Apache License 2.0 3 votes vote down vote up
/**
 * Creates the channel handler provide cleartext HTTP/2 upgrade from HTTP
 * upgrade or prior knowledge
 *
 * @param httpServerCodec the http server codec
 * @param httpServerUpgradeHandler the http server upgrade handler for HTTP/2
 * @param http2ServerHandler the http2 server handler, will be added into pipeline
 *                           when starting HTTP/2 by prior knowledge
 */
public CleartextHttp2ServerUpgradeHandler(HttpServerCodec httpServerCodec,
                                          HttpServerUpgradeHandler httpServerUpgradeHandler,
                                          ChannelHandler http2ServerHandler) {
    this.httpServerCodec = checkNotNull(httpServerCodec, "httpServerCodec");
    this.httpServerUpgradeHandler = checkNotNull(httpServerUpgradeHandler, "httpServerUpgradeHandler");
    this.http2ServerHandler = checkNotNull(http2ServerHandler, "http2ServerHandler");
}
 
Example #13
Source File: Http2ServerUpgradeHandler.java    From sofa-rpc with Apache License 2.0 3 votes vote down vote up
/**
 * Creates the channel handler provide cleartext HTTP/2 upgrade from HTTP
 * upgrade or prior knowledge
 *
 * @param bizGroup                 the EventLoopGroup
 * @param httpServerCodec          the http server codec
 * @param httpServerUpgradeHandler the http server upgrade handler for HTTP/2
 * @param http2ServerHandler       the http2 server handler, will be added into pipeline
 *                                 when starting HTTP/2 by prior knowledge
 */
public Http2ServerUpgradeHandler(EventLoopGroup bizGroup,
                                 HttpServerCodec httpServerCodec,
                                 HttpServerUpgradeHandler httpServerUpgradeHandler,
                                 ChannelHandler http2ServerHandler) {
    this.bizGroup = checkNotNull(bizGroup, "bizGroup");
    this.httpServerCodec = checkNotNull(httpServerCodec, "httpServerCodec");
    this.httpServerUpgradeHandler = checkNotNull(httpServerUpgradeHandler, "httpServerUpgradeHandler");
    this.http2ServerHandler = checkNotNull(http2ServerHandler, "http2ServerHandler");
}