[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libmicrohttpd] 03/14: Refactored create response functions.
From: |
gnunet |
Subject: |
[libmicrohttpd] 03/14: Refactored create response functions. |
Date: |
Tue, 19 Apr 2022 19:31:10 +0200 |
This is an automated email from the git hooks/post-receive script.
karlson2k pushed a commit to branch master
in repository libmicrohttpd.
commit 71633eb91757cbe6dd3419cc2cacc90d26abdc67
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Mon Apr 18 10:35:42 2022 +0300
Refactored create response functions.
Moved away from the deprecated function, shortened typical call chain.
---
src/microhttpd/response.c | 100 ++++++++++++++++++++++++++--------------------
src/microhttpd/response.h | 2 +-
2 files changed, 57 insertions(+), 45 deletions(-)
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index 05131fd5..b63db1e5 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -1,7 +1,7 @@
/*
This file is part of libmicrohttpd
Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff
- Copyright (C) 2015-2021 Evgeny Grin (Karlson2k)
+ Copyright (C) 2015-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
@@ -1296,38 +1296,16 @@ MHD_create_response_from_data (size_t size,
int must_free,
int must_copy)
{
- struct MHD_Response *response;
- void *mhd_copy;
+ enum MHD_ResponseMemoryMode mode;
- if ((must_copy) && (size > 0))
- {
- if (NULL == data)
- return NULL;
- mhd_copy = malloc (size);
- if (NULL == mhd_copy)
- return NULL;
- memcpy (mhd_copy, data, size);
- must_free = MHD_YES;
- data = mhd_copy;
- }
+ if (0 != must_copy)
+ mode = MHD_RESPMEM_MUST_COPY;
+ else if (0 != must_free)
+ mode = MHD_RESPMEM_MUST_FREE;
else
- mhd_copy = NULL;
+ mode = MHD_RESPMEM_PERSISTENT;
- response =
- MHD_create_response_from_buffer_with_free_callback_cls (size,
- data,
- must_free ?
- &free : NULL,
- data);
- if (NULL == response)
- {
- if (NULL != mhd_copy)
- free (mhd_copy);
- return NULL;
- }
- if (must_copy)
- response->data_buffer_size = size;
- return response;
+ return MHD_create_response_from_buffer (size, data, mode);
}
@@ -1353,10 +1331,50 @@ MHD_create_response_from_buffer (size_t size,
void *buffer,
enum MHD_ResponseMemoryMode mode)
{
- return MHD_create_response_from_data (size,
- buffer,
- mode == MHD_RESPMEM_MUST_FREE,
- mode == MHD_RESPMEM_MUST_COPY);
+ struct MHD_Response *r;
+ void *mhd_copy;
+ MHD_ContentReaderFreeCallback crfc;
+ void *crfc_cls;
+
+ if ((MHD_RESPMEM_MUST_COPY == mode) && (size > 0))
+ {
+ if (NULL == buffer)
+ return NULL;
+ mhd_copy = malloc (size);
+ if (NULL == mhd_copy)
+ return NULL;
+ crfc = &free;
+ crfc_cls = mhd_copy;
+ memcpy (mhd_copy, buffer, size);
+ buffer = mhd_copy;
+ }
+ else if (MHD_RESPMEM_MUST_FREE == mode)
+ {
+ mhd_copy = NULL;
+ crfc = &free;
+ crfc_cls = buffer;
+ }
+ else
+ {
+ mhd_copy = NULL;
+ crfc = NULL;
+ crfc_cls = NULL;
+ }
+
+ r = MHD_create_response_from_buffer_with_free_callback_cls (size,
+ buffer,
+ crfc,
+ crfc_cls);
+ if (NULL == r)
+ {
+ if (NULL != mhd_copy)
+ free (mhd_copy);
+ return NULL;
+ }
+ /* TODO: remove the next check, the buffer should not be modifiable */
+ if (MHD_RESPMEM_MUST_COPY == mode)
+ r->data_buffer_size = size;
+ return r;
}
@@ -1383,16 +1401,10 @@ MHD_create_response_from_buffer_with_free_callback
(size_t size,
MHD_ContentReaderFreeCallback
crfc)
{
- struct MHD_Response *r;
-
- r = MHD_create_response_from_data (size,
- buffer,
- MHD_YES,
- MHD_NO);
- if (NULL == r)
- return r;
- r->crfc = crfc;
- return r;
+ return MHD_create_response_from_buffer_with_free_callback_cls (size,
+ buffer,
+ crfc,
+ buffer);
}
diff --git a/src/microhttpd/response.h b/src/microhttpd/response.h
index 4d1e84b5..ba6c4ab8 100644
--- a/src/microhttpd/response.h
+++ b/src/microhttpd/response.h
@@ -1,7 +1,7 @@
/*
This file is part of libmicrohttpd
Copyright (C) 2007 Daniel Pittman and Christian Grothoff
- Copyright (C) 2015-2021 Karlson2k (Evgeny Grin)
+ Copyright (C) 2015-2022 Karlson2k (Evgeny Grin)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [libmicrohttpd] branch master updated (03c29b63 -> 4b6419b1), gnunet, 2022/04/19
- [libmicrohttpd] 01/14: refactoring: use 'const' for response buffers, gnunet, 2022/04/19
- [libmicrohttpd] 04/14: microhttpd.h: removed empty line, gnunet, 2022/04/19
- [libmicrohttpd] 03/14: Refactored create response functions.,
gnunet <=
- [libmicrohttpd] 02/14: Added _MHD_EXTERN to the all public functions definitions, gnunet, 2022/04/19
- [libmicrohttpd] 06/14: Added new API function MHD_create_response_from_buffer_static(), gnunet, 2022/04/19
- [libmicrohttpd] 07/14: Fixed drop of 'const' when building internal error response, gnunet, 2022/04/19
- [libmicrohttpd] 12/14: microhttpd.h: minor doxy improvement, gnunet, 2022/04/19
- [libmicrohttpd] 08/14: Fixed missing 'const' qualifiers, gnunet, 2022/04/19
- [libmicrohttpd] 05/14: MHD_get_version(): fixed signed value bit shift, gnunet, 2022/04/19
- [libmicrohttpd] 09/14: Added workaround for external APIs, gnunet, 2022/04/19
- [libmicrohttpd] 11/14: Added new API function MHD_create_response_from_buffer_copy(), gnunet, 2022/04/19
- [libmicrohttpd] 10/14: Minor simplification for ALPN protocols code, gnunet, 2022/04/19
- [libmicrohttpd] 14/14: doc/examples: Fixed drop of 'const' qualifiers, gnunet, 2022/04/19