使用gzip缩小POCO HttpResponse会削减内容

时间:2018-01-09 07:30:17

标签: c++ gzip zlib poco poco-libraries

我正在使用POCO 1.7.8编写HTTP服务器。问题是当使用gzip来缩小响应数据时:

std::string content = "HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT";
response->set("Content-Encoding", "gzip");
std::ostream& responseStream = response->send();
Poco::DeflatingOutputStream deflater(responseStream, Poco::DeflatingStreamBuf::STREAM_GZIP);
deflater << content;
deflater.close();

客户的回应是:

HELLO WORLD, THIS IS LONGISH STRING

回复标题:

Access-Control-Allow-Headers: origin, x-csrftoken, content-type, accept
Access-Control-Allow-Methods:POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Connection: Close
Content-Encoding: gzip
Content-Language: en
Content-Length: 45
Content-Type: text/plain
Date: Tue, 09 Jan 2018 07:52:17 GMT

如果我将其更改为使用ZLIB并将Content-Encoding设置为deflate,则会从服务器正确返回整个响应:

std::string content = "HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT";
response->set("Content-Encoding", "deflate");
std::ostream& responseStream = response->send();
Poco::DeflatingOutputStream deflater(responseStream, Poco::DeflatingStreamBuf::STREAM_ZLIB);
deflater << content;
deflater.close();

客户的回应是:

HELLO WORLD, THIS IS LONGISH STRING THAT IS CUT

回复标题:

Access-Control-Allow-Headers: origin, x-csrftoken, content-type, accept
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 86400
Connection: Close
Content-Encoding: deflate
Content-Language: en
Content-Length: 45
Content-Type: text/plain
Date: Tue, 09 Jan 2018 08:07:36 GMT

我试图找到如何在POCO服务器中完成此示例但没有找到任何内容的示例,我现在有点卡住了。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

您确定要为压缩响应正确设置Content-Length标头吗?或者,在调用send()之前尝试启用分块传输编码。

response->setChunkedTransferEncoding(true);