gpt4 book ai didi

c++ - 网络浏览器中定义的 zlib gzip 无效响应 (c++)

转载 作者:行者123 更新时间:2023-11-28 04:26:11 28 4
gpt4 key购买 nike

我有一个使用 C++ 的 FastCGI 应用程序。我喜欢用 gzip 压缩将我的回复发送给客户端。
(ZLIB 版本“1.2.11”)
这是我的源代码示例:

#pragma warning (disable : 4231)
#pragma warning(disable : 4996)
//3:45 PM 11/24/2018
#if !(defined(_WIN32)||defined(_WIN64)) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#error Have to check !TODO
#else
#if !defined(_IOSTREAM_)
#include <iostream>
#endif//!_IOSTREAM_
#ifndef _WINDOWS_
#include <windows.h>
#endif//!_WINDOWS_
#endif//_WIN32||_WIN64/__unix__
#if !defined(_INC_STDIO)
#include <stdio.h> /* defines FILENAME_MAX, printf, sprintf */
#endif//!_INC_STDIO
#ifndef _XSTRING_
#include <string>// !_XSTRING_// memcpy, memset
#endif //!_XSTRING_
#if !defined(ZLIB_H)
#include <zlib.h>
#endif//!ZLIB_H
#if !defined(_SSTREAM_)
#include <sstream> // std::stringstream
#endif//_SSTREAM_
#if !defined(CHUNK)
#define CHUNK 16384
#endif//!CHUNK
#ifndef OS_CODE
# define OS_CODE 0x03 /* assume Unix */
#endif//!OS_CODE
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif//!MAX_MEM_LEVEL
#if !defined(assert)
#define assert(expression) ((void)0)
#endif//!assert

static int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
void __write_magic_header(std::stringstream&output) {
char*dest = (char*)malloc(10);
sprintf(dest, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], Z_DEFLATED, 0 /*flags*/, 0, 0, 0, 0 /*time*/, 0 /*xflags*/, OS_CODE);
output.write(const_cast<const char*>(dest), 10);
free(dest);
};
int ____def_strm(std::stringstream&source, std::stringstream&dest, int level = Z_BEST_SPEED) {
//6:00 AM 1/18/2019
int ret, flush;
unsigned have;
z_stream strm;
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2_(&strm, level, Z_DEFLATED,
-MAX_WBITS,
DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
ZLIB_VERSION, (int)sizeof(z_stream));
if (ret != Z_OK)
return ret;
/* compress until end of stream */
std::streamsize n;
source.seekg(0, std::ios::end);//Go to end of stream
std::streamoff size = source.tellg();
source.seekg(0, std::ios::beg);//Back to begain of stream
int write_len = 0;
do {
char in[CHUNK];
n = source.rdbuf()->sgetn(in, CHUNK);
strm.avail_in = (uInt)n;
size -= n;
flush = size <= 0 ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = (Bytef*)in;
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
char out[CHUNK];
strm.avail_out = CHUNK;
strm.next_out = (Bytef*)out;
ret = deflate(&strm, flush); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
have = CHUNK - strm.avail_out;
dest.write(out, have);
write_len += have;
} while (strm.avail_out == 0);
assert(strm.avail_in == 0); /* all input will be used */
/* done when last data in file processed */
} while (flush != Z_FINISH);
assert(ret == Z_STREAM_END); /* stream will be complete */
/* clean up and return */
(void)deflateEnd(&strm);
return write_len;
};
void compress_gzip (std::stringstream&source, std::stringstream&output) {
__write_magic_header(output);
____def_strm(source, output);
return;
};
void gzip_test(int loop) {
std::stringstream body(std::stringstream::in | std::stringstream::out | std::stringstream::binary);
for (int i = 0; i < loop; i++) {
body << "<b>Hello World</b><br/>";
body << "<a href=\"/wiki/General-purpose_programming_language\" title=\"General-purpose programming language\">general-purpose programming language</a>";
body << "\r\n";
}
std::stringstream compressed(std::stringstream::in | std::stringstream::out | std::stringstream::binary);
compress_gzip(body, compressed);
std::stringstream().swap(body);
std::cout << compressed.str();
std::stringstream().swap(compressed);
};
void write_header(const char* ct) {
std::cout << "Content-Type:" << ct << "\n";
std::cout << "Accept-Ranges:bytes\n";
};
int main(int argc, char *argv[], char*envp[]) {
//100 problem ==> ERR_CONTENT_DECODING_FAILED
//1000 problem ==> ERR_CONTENT_DECODING_FAILED
//10000 Ok
write_header("text/plain");
std::cout << "Content-Encoding:gzip\n";
std::cout << "\r\n";
gzip_test(10000);
return EXIT_SUCCESS;
};

它的工作,但我认为这个程序有错误,但我无法弄清楚。
问题如下:
如果 gzip_test(10000); 然后确定
如果 gzip_test(100); 浏览器显示 ERR_CONTENT_DECODING_FAILED
如果 gzip_test(1000); 浏览器显示 ERR_CONTENT_DECODING_FAILED
请帮我解决这个错误。

成功响应:
Success response

错误响应:
Error response

最佳答案

您没有编写包含 CRC 和数据长度的 gzip 页脚:

std::streamoff size = source.tellg();
int totalSize = size;
int tcrc = 0;
...
n = source.rdbuf()->sgetn( in, CHUNK );
strm.avail_in = (uInt)n;
tcrc = crc32( tcrc, (uint8_t*)in, n );
...
(void)deflateEnd( &strm );
dest.write( (char*)&tcrc, sizeof( tcrc ) );
dest.write( (char*)&totalSize, sizeof( totalSize ) );
return write_len;

您的 __write_magic_header 方法也不正确,因为它只分配了 10 个字节,然后用 sprintf 写入了 10 个字符,这实际上会写入 11 个字节溢出您的缓冲区。

在 Windows 上,您无法通过 std::cout 发送二进制数据,您遇到的问题与使用 ofstream 打开文件而不指定 binary。要修复此问题,请在使用 std::cout 之前调用以下命令:

_setmode( _fileno( stdout ), _O_BINARY );

其他与您的问题无关的几点:

  1. 不要用 #ifdef 包装您的 includes,您使用的宏是实现细节,在现代编译器上应该没有/可以忽略不计的性能差异。
  2. 不要在方法名称或其他标识符的开头使用“__”,这些名称(连同“_”后跟大写字母)保留供编译器使用。

关于c++ - 网络浏览器中定义的 zlib gzip 无效响应 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54256829/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com