gpt4 book ai didi

java - 如何防止HPACK解压失败?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:02 29 4
gpt4 key购买 nike

所以我正在为一个学校项目构建一个 HTTP/2.0 服务器库,我偶然发现了一个问题。每当我尝试发送标题框架时,我都会收到来自 Chrome 的压缩错误(和 Firefox,Edge 没有响应)。我正在使用 twitter/hpack 库来压缩和解压缩:

public static ByteBuffer compress(ByteBuffer bb) {
bb.rewind();
Encoder encoder = new Encoder(4096);
byte[] bytes = new byte[bb.remaining()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = bb.get();
}
String string = new String(bytes);
String[] split = string.split("[\\n\\r]+");
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
for (String s1 : split) {
String[] s1Split = s1.split(":", 3);
if (s1Split.length == 3)
encoder.encodeHeader(os, (":" + s1Split[1]).getBytes(), s1Split[2].getBytes(), false);
else if (s1Split.length == 2)
encoder.encodeHeader(os, s1Split[0].getBytes(), s1Split[1].getBytes(), false);
}
} catch (IOException e) {
e.printStackTrace();
}
return ByteBuffer.wrap(os.toByteArray());
}

其中 bb 只是一个包含字符串的字节缓冲区:

":status:200\r\ncontent-length:155\r\ncontent-type:text/html;charset=utf-8\r\n"

我能够再次成功解压结果,以及最初从 Chrome 收到的压缩 GET header ,但是当我尝试将压缩结果发送到 Chrome 时,它​​给我一个错误。以下是发送/接收的帧的打印:

Send: frames.SettingsFrame: length=0, flags=0b0, streamId=0, settings={Settings: [SETTINGS_HEADER_TABLE_SIZE=UNDEFINED, SETTINGS_ENABLE_PUSH=UNDEFINED, SETTINGS_MAX_CONCURRENT_STREAMS=UNDEFINED, SETTINGS_INITIAL_WINDOW_SIZE=UNDEFINED, SETTINGS_MAX_FRAME_SIZE=UNDEFINED, SETTINGS_MAX_HEADER_LIST_SIZE=UNDEFINED]}
Recv: frames.HeadersFrame: length=301, flags=0b100101, streamId=1, padLength=0, E=true, streamDependency=0, weight=256, headerBlockFragment={:method: GET\r\n:authority: localhost\r\n:scheme: https\r\n:path: /\r\ncache-control: max-age=0\r\nupgrade-insecure-requests: 1\r\nuser-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36\r\naccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\naccept-encoding: gzip, deflate, br\r\naccept-language: nb-NO,nb;q=0.9,no;q=0.8,nn;q=0.7,en-US;q=0.6,en;q=0.5\r\ncookie: Idea-cda28813=32d9f659-df76-4e58-9116-41939eaf3d24\r\n}
Send: frames.HeadersFrame: length=76, flags=0b100100, streamId=1, padLength=0, E=true, streamDependency=0, weight=256, headerBlockFragment={:status:200\r\ncontent-length:155\r\ncontent-type:text/html;charset=utf-8\r\n}
Send: frames.DataFrame: length=152, flags=0b1, streamId=1, padLength=0, data={<!DOCTYPE html>\r\n<html lang="en">\r\n<head>\r\n <meta charset="UTF-8">\r\n <title>Hello</title>\r\n</head>\r\n<body>\r\n<p>Hello world!</p>\r\n</body>\r\n</html>}
Recv: frames.SettingsFrame: length=0, flags=0b1, streamId=0, settings={Settings: [SETTINGS_HEADER_TABLE_SIZE=UNDEFINED, SETTINGS_ENABLE_PUSH=UNDEFINED, SETTINGS_MAX_CONCURRENT_STREAMS=UNDEFINED, SETTINGS_INITIAL_WINDOW_SIZE=UNDEFINED, SETTINGS_MAX_FRAME_SIZE=UNDEFINED, SETTINGS_MAX_HEADER_LIST_SIZE=UNDEFINED]}
Recv: frames.GoAwayFrame: length=45, flags=0b0, streamId=0, lastStreamId=0, errorCode=Compression error, additionalData={Framer error: 6 (DECOMPRESS_FAILURE).}

此外,如果我将任何 header 名称设为大写,Chrome 会响应错误:

HTTP2_SESSION_RECV_INVALID_HEADER
--> error = "Upper case characters in header name."
--> header_name = "Content-type"
--> header_value = "text/html;charset=utf-8"

这意味着 Chrome 也可以解压其中的一部分,但在解压整个过程时仍然会失败。

最佳答案

你确定框架的长度是正确的吗?您必须使用压缩的帧长度。不是压缩前的长度。

关于java - 如何防止HPACK解压失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50039902/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com