如何防止HPACK解压缩失败?

时间:2018-04-26 09:49:10

标签: java google-chrome http2 compression

所以我正在为学校项目构建一个HTTP / 2.0服务器库,我偶然发现了一个问题。每当我尝试发送标题框时,我都会收到来自Chrome(和Firefox的压缩错误)的压缩错误。我正在使用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也可以解压缩其中一部分,但在整个过程中仍然会失败。

1 个答案:

答案 0 :(得分:3)

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