io.netty.handler.codec.http.HttpServerUpgradeHandler # UpgradeEvent ( ) 源码实例Demo

下面列出了 io.netty.handler.codec.http.HttpServerUpgradeHandler # UpgradeEvent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


@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());
}
 

/**
 * 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);
}
 

/**
 * 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);
}
 

/**
 * 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);
}
 

@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);
}