diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-04-17 19:58:42 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-04-18 11:18:02 +0300 |
commit | 4ff1024763a306200de3fd1e5581377aca9047e1 (patch) | |
tree | ae7945c56b3e51e25033d1749913b1ef847b4224 | |
parent | 03c29b63e0e4c4036dafaa382cd0db1fe400a7a2 (diff) | |
download | libmicrohttpd-4ff1024763a306200de3fd1e5581377aca9047e1.tar.gz libmicrohttpd-4ff1024763a306200de3fd1e5581377aca9047e1.zip |
refactoring: use 'const' for response buffers
The response buffers shouldn't be modifiable.
-rw-r--r-- | src/include/microhttpd.h | 5 | ||||
-rw-r--r-- | src/microhttpd/internal.h | 2 | ||||
-rw-r--r-- | src/microhttpd/response.c | 80 |
3 files changed, 47 insertions, 40 deletions
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h index e0351159..45e3524e 100644 --- a/src/include/microhttpd.h +++ b/src/include/microhttpd.h | |||
@@ -96,7 +96,7 @@ extern "C" | |||
96 | * they are parsed as decimal numbers. | 96 | * they are parsed as decimal numbers. |
97 | * Example: 0x01093001 = 1.9.30-1. | 97 | * Example: 0x01093001 = 1.9.30-1. |
98 | */ | 98 | */ |
99 | #define MHD_VERSION 0x00097503 | 99 | #define MHD_VERSION 0x00097504 |
100 | 100 | ||
101 | /* If generic headers don't work on your platform, include headers | 101 | /* If generic headers don't work on your platform, include headers |
102 | which define 'va_list', 'size_t', 'ssize_t', 'intptr_t', | 102 | which define 'va_list', 'size_t', 'ssize_t', 'intptr_t', |
@@ -3582,11 +3582,12 @@ MHD_create_response_from_buffer_with_free_callback (size_t size, | |||
3582 | * @param crfc_cls an argument for @a crfc | 3582 | * @param crfc_cls an argument for @a crfc |
3583 | * @return NULL on error (i.e. invalid arguments, out of memory) | 3583 | * @return NULL on error (i.e. invalid arguments, out of memory) |
3584 | * @note Available since #MHD_VERSION 0x00097302 | 3584 | * @note Available since #MHD_VERSION 0x00097302 |
3585 | * @note 'const' qualifier is used for @a buffer since #MHD_VERSION 0x00097504 | ||
3585 | * @ingroup response | 3586 | * @ingroup response |
3586 | */ | 3587 | */ |
3587 | _MHD_EXTERN struct MHD_Response * | 3588 | _MHD_EXTERN struct MHD_Response * |
3588 | MHD_create_response_from_buffer_with_free_callback_cls (size_t size, | 3589 | MHD_create_response_from_buffer_with_free_callback_cls (size_t size, |
3589 | void *buffer, | 3590 | const void *buffer, |
3590 | MHD_ContentReaderFreeCallback | 3591 | MHD_ContentReaderFreeCallback |
3591 | crfc, | 3592 | crfc, |
3592 | void *crfc_cls); | 3593 | void *crfc_cls); |
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h index 50c1a1ec..95d301f9 100644 --- a/src/microhttpd/internal.h +++ b/src/microhttpd/internal.h | |||
@@ -449,7 +449,7 @@ struct MHD_Response | |||
449 | * Buffer pointing to data that we are supposed | 449 | * Buffer pointing to data that we are supposed |
450 | * to send as a response. | 450 | * to send as a response. |
451 | */ | 451 | */ |
452 | char *data; | 452 | const char *data; |
453 | 453 | ||
454 | /** | 454 | /** |
455 | * Closure to give to the content reader @e crc | 455 | * Closure to give to the content reader @e crc |
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 * |
1431 | MHD_create_response_from_buffer_with_free_callback_cls (size_t size, | 1421 | MHD_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 | ||