diff options
Diffstat (limited to 'src/microhttpd/response.c')
-rw-r--r-- | src/microhttpd/response.c | 100 |
1 files changed, 56 insertions, 44 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 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libmicrohttpd | 2 | This file is part of libmicrohttpd |
3 | Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff | 3 | Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff |
4 | Copyright (C) 2015-2021 Evgeny Grin (Karlson2k) | 4 | Copyright (C) 2015-2022 Evgeny Grin (Karlson2k) |
5 | 5 | ||
6 | This library is free software; you can redistribute it and/or | 6 | This library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public | 7 | modify it under the terms of the GNU Lesser General Public |
@@ -1296,38 +1296,16 @@ MHD_create_response_from_data (size_t size, | |||
1296 | int must_free, | 1296 | int must_free, |
1297 | int must_copy) | 1297 | int must_copy) |
1298 | { | 1298 | { |
1299 | struct MHD_Response *response; | 1299 | enum MHD_ResponseMemoryMode mode; |
1300 | void *mhd_copy; | ||
1301 | 1300 | ||
1302 | if ((must_copy) && (size > 0)) | 1301 | if (0 != must_copy) |
1303 | { | 1302 | mode = MHD_RESPMEM_MUST_COPY; |
1304 | if (NULL == data) | 1303 | else if (0 != must_free) |
1305 | return NULL; | 1304 | mode = MHD_RESPMEM_MUST_FREE; |
1306 | mhd_copy = malloc (size); | ||
1307 | if (NULL == mhd_copy) | ||
1308 | return NULL; | ||
1309 | memcpy (mhd_copy, data, size); | ||
1310 | must_free = MHD_YES; | ||
1311 | data = mhd_copy; | ||
1312 | } | ||
1313 | else | 1305 | else |
1314 | mhd_copy = NULL; | 1306 | mode = MHD_RESPMEM_PERSISTENT; |
1315 | 1307 | ||
1316 | response = | 1308 | return MHD_create_response_from_buffer (size, data, mode); |
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) | ||
1323 | { | ||
1324 | if (NULL != mhd_copy) | ||
1325 | free (mhd_copy); | ||
1326 | return NULL; | ||
1327 | } | ||
1328 | if (must_copy) | ||
1329 | response->data_buffer_size = size; | ||
1330 | return response; | ||
1331 | } | 1309 | } |
1332 | 1310 | ||
1333 | 1311 | ||
@@ -1353,10 +1331,50 @@ MHD_create_response_from_buffer (size_t size, | |||
1353 | void *buffer, | 1331 | void *buffer, |
1354 | enum MHD_ResponseMemoryMode mode) | 1332 | enum MHD_ResponseMemoryMode mode) |
1355 | { | 1333 | { |
1356 | return MHD_create_response_from_data (size, | 1334 | struct MHD_Response *r; |
1357 | buffer, | 1335 | void *mhd_copy; |
1358 | mode == MHD_RESPMEM_MUST_FREE, | 1336 | MHD_ContentReaderFreeCallback crfc; |
1359 | mode == MHD_RESPMEM_MUST_COPY); | 1337 | void *crfc_cls; |
1338 | |||
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) | ||
1376 | r->data_buffer_size = size; | ||
1377 | return r; | ||
1360 | } | 1378 | } |
1361 | 1379 | ||
1362 | 1380 | ||
@@ -1383,16 +1401,10 @@ MHD_create_response_from_buffer_with_free_callback (size_t size, | |||
1383 | MHD_ContentReaderFreeCallback | 1401 | MHD_ContentReaderFreeCallback |
1384 | crfc) | 1402 | crfc) |
1385 | { | 1403 | { |
1386 | struct MHD_Response *r; | 1404 | return MHD_create_response_from_buffer_with_free_callback_cls (size, |
1387 | 1405 | buffer, | |
1388 | r = MHD_create_response_from_data (size, | 1406 | crfc, |
1389 | buffer, | 1407 | buffer); |
1390 | MHD_YES, | ||
1391 | MHD_NO); | ||
1392 | if (NULL == r) | ||
1393 | return r; | ||
1394 | r->crfc = crfc; | ||
1395 | return r; | ||
1396 | } | 1408 | } |
1397 | 1409 | ||
1398 | 1410 | ||