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.c113
1 files changed, 70 insertions, 43 deletions
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index 8ddde763..47e28046 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -1331,50 +1331,19 @@ MHD_create_response_from_buffer (size_t size,
1331 void *buffer, 1331 void *buffer,
1332 enum MHD_ResponseMemoryMode mode) 1332 enum MHD_ResponseMemoryMode mode)
1333{ 1333{
1334 struct MHD_Response *r; 1334 if (MHD_RESPMEM_MUST_FREE == mode)
1335 void *mhd_copy; 1335 return MHD_create_response_from_buffer_with_free_callback_cls (size,
1336 MHD_ContentReaderFreeCallback crfc; 1336 buffer,
1337 void *crfc_cls; 1337 &free,
1338 1338 buffer);
1339 if ((MHD_RESPMEM_MUST_COPY == mode) && (size > 0))
1340 {
1341 if (NULL == buffer)
1342 return NULL;
1343 mhd_copy = malloc (size);
1344 if (NULL == mhd_copy)
1345 return NULL;
1346 crfc = &free;
1347 crfc_cls = mhd_copy;
1348 memcpy (mhd_copy, buffer, size);
1349 buffer = mhd_copy;
1350 }
1351 else if (MHD_RESPMEM_MUST_FREE == mode)
1352 {
1353 mhd_copy = NULL;
1354 crfc = &free;
1355 crfc_cls = buffer;
1356 }
1357 else
1358 {
1359 mhd_copy = NULL;
1360 crfc = NULL;
1361 crfc_cls = NULL;
1362 }
1363
1364 r = MHD_create_response_from_buffer_with_free_callback_cls (size,
1365 buffer,
1366 crfc,
1367 crfc_cls);
1368 if (NULL == r)
1369 {
1370 if (NULL != mhd_copy)
1371 free (mhd_copy);
1372 return NULL;
1373 }
1374 /* TODO: remove the next check, the buffer should not be modifiable */
1375 if (MHD_RESPMEM_MUST_COPY == mode) 1339 if (MHD_RESPMEM_MUST_COPY == mode)
1376 r->data_buffer_size = size; 1340 return MHD_create_response_from_buffer_copy (size,
1377 return r; 1341 buffer);
1342
1343 return MHD_create_response_from_buffer_with_free_callback_cls (size,
1344 buffer,
1345 NULL,
1346 NULL);
1378} 1347}
1379 1348
1380 1349
@@ -1411,6 +1380,64 @@ MHD_create_response_from_buffer_static (size_t size,
1411 1380
1412 1381
1413/** 1382/**
1383 * Create a response object with the content of provided statically allocated
1384 * buffer used as the response body.
1385 *
1386 * An internal copy of the buffer will be made automatically, so buffer have
1387 * to be valid only during the call of this function (as a typical example:
1388 * buffer is a local (non-static) array).
1389 *
1390 * The response object can be extended with header information and then
1391 * be used any number of times.
1392 *
1393 * If response object is used to answer HEAD request then the body
1394 * of the response is not used, while all headers (including automatic
1395 * headers) are used.
1396 *
1397 * @param size the size of the data in @a buffer, can be zero
1398 * @param buffer the buffer with the data for the response body, can be NULL
1399 * if @a size is zero
1400 * @return NULL on error (i.e. invalid arguments, out of memory)
1401 * @note Available since #MHD_VERSION 0x00097507
1402 * @ingroup response
1403 */
1404_MHD_EXTERN struct MHD_Response *
1405MHD_create_response_from_buffer_copy (size_t size,
1406 const void *buffer)
1407{
1408 struct MHD_Response *r;
1409 void *mhd_copy;
1410
1411 if (0 == size)
1412 return MHD_create_response_from_buffer_with_free_callback_cls (0,
1413 NULL,
1414 NULL,
1415 NULL);
1416 if (NULL == buffer)
1417 return NULL;
1418
1419 mhd_copy = malloc (size);
1420 if (NULL == mhd_copy)
1421 return NULL;
1422 memcpy (mhd_copy, buffer, size);
1423
1424 r = MHD_create_response_from_buffer_with_free_callback_cls (size,
1425 mhd_copy,
1426 &free,
1427 mhd_copy);
1428 if (NULL == r)
1429 free (mhd_copy);
1430 else
1431 {
1432 /* TODO: remove the next assignment, the buffer should not be modifiable */
1433 r->data_buffer_size = size;
1434 }
1435
1436 return r;
1437}
1438
1439
1440/**
1414 * Create a response object with the content of provided buffer used as 1441 * Create a response object with the content of provided buffer used as
1415 * the response body. 1442 * the response body.
1416 * 1443 *