【问题标题】:How to prevent HPACK decompress failure?如何防止HPACK解压失败?
【发布时间】:2018-10-06 23:18:28
【问题描述】:

所以我正在为一个学校项目构建一个 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 标头,但是当我尝试将压缩结果发送到 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).}

此外,如果我将任何标题名称设为大写,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 google-chrome http2 compression


    【解决方案1】:

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

    【讨论】:

    • 哇!不敢相信我错过了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2017-03-09
    相关资源
    最近更新 更多