vendredi 18 janvier 2019

zlib gzip invalid response defined in web browser (c++)

I've a FastCGI application with C++. I like to send my response with gzip compression to client.
(ZLIB VERSION "1.2.11")
Here is the sample of my source code:

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);
};
int main(int argc, char *argv[], char*envp[]) {
    //100 problem ==> ERR_CONTENT_DECODING_FAILED
    //1000 problem ==> ERR_CONTENT_DECODING_FAILED
    //10000 Ok
    gzip_test(10000);
    return EXIT_SUCCESS;
};

I think this source code has bug, but i'm unable to figure it out.
if gzip_test(10000); then OK
if gzip_test(100); browser shown ERR_CONTENT_DECODING_FAILED
if gzip_test(1000); browser shown ERR_CONTENT_DECODING_FAILED
Please help me to figure it out this bug.




Aucun commentaire:

Enregistrer un commentaire