http_chunked_compression: fixed errors

This commit is contained in:
Evgeny Grin (Karlson2k)
2022-06-01 22:12:38 +03:00
parent 5821a7f303
commit badf75607b
+17 -13
View File
@@ -1,6 +1,7 @@
/*
This file is part of libmicrohttpd
Copyright (C) 2019 Christian Grothoff (and other contributing authors)
Copyright (C) 2019-2022 Evgeny Grin (Karlson2k)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -20,15 +21,21 @@
* @file http_chunked_compression.c
* @brief example for how to compress a chunked HTTP response
* @author Silvio Clecio (silvioprog)
* @author Karlson2k (Evgeny Grin)
*/
#include "platform.h"
#ifndef ZLIB_CONST
/* Correct API with const pointer for input data is required */
#define ZLIB_CONST 1
#endif /* ! ZLIB_CONST */
#include <zlib.h>
#include <microhttpd.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif /* HAVE_LIMITS_H */
#include <stddef.h>
#include <stdint.h>
#ifndef SSIZE_MAX
#ifdef __SSIZE_MAX__
@@ -76,7 +83,7 @@ compress_buf (z_stream *strm, const void *src, size_t src_size, size_t *offset,
flush = Z_SYNC_FLUSH;
}
*offset += strm->avail_in;
strm->next_in = (Bytef *) src;
strm->next_in = (const Bytef *) src;
do
{
strm->avail_out = CHUNK;
@@ -92,7 +99,7 @@ compress_buf (z_stream *strm, const void *src, size_t src_size, size_t *offset,
return MHD_NO;
}
*dest = tmp_dest;
memcpy ((*dest) + ((*dest_size) - have), tmp, have);
memcpy (((uint8_t *) (*dest)) + ((*dest_size) - have), tmp, have);
}
while (0 == strm->avail_out);
}
@@ -109,6 +116,7 @@ read_cb (void *cls, uint64_t pos, char *mem, size_t size)
void *buf;
ssize_t ret;
size_t offset;
size_t r_size;
if (pos > SSIZE_MAX)
return MHD_CONTENT_READER_END_WITH_ERROR;
@@ -116,24 +124,20 @@ read_cb (void *cls, uint64_t pos, char *mem, size_t size)
src = malloc (size);
if (NULL == src)
return MHD_CONTENT_READER_END_WITH_ERROR;
ret = fread (src, 1, size, holder->file);
if (ret < 0)
r_size = fread (src, 1, size, holder->file);
if (0 == r_size)
{
ret = MHD_CONTENT_READER_END_WITH_ERROR;
ret = (0 != ferror (holder->file)) ?
MHD_CONTENT_READER_END_WITH_ERROR : MHD_CONTENT_READER_END_OF_STREAM;
goto done;
}
if (0 == ret)
{
ret = MHD_CONTENT_READER_END_OF_STREAM;
goto done;
}
if (MHD_YES != compress_buf (&holder->stream, src, ret, &offset, &buf, &size,
holder->buf))
if (MHD_YES != compress_buf (&holder->stream, src, r_size, &offset, &buf,
&size, holder->buf))
ret = MHD_CONTENT_READER_END_WITH_ERROR;
else
{
memcpy (mem, buf, size);
ret = size;
ret = (ssize_t) size;
}
free (buf); /* Buf may be set even on error return. */
done: