From 3b95f2051ab9a998a58edb31388813a6ff809569 Mon Sep 17 00:00:00 2001 From: krunal1313 Date: Fri, 23 Jun 2023 15:32:11 +0530 Subject: [PATCH] header check for gzip files --- src/http_server.cpp | 53 +++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/http_server.cpp b/src/http_server.cpp index 71304aca..5e45c86d 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -635,44 +635,45 @@ int HttpServer::async_req_cb(void *ctx, int is_end_stream) { if(can_process_async || is_end_stream) { // For async streaming requests, handler should be invoked for every aggregated chunk // For a non streaming request, buffer body and invoke only at the end - if(!zstream_initialized) { + if(!zstream_initialized && + (31 == (int)request->body[0] && -117 == (int)request->body[1])) { //gzip header if (inflateInit2(&zs, 16 + MAX_WBITS) != Z_OK) throw (std::runtime_error("inflateInit failed while decompressing.")); zstream_initialized = true; } - std::string outbuffer; - outbuffer.resize(10 * request->body.size()); + if(zstream_initialized) { + std::string outbuffer; + outbuffer.resize(10 * request->body.size()); - zs.next_in = (Bytef *) request->body.c_str(); - zs.avail_in = request->body.size(); - std::size_t size_uncompressed = 0; - int ret = 0; - do - { - zs.avail_out = static_cast(outbuffer.size()); - zs.next_out = reinterpret_cast(&outbuffer[0] + size_uncompressed); - ret = inflate(&zs, Z_FINISH); - if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) - { - std::string error_msg = zs.msg; + zs.next_in = (Bytef *) request->body.c_str(); + zs.avail_in = request->body.size(); + std::size_t size_uncompressed = 0; + int ret = 0; + do { + zs.avail_out = static_cast(outbuffer.size()); + zs.next_out = reinterpret_cast(&outbuffer[0] + size_uncompressed); + ret = inflate(&zs, Z_FINISH); + if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) { + std::string error_msg = zs.msg; + inflateEnd(&zs); + throw std::runtime_error(error_msg); + } + + size_uncompressed += (outbuffer.size() - zs.avail_out); + } while (zs.avail_out == 0); + + if (ret == Z_STREAM_END) { inflateEnd(&zs); - throw std::runtime_error(error_msg); + zstream_initialized = false; } - size_uncompressed += (outbuffer.size() - zs.avail_out); - } while (zs.avail_out == 0); + outbuffer.resize(size_uncompressed); - if(ret == Z_STREAM_END) { - inflateEnd(&zs); - zstream_initialized = false; + request->body = outbuffer; + request->chunk_len = outbuffer.size(); } - outbuffer.resize(size_uncompressed); - - request->body = outbuffer; - request->chunk_len = outbuffer.size(); - if(request->first_chunk_aggregate) { request->first_chunk_aggregate = false; }