Przeglądaj źródła

Replaced MHD_RESPMEM_MUST_FREE with more portable solution in examples

Evgeny Grin (Karlson2k) 3 lat temu
rodzic
commit
a13647d1d0

+ 19 - 13
doc/examples/sessions.c

@@ -347,19 +347,22 @@ fill_v1_form (const void *cls,
   enum MHD_Result ret;
   char *reply;
   struct MHD_Response *response;
+  int reply_len;
   (void) cls; /* Unused */
 
-  if (-1 == MHD_asprintf (&reply,
-                          MAIN_PAGE,
-                          session->value_1))
+  reply_len = MHD_asprintf (&reply,
+                            MAIN_PAGE,
+                            session->value_1);
+  if (0 > reply_len)
   {
     /* oops */
     return MHD_NO;
   }
   /* return static form */
-  response = MHD_create_response_from_buffer (strlen (reply),
-                                              (void *) reply,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback ((size_t) reply_len,
+                                                        (void *) reply,
+                                                        &free);
   add_session_cookie (session, response);
   MHD_add_response_header (response,
                            MHD_HTTP_HEADER_CONTENT_ENCODING,
@@ -389,20 +392,23 @@ fill_v1_v2_form (const void *cls,
   enum MHD_Result ret;
   char *reply;
   struct MHD_Response *response;
+  int reply_len;
   (void) cls; /* Unused */
 
-  if (-1 == MHD_asprintf (&reply,
-                          SECOND_PAGE,
-                          session->value_1,
-                          session->value_2))
+  reply_len = MHD_asprintf (&reply,
+                            SECOND_PAGE,
+                            session->value_1,
+                            session->value_2);
+  if (0 > reply_len)
   {
     /* oops */
     return MHD_NO;
   }
   /* return static form */
-  response = MHD_create_response_from_buffer (strlen (reply),
-                                              (void *) reply,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (reply_len,
+                                                        (void *) reply,
+                                                        &free);
   add_session_cookie (session, response);
   MHD_add_response_header (response,
                            MHD_HTTP_HEADER_CONTENT_ENCODING,

+ 4 - 3
src/examples/demo.c

@@ -383,9 +383,10 @@ update_directory (void)
                        "%s",
                        INDEX_PAGE_FOOTER);
   initial_allocation = rdc.buf_len; /* remember for next time */
-  response = MHD_create_response_from_buffer (rdc.off,
-                                              rdc.buf,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (rdc.off,
+                                                        rdc.buf,
+                                                        &free);
   mark_as_html (response);
 #if FORCE_CLOSE
   (void) MHD_add_response_header (response,

+ 4 - 3
src/examples/demo_https.c

@@ -385,9 +385,10 @@ update_directory (void)
                        "%s",
                        INDEX_PAGE_FOOTER);
   initial_allocation = rdc.buf_len; /* remember for next time */
-  response = MHD_create_response_from_buffer (rdc.off,
-                                              rdc.buf,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (rdc.off,
+                                                        rdc.buf,
+                                                        &free);
   mark_as_html (response);
 #if FORCE_CLOSE
   (void) MHD_add_response_header (response,

+ 5 - 3
src/examples/http_compression.c

@@ -130,9 +130,11 @@ ahc_echo (void *cls,
       can_compress (connection))
     comp = body_compress ((void **) &body_str,
                           &body_len);
-  response = MHD_create_response_from_buffer (body_len,
-                                              body_str,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (body_len,
+                                                        body_str,
+                                                        &free);
+
   if (NULL == response)
   {
     free (body_str);

+ 8 - 6
src/examples/post_example.c

@@ -332,9 +332,10 @@ fill_v1_form (const void *cls,
             MAIN_PAGE,
             session->value_1);
   /* return static form */
-  response = MHD_create_response_from_buffer (slen,
-                                              (void *) reply,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (slen,
+                                                        (void *) reply,
+                                                        &free);
   if (NULL == response)
   {
     free (reply);
@@ -383,9 +384,10 @@ fill_v1_v2_form (const void *cls,
             session->value_1,
             session->value_2);
   /* return static form */
-  response = MHD_create_response_from_buffer (slen,
-                                              (void *) reply,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (slen,
+                                                        (void *) reply,
+                                                        &free);
   if (NULL == response)
   {
     free (reply);

+ 4 - 2
src/examples/querystring_example.c

@@ -72,8 +72,10 @@ ahc_echo (void *cls,
     free (me);
     return MHD_NO;  /* Error forming the response body */
   }
-  response = MHD_create_response_from_buffer (resp_len, me,
-                                              MHD_RESPMEM_MUST_FREE);
+  response =
+    MHD_create_response_from_buffer_with_free_callback (resp_len,
+                                                        (void *) me,
+                                                        &free);
   if (response == NULL)
   {
     free (me);

+ 6 - 4
src/include/microhttpd.h

@@ -3624,10 +3624,12 @@ enum MHD_ResponseMemoryMode
    * Buffer is heap-allocated with `malloc()` (or equivalent) and
    * should be freed by MHD after processing the response has
    * concluded (response reference counter reaches zero).
-   * @warning Make sure that your application and MHD are using the same
-   *          C-runtime library (especially important for W32). if in doubt,
-   *          use function MHD_create_response_from_buffer_with_free_callback()
-   *          with '&free' as crfc parameter.
+   * The more portable way to automatically free the buffer is function
+   * MHD_create_response_from_buffer_with_free_callback() with '&free' as
+   * crfc parameter as it does not require to use the same runtime library.
+   * @warning It is critical to make sure that the same C-runtime library
+   *          is used by both application and MHD (especially
+   *          important for W32).
    * @ingroup response
    */
   MHD_RESPMEM_MUST_FREE,