Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketServerHandshaker::handshake returns a Future that is never completed if HTTP request is not a FullHttpRequest #12655

Open
hoat4 opened this issue Jul 28, 2022 · 4 comments

Comments

@hoat4
Copy link

hoat4 commented Jul 28, 2022

Expected behavior

The FutureListener should be invoked when the WS handshake is done.

Actual behavior

The handshake completes successfully, but the FutureListener associated with handshaking is not invoked (but the next ChannelHandlerAdapter receives the WS message correctly).

Steps to reproduce

Run the code below. If you connect to it, the "Handshake done" string won't print, but the received messages will be printed.

Minimal yet complete reproducer code (or URL to code)

public static void main(String[] args) {
    ServerBootstrap b = new ServerBootstrap().
            group(new MultithreadEventLoopGroup(NioHandler.newFactory()), new MultithreadEventLoopGroup(NioHandler.newFactory())).
            channel(NioServerSocketChannel.class).
            childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("httpServerCodec", new HttpServerCodec());
                    pipeline.addLast("httpHandler", new HttpServerHandler());
                }
            });

    b.bind(5004);
}

private static class HttpServerHandler extends ChannelHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        HttpObject httpObject = (HttpObject) msg;

        if (httpObject instanceof HttpRequest) {
            HttpRequest req = (HttpRequest) msg;

            ctx.pipeline().replace(this, "websocketHandler", new ChannelHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    System.out.println("Received: "+msg);
                }
            });
            String wsURL = "ws://" + req.headers().get(HttpHeaderNames.HOST) + req.uri();
            WebSocketServerHandshakerFactory wsFactory =
                    new WebSocketServerHandshakerFactory(wsURL, null, true);
            WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
            if (handshaker == null) {
                WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
            } else {
                handshaker.handshake(ctx.channel(), req).addListener((Future<?> future) -> {
                    System.out.println("Handshake done");
                });
            }
        }
    }
}

Netty version

5.0.0.Alpha4

JVM version (e.g. java -version)

17

OS version (e.g. uname -a)

Windows 11

@hoat4
Copy link
Author

hoat4 commented Jul 28, 2022

A workaround is to put a HttpObjectAggregator to the channel's initial pipeline. If it is added in the channel initializer, then the handshaking future will be completed after the handshake is done.

@hoat4 hoat4 changed the title WebSocketServerHandshaker::handshake returns a Promise that is never completed if HTTP request is not a FullHttpRequest WebSocketServerHandshaker::handshake returns a Future that is never completed if HTTP request is not a FullHttpRequest Jul 28, 2022
@amizurov
Copy link
Sponsor Contributor

@hoat4 Hi, i'll take a look.

chrisvest pushed a commit that referenced this issue Aug 1, 2022
…12657)

Motivation:

Remove the `httpObjectAggregator` from the websocket handshake process.

Modification:

Port from #12192

Result:

Fixes #12655
Fix TODO: Make handshake work without HttpObjectAggregator at all
@normanmaurer
Copy link
Member

@amizurov what is the status of this one ?

@amizurov
Copy link
Sponsor Contributor

@amizurov what is the status of this one ?

I think this one we can close, but we have similar issue for client handshaker i'll prepare PR. @hoat4 any concerns ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants