aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/response.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/response.c')
-rw-r--r--src/microhttpd/response.c80
1 files changed, 43 insertions, 37 deletions
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index 8ac0c58e..ab04061c 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -1297,45 +1297,34 @@ MHD_create_response_from_data (size_t size,
1297 int must_copy) 1297 int must_copy)
1298{ 1298{
1299 struct MHD_Response *response; 1299 struct MHD_Response *response;
1300 void *tmp; 1300 void *mhd_copy;
1301 1301
1302 if ((NULL == data) && (size > 0))
1303 return NULL;
1304 if (MHD_SIZE_UNKNOWN == size)
1305 return NULL;
1306 if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response))))
1307 return NULL;
1308 response->fd = -1;
1309#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1310 if (! MHD_mutex_init_ (&response->mutex))
1311 {
1312 free (response);
1313 return NULL;
1314 }
1315#endif
1316 if ((must_copy) && (size > 0)) 1302 if ((must_copy) && (size > 0))
1317 { 1303 {
1318 if (NULL == (tmp = malloc (size))) 1304 if (NULL == data)
1319 {
1320#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1321 MHD_mutex_destroy_chk_ (&response->mutex);
1322#endif
1323 free (response);
1324 return NULL; 1305 return NULL;
1325 } 1306 mhd_copy = malloc (size);
1326 memcpy (tmp, data, size); 1307 if (NULL == mhd_copy)
1308 return NULL;
1309 memcpy (mhd_copy, data, size);
1327 must_free = MHD_YES; 1310 must_free = MHD_YES;
1328 data = tmp; 1311 data = mhd_copy;
1329 } 1312 }
1330 if (must_free) 1313 else
1314 mhd_copy = NULL;
1315
1316 response =
1317 MHD_create_response_from_buffer_with_free_callback_cls (size,
1318 data,
1319 must_free ?
1320 &free : NULL,
1321 data);
1322 if (NULL == response)
1331 { 1323 {
1332 response->crfc = &free; 1324 if (NULL != mhd_copy)
1333 response->crc_cls = data; 1325 free (mhd_copy);
1326 return NULL;
1334 } 1327 }
1335 response->reference_count = 1;
1336 response->total_size = size;
1337 response->data = data;
1338 response->data_size = size;
1339 if (must_copy) 1328 if (must_copy)
1340 response->data_buffer_size = size; 1329 response->data_buffer_size = size;
1341 return response; 1330 return response;
@@ -1425,22 +1414,39 @@ MHD_create_response_from_buffer_with_free_callback (size_t size,
1425 * @param crfc_cls an argument for @a crfc 1414 * @param crfc_cls an argument for @a crfc
1426 * @return NULL on error (i.e. invalid arguments, out of memory) 1415 * @return NULL on error (i.e. invalid arguments, out of memory)
1427 * @note Available since #MHD_VERSION 0x00097302 1416 * @note Available since #MHD_VERSION 0x00097302
1417 * @note 'const' qualifier is used for @a buffer since #MHD_VERSION 0x00097504
1428 * @ingroup response 1418 * @ingroup response
1429 */ 1419 */
1430_MHD_EXTERN struct MHD_Response * 1420_MHD_EXTERN struct MHD_Response *
1431MHD_create_response_from_buffer_with_free_callback_cls (size_t size, 1421MHD_create_response_from_buffer_with_free_callback_cls (size_t size,
1432 void *buffer, 1422 const void *buffer,
1433 MHD_ContentReaderFreeCallback 1423 MHD_ContentReaderFreeCallback
1434 crfc, 1424 crfc,
1435 void *crfc_cls) 1425 void *crfc_cls)
1436{ 1426{
1437 struct MHD_Response *r; 1427 struct MHD_Response *r;
1438 1428
1439 r = MHD_create_response_from_buffer_with_free_callback (size, 1429 if ((NULL == buffer) && (size > 0))
1440 buffer, 1430 return NULL;
1441 crfc); 1431 if (MHD_SIZE_UNKNOWN == size)
1442 if (NULL != r) 1432 return NULL;
1443 r->crc_cls = crfc_cls; 1433 r = MHD_calloc_ (1, sizeof (struct MHD_Response));
1434 if (NULL == r)
1435 return NULL;
1436#if defined(MHD_USE_THREADS)
1437 if (! MHD_mutex_init_ (&r->mutex))
1438 {
1439 free (r);
1440 return NULL;
1441 }
1442#endif
1443 r->fd = -1;
1444 r->reference_count = 1;
1445 r->total_size = size;
1446 r->data = buffer;
1447 r->data_size = size;
1448 r->crfc = crfc;
1449 r->crc_cls = crfc_cls;
1444 return r; 1450 return r;
1445} 1451}
1446 1452