Properly handle the "compression" flag:

- Fix compression enable flag in the inbound peer
- Fix compression algorithm fetching in protocol message
This commit is contained in:
Gregory Tsipenyuk
2020-12-15 13:50:55 -05:00
committed by Nik Bougalis
parent 42a068ab5e
commit 4159b02753
2 changed files with 12 additions and 4 deletions

View File

@@ -100,7 +100,8 @@ PeerImp::PeerImp(
, request_(std::move(request)) , request_(std::move(request))
, headers_(request_) , headers_(request_)
, compressionEnabled_( , compressionEnabled_(
headers_["X-Offer-Compression"] == "lz4" ? Compressed::On headers_["X-Offer-Compression"] == "lz4" && app_.config().COMPRESSION
? Compressed::On
: Compressed::Off) : Compressed::Off)
{ {
} }

View File

@@ -141,7 +141,11 @@ parseMessageHeader(
MessageHeader hdr; MessageHeader hdr;
auto iter = buffersBegin(bufs); auto iter = buffersBegin(bufs);
// Check valid header // Check valid header compressed message:
// - 4 bits are the compression algorithm, 1st bit is always set to 1
// - 2 bits are always set to 0
// - 26 bits are the payload size
// - 32 bits are the uncompressed data size
if (*iter & 0x80) if (*iter & 0x80)
{ {
hdr.header_size = headerBytesCompressed; hdr.header_size = headerBytesCompressed;
@@ -159,7 +163,7 @@ parseMessageHeader(
return boost::none; return boost::none;
} }
hdr.algorithm = static_cast<compression::Algorithm>(*iter); hdr.algorithm = static_cast<compression::Algorithm>(*iter & 0xF0);
if (hdr.algorithm != compression::Algorithm::LZ4) if (hdr.algorithm != compression::Algorithm::LZ4)
{ {
@@ -184,6 +188,9 @@ parseMessageHeader(
return hdr; return hdr;
} }
// Check valid header uncompressed message:
// - 6 bits are set to 0
// - 26 bits are the payload size
if ((*iter & 0xFC) == 0) if ((*iter & 0xFC) == 0)
{ {
hdr.header_size = headerBytes; hdr.header_size = headerBytes;