java - Netty TooLongFrameException : How is the length computed?

标签 java netty vert.x

我有一个测试服务器和一个客户端应用程序。服务器使用 Vert.x 3.2.1(用户 Netty 4.0.33.Final)构建,客户端使用 Apache httpclient 4.5.1 构建。

已将服务器配置为接受 maxHeaderSize 为 8KB。当我从客户端发送请求时,我发现带有大约 3KB header 大小的请求开始失败,服务器上出现以下异常。

io.netty.handler.codec.TooLongFrameException: HTTP header is larger than 8192 bytes

分享服务器和客户端代码以供引用。

服务器:

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        HttpServerOptions options = new HttpServerOptions();
        options.setMaxHeaderSize(1024*8);
        HttpServer httpServer = (HttpServer) vertx.createHttpServer(options );
        httpServer.requestHandler(req -> {
            System.out.println(req.headers().names());
            for(String h : req.headers().names()){
                System.out.println(h+": "+req.headers().get(h).length()+": "+(h.length()+req.headers().get(h).length()));
            }
            req.response().setStatusCode(200).end();
        });
        httpServer.listen(6565);

    }

客户:

public static void main(String[] args) {

    try {
        CloseableHttpClient httpclient = HttpClients.custom().build();
        HttpGet get = new HttpGet("http://localhost:6565");
        String headerValue = generateHeader(3045);
        get.addHeader("myheader", headerValue);
        HttpResponse response = httpclient.execute(get);
        String resp = IOUtils.toString(response.getEntity().getContent());
        System.out.println(response.getStatusLine());
        httpclient.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

generateHeader() 是一个生成指定长度字符串的方法 - 当输入 3 时,结果为“aaa”。

Apache http 客户端还添加了更多 header - Host、Connection、User-Agent 和 Accept-Encoding。除了所有这些,我注意到的行为是,当我将 3045 或以上作为输入传递给 generateHeader() 时,服务器会失败,但对于任何较低的值都会成功。

虽然配置的是8k,但我不明白为什么这么低的值会出现故障。

最佳答案

这可能是 Netty 中的一个错误。请在 Netty 问题跟踪器中打开错误报告并提供如何重现它的详细信息:

https://github.com/netty/netty/issues

关于java - Netty TooLongFrameException : How is the length computed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502166/

相关文章:

java - 转换结果 HttpPost android 时出错

netty - 在 Netty 4 中,ctx.close 和 ctx.channel.close 有什么区别?

java - 在 List<Integer> 上使用 set 方法会导致不支持的操作异常

java - 如何对文本文件中的并行数组进行排序

java - ConcurrentIdentityWeakKeyHashMap 和整数键

java - 使用 vertx-config 异步加载配置文件

netty - 如何在 vert.x 套接字服务器中实现连接超时?

rx-java2 - 即使提供了 doOnerror,RxJava2 Single 也不处理错误

java - 尽管添加了 android :usesCleartextTraffic and android:networkSecurityConfig,但仍不允许明文 HTTP 流量

java - Netty Camel 示例