diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2019-02-15 15:21:54 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2019-02-15 15:21:54 +0100 |
commit | 0506a80bdc307e3ff26335462918621fe6bdc163 (patch) | |
tree | 4e5cff9313c59fe57215426bd02b4509634f36b1 | |
parent | fb53910b8752495f3e2279cb7bd6ca68050145c9 (diff) | |
download | twister-0506a80bdc307e3ff26335462918621fe6bdc163.tar.gz twister-0506a80bdc307e3ff26335462918621fe6bdc163.tar.bz2 twister-0506a80bdc307e3ff26335462918621fe6bdc163.zip |
Fix Valgrind reports.
-rw-r--r-- | src/twister/taler-twister-service.c | 105 |
1 files changed, 44 insertions, 61 deletions
diff --git a/src/twister/taler-twister-service.c b/src/twister/taler-twister-service.c index 3646c41..ef6a2e1 100644 --- a/src/twister/taler-twister-service.c +++ b/src/twister/taler-twister-service.c @@ -428,20 +428,7 @@ curl_check_hdr (void *buffer, "deflate"))) { TALER_LOG_INFO ("Compressed response\n"); - hr->deflate = GNUNET_YES; - hr->zstate.zalloc = Z_NULL; - hr->zstate.zfree = Z_NULL; - hr->zstate.opaque = Z_NULL; - - if (Z_OK != deflateInit (&hr->zstate, - Z_DEFAULT_COMPRESSION)) - { - TALER_LOG_ERROR ("Failed to initialize compression\n"); - /* FIXME: put here a "emergency call" - * to suddenly return to the client. */ - return MHD_NO; - } } /* Skip "Host:" header as it will be wrong, given @@ -1245,6 +1232,44 @@ delete_object (struct MHD_Connection *con, /** + * Try to compress a response body. + * Updates @a buf and @a buf_size. + * + * @param[in,out] buf pointer to body to compress + * @param[in,out] buf_size pointer to initial size of @a buf + * @return #MHD_YES if @a buf was compressed + */ +static int +body_compress (void **buf, + size_t *buf_size) +{ + Bytef *cbuf; + uLongf cbuf_size; + int ret; + + cbuf_size = compressBound (*buf_size); + cbuf = malloc (cbuf_size); + if (NULL == cbuf) + return MHD_NO; + ret = compress (cbuf, + &cbuf_size, + (const Bytef *) *buf, + *buf_size); + if ( (Z_OK != ret) || + (cbuf_size >= *buf_size) ) + { + /* compression failed */ + free (cbuf); + return MHD_NO; + } + free (*buf); + *buf = (void *) cbuf; + *buf_size = (size_t) cbuf_size; + return MHD_YES; +} + + +/** * Main MHD callback for handling requests. * * @param cls unused @@ -1367,7 +1392,8 @@ create_response (void *cls, "Going to flip the upload object (%s)\n", hr->io_buf); - hr->json = json_loads (hr->io_buf, + hr->json = json_loadb (hr->io_buf, + hr->io_len, JSON_REJECT_DUPLICATES, NULL); flip_object (con, @@ -1701,55 +1727,12 @@ create_response (void *cls, } /** - * COMPRESSION STARTS HERE. + * COMPRESSION STARTS HERE (hr->io_buf/len is data). */ if (GNUNET_YES == hr->deflate) - { - #define CHUNK 1024 - unsigned char out[CHUNK]; - unsigned int uncompressed = hr->io_len; - unsigned int flush; - unsigned int done; - unsigned int zdone = 0; - unsigned int chunk; - - TALER_LOG_DEBUG ("Compressing the response..\n"); - do - { - chunk = GNUNET_MIN (CHUNK, uncompressed); - hr->zstate.avail_in = chunk; - hr->zstate.next_in = (unsigned char *) - &hr->io_buf[hr->io_len - uncompressed]; - flush = CHUNK >= uncompressed ? Z_FINISH : Z_NO_FLUSH; - - do - { - hr->zstate.avail_out = CHUNK; - hr->zstate.next_out = out; - - GNUNET_assert - (Z_STREAM_ERROR != deflate (&hr->zstate, flush)); - done = CHUNK - hr->zstate.avail_out; - - /** - * No check for overflows, as compressing - * data yields always less bytes than the input. - */ - TALER_LOG_DEBUG ("zdone/done: %u/%u\n", - zdone, - done); - memcpy (&hr->io_buf[zdone], - out, - done); - zdone += done; - } - while (0 != done); - TALER_LOG_DEBUG ("Out of inner compression loop\n"); - uncompressed -= chunk; - } - while (Z_FINISH != flush); - deflateEnd (&hr->zstate); - } + GNUNET_assert + (MHD_YES == body_compress ((void **) &hr->io_buf, + &hr->io_len)); hr->response = MHD_create_response_from_buffer (hr->io_len, |