我们如何区分deflate stream和deflateRaw流? [英] How can we distinguish deflate stream from deflateRaw stream?

查看:684
本文介绍了我们如何区分deflate stream和deflateRaw流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些HTTP服务器发送deflate raw body(没有zlib头)而不是实际的deflate body。请参阅讨论:为什么真实世界的服务器更喜欢gzip over deflate encoding?

Some HTTP servers send deflate raw body (without zlib headers) instead of actual deflate body. See discussion at: Why do real-world servers prefer gzip over deflate encoding?

是否可以检测到它们并在Node.js中正确处理膨胀?我的意思是除了尝试 createInflate 他们并捕获错误然后再次尝试 createInflateRaw

Is it possible to detect them and handle inflate properly in Node.js? I mean besides try to createInflate them and catch error then try createInflateRaw again.

推荐答案

如果十六进制中的第一个字节具有 8 的低nybble,则它是一个zlib流。否则它是原始的放气流。 (假设您事先知道唯一可能的选择是有效的zlib流或有效的deflate流。)原始deflate流在低优先级中永远不会有 8 nybble,但是zlib流总是会。

If the first byte in hex has a low nybble of 8, then it is a zlib stream. Otherwise it is a raw deflate stream. (Assuming that you know a priori that the only possible choices are a valid zlib stream or a valid deflate stream.) A raw deflate stream will never have an 8 in the low first nybble, but a zlib stream always will.

背景:

zlib头格式将压缩方法放入第一个字节的低nybble。对于deflate,该压缩方法总是 8

The zlib header format puts the compression method in the low nybble of the first byte. That compression method is always 8 for deflate.

原始deflate流中的位序列从最不重要的开始字节的位。如果前三位是 000 (因为它们是 8 ),则表示存储(未压缩的块) ),它不是最后一个块。存储块将输入的字节放在字节边界上。因此,在写入 000 位之后压缩器完成的下一步工作是用零位填充字节的其余部分以到达下一个字节边界。因此,下一位永远不会是 1 ,因此有效的deflate流不可能让前四位为 1000 ,或者第一个nybble是 8 。 (注意,这些位是从下往上读取的。)

The bit sequence in a raw deflate stream starts from the least significant bits of the bytes. If the first three bits are 000 (as they are for an 8), that signifies a stored (not compressed block), and it is not the last block. Stored blocks put the bytes of the input on byte boundaries. So the next thing that is done by the compressor after writing the 000 bits is to fill out the rest of the byte with zero bits to get to the next byte boundary. Therefore the next bit will never be a 1, so it is not possible for a valid deflate stream to have the first four bits be 1000, or the first nybble to be 8. (Note that the bits are read from the bottom up.)

有效的deflate流的第一个(即低位)nybble只能是 0 .. 5 a .. d 。如果你看到 6 .. 9 e ,或者 f ,那么它不是有效的deflate流。

The first (i.e. low) nybble of a valid deflate stream can only be 0..5 or a..d. If you see 6..9, e, or f, then it is not a valid deflate stream.

这篇关于我们如何区分deflate stream和deflateRaw流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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