mirror of
https://github.com/typesense/typesense.git
synced 2025-05-17 12:12:35 +08:00
header check for gzip files
This commit is contained in:
parent
22957fbe03
commit
3b95f2051a
@ -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<unsigned int>(outbuffer.size());
|
||||
zs.next_out = reinterpret_cast<Bytef*>(&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<unsigned int>(outbuffer.size());
|
||||
zs.next_out = reinterpret_cast<Bytef *>(&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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user