Browse Source

Simplified the sg_httpres_zsendbinary() and solved double free on its internal buffer.

silvioprog 6 years ago
parent
commit
3e2ee4f5ce
6 changed files with 14 additions and 68 deletions
  1. 1 0
      BUILD.md
  2. 12 16
      src/sg_httpres.c
  3. 1 2
      src/sg_httpres.h
  4. 0 40
      src/sg_utils.c
  5. 0 9
      src/sg_utils.h
  6. 0 1
      test/test_utils.c

+ 1 - 0
BUILD.md

@@ -27,6 +27,7 @@ There are a few options of the Sagui building, they are:
 -DSG_BUILD_<EXAMPLE>_EXAMPLE=<ON|OFF>
 -DSG_BUILD_<TEST>_TESTING=<ON|OFF>
 -DSG_HTTPS_SUPPORT=<ON|OFF>
+-DSG_HTTP_COMPRESSION=<ON|OFF>
 -DSG_PATH_ROUTING=<ON|OFF>
 ```
 

+ 12 - 16
src/sg_httpres.c

@@ -266,33 +266,29 @@ error:
 
 int sg_httpres_zsendbinary(struct sg_httpres *res, void *buf, size_t size, const char *content_type,
                            unsigned int status) {
-    Bytef *dest;
-    uLongf dest_size;
-    int errnum;
+    void *zbuf;
+    size_t zsize;
     if (!res || !buf || ((ssize_t) size < 0) || (status < 100) || (status > 599))
         return EINVAL;
     if (res->handle)
         return EALREADY;
     if (size > 0) {
-        dest_size = compressBound(size);
-        dest = sg_malloc(dest_size);
-        if (!dest)
+        zsize = compressBound(size);
+        zbuf = sg_malloc(zsize);
+        if (!zbuf)
             return ENOMEM;
-        errnum = sg__compress(buf, size, dest, (size_t *) &dest_size);
-        if ((errnum != Z_OK) || (dest_size >= size)) {
-            dest_size = size;
-            memcpy(dest, buf, dest_size);
+        if ((compress2(zbuf, &zsize, buf, size, Z_BEST_COMPRESSION) != Z_OK) || (zsize >= size)) {
+            zsize = size;
+            memcpy(zbuf, buf, zsize);
         } else
             sg_strmap_set(&res->headers, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate");
     } else {
-        dest_size = 0;
-        dest = (Bytef *) strdup("");
+        zbuf = strdup("");
+        zsize = 0;
     }
-    res->handle = MHD_create_response_from_buffer(dest_size, dest, MHD_RESPMEM_MUST_FREE);
-    if (!res->handle) {
-        sg_free(dest);
+    res->handle = MHD_create_response_from_buffer(zsize, zbuf, MHD_RESPMEM_MUST_FREE);
+    if (!res->handle)
         return ENOMEM;
-    }
     if (content_type)
         sg_strmap_set(&res->headers, MHD_HTTP_HEADER_CONTENT_TYPE, content_type);
     res->status = status;

+ 1 - 2
src/sg_httpres.h

@@ -28,10 +28,9 @@
 #ifndef SG_HTTPRES_H
 #define SG_HTTPRES_H
 
-#include <time.h>
+#include "sg_macros.h"
 #ifdef SG_HTTP_COMPRESSION
 #include <stdint.h>
-#include "sg_macros.h"
 #include "zlib.h"
 #endif
 #include "microhttpd.h"

+ 0 - 40
src/sg_utils.c

@@ -37,9 +37,6 @@
 #include <unistd.h>
 #include <ctype.h>
 #include "sg_macros.h"
-#ifdef SG_HTTP_COMPRESSION
-#include "zlib.h"
-#endif
 #include "sagui.h"
 #include "sg_utils.h"
 
@@ -194,43 +191,6 @@ bool sg__is_cookie_val(const char *val) {
     return true;
 }
 
-#ifdef SG_HTTP_COMPRESSION
-
-int sg__compress(const void *src, size_t src_size, void *dest, size_t *dest_size) {
-    z_stream stream;
-    const uInt max = (uInt) -1;
-    uLong left;
-    int errnum;
-    left = *dest_size;
-    *dest_size = 0;
-    stream.zalloc = NULL;
-    stream.zfree = NULL;
-    stream.opaque = NULL;
-    errnum = deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
-    if (errnum != Z_OK)
-        return errnum;
-    stream.next_out = dest;
-    stream.avail_out = 0;
-    stream.next_in = (z_const Bytef *) src;
-    stream.avail_in = 0;
-    do {
-        if (stream.avail_out == 0) {
-            stream.avail_out = left > (uLong) max ? max : (uInt) left;
-            left -= stream.avail_out;
-        }
-        if (stream.avail_in == 0) {
-            stream.avail_in = src_size > (uLong) max ? max : (uInt) src_size;
-            src_size -= stream.avail_in;
-        }
-        errnum = deflate(&stream, src_size ? Z_NO_FLUSH : Z_FINISH);
-    } while (errnum == Z_OK);
-    *dest_size = stream.total_out;
-    deflateEnd(&stream);
-    return errnum == Z_STREAM_END ? Z_OK : errnum;
-}
-
-#endif
-
 /* Version. */
 
 unsigned int sg_version(void) {

+ 0 - 9
src/sg_utils.h

@@ -34,9 +34,6 @@
 #include <stddef.h>
 #include <stdbool.h>
 #include "sg_macros.h"
-#ifdef SG_HTTP_COMPRESSION
-#include "zlib.h"
-#endif
 #include "sagui.h"
 
 #ifdef _WIN32
@@ -69,12 +66,6 @@ SG__EXTERN bool sg__is_cookie_name(const char *name);
 
 SG__EXTERN bool sg__is_cookie_val(const char *val);
 
-#ifdef SG_HTTP_COMPRESSION
-
-SG__EXTERN int sg__compress(const void *src, size_t src_size, void *dest, size_t *dest_size);
-
-#endif
-
 SG__EXTERN void sg__err_cb(__SG_UNUSED void *cls, const char *err);
 
 #endif /* SG_UTILS_H */

+ 0 - 1
test/test_utils.c

@@ -405,7 +405,6 @@ int main(void) {
     test__strjoin();
     test__is_cookie_name();
     test__is_cookie_val();
-    /*TODO: test__compress() */
     test_version();
     test_malloc();
     test_alloc();