小写的 netty websocket 标头名称的问题 [英] Issue with netty websocket headers name in lowercase

查看:138
本文介绍了小写的 netty websocket 标头名称的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 netty 来实现 websocket 服务器.服务器需要使用一些旧的 websocket 客户端库(用其他语言编写),这些库不遵守 HTTP 标头不区分大小写规则.我在那里遇到了问题.

I am using netty to implement a websocket server. The server needs to work with some old websocket clients libraries (written in other languages) out there which does not honour the HTTP header case insensitivity rule. I am facing a problem there.

我的入站渠道管道非常标准,如下所示HttpServerCodec -> HttpObjectAggregator -> HttpRequestBuilder ->WebSocketServerProtocolHandler -> WebSocketFrameAggregator -> CustomHandler

My inbound channel pipeline is pretty standard which looks like following HttpServerCodec -> HttpObjectAggregator -> HttpRequestBuilder ->WebSocketServerProtocolHandler -> WebSocketFrameAggregator -> CustomHandler

在 netty 版本 4.1.23.Final 中,我看到发送到客户端的 websocket 握手响应都是小写的标头名称,如下所示.

In netty version 4.1.23.Final I see that the websocket handshake response being sent to client is all in lowercase header names, like following.

upgrade: websocket\r\n
connection: upgrade\r\n
sec-websocket-accept: hex-values=\r\n

但较旧的 websocket 客户端需要以下格式的标头.

But the older websocket client expects the header in the following format.

Upgrade: websocket\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Accept: hex-values=\r\n

因此 websocket 连接没有完成.有人遇到过这个问题吗?

As a result the websocket connection does not complete. Has anybody faced this problem?

标头字符串来自 HttpHeaderNames 类而不是 HttpHeader 类.修复方法是更改​​ WebSocketServerHandshaker13.newHandshakeResponse() 以使用所需的 HttpHeader.但这可能需要更改 netty 代码本身.有人可以建议任何更简洁的方法来解决此问题而不更改 netty 代码吗?

The header strings are coming from HttpHeaderNames class instead of HttpHeader class. The fix is to change WebSocketServerHandshaker13.newHandshakeResponse() to use the required HttpHeader. But that would need change in netty code itself probably . Can somebody suggest any cleaner way to get around this problem without changing netty code?

  1. 在使用现有的 netty websocket 握手类时,是否需要添加/修改 http 响应头?
  2. 我是否应该编写自定义 WebSocketServerProtocolHandler 来实现相同的目的?
  3. 还有其他方法吗?

感谢任何回应!!!

非常感谢.

推荐答案

您可以使用 Netty ChannelOutboundHandlerAdapter 来更改管道中某处的标头.在从管道远程处理该处理程序之后,它不会干扰 WebSocket 帧.请在下面找到代码.

You can use Netty ChannelOutboundHandlerAdapter to change the header somewhere in the pipeline. After that remote that handler from the pipeline so it will not interfere with WebSocket frames. Please find the code below.

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpResponse;

public class OutboundHeadersChanger extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof HttpResponse) {
            ((HttpResponse) msg).headers().set("some-header", "some-header-value");
        }

        super.write(ctx, msg, promise);
    }
}

这篇关于小写的 netty websocket 标头名称的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆