libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit b55c4031d49904148139660dea231f37f2afc878
parent 2275296815b6f90f4b9fc313cb5c46879aae434d
Author: Christian Grothoff <christian@grothoff.org>
Date:   Fri, 26 Feb 2021 21:23:11 +0100

-misc style fixes, no semantic changes

Diffstat:
Mdoc/libmicrohttpd.texi | 2+-
Msrc/include/microhttpd.h | 6+++---
Msrc/microhttpd/connection.c | 13+++++++++----
Msrc/microhttpd/internal.h | 92++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Msrc/microhttpd/response.c | 217++++++++++++++++++++++++++++++++++++++-----------------------------------------
5 files changed, 169 insertions(+), 161 deletions(-)

diff --git a/doc/libmicrohttpd.texi b/doc/libmicrohttpd.texi @@ -2181,7 +2181,7 @@ The response object can be extended with header information and then be used any number of times. @table @var @item iov -the array for response data buffers, an internal copy of this will be made; +the array for response data buffers, an internal copy of this will be made; however, note that the data pointed to by the @var{iov} is not copied and must be preserved unchanged at the given locations until the response is no longer in use and the @var{crfc} is called; @item iovcnt the number of elements in @var{iov}; diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h @@ -1974,14 +1974,14 @@ union MHD_ConnectionInfo /** - * I/O vector type. Provided for use with MHD_create_response_from_iovec. + * I/O vector type. Provided for use with #MHD_create_response_from_iovec(). */ struct MHD_IoVec { /** * The pointer to the memory region for I/O. */ - void *iov_base; + const void *iov_base; /** * The size in bytes of the memory region for I/O. @@ -3262,7 +3262,7 @@ MHD_create_response_from_fd_at_offset64 (uint64_t size, */ _MHD_EXTERN struct MHD_Response * MHD_create_response_from_iovec (const struct MHD_IoVec *iov, - int iovcnt, + unsigned int iovcnt, MHD_ContentReaderFreeCallback free_cb, void *cls); diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -800,11 +800,12 @@ try_ready_normal_body (struct MHD_Connection *connection) if (NULL != response->data_iov) { size_t copy_size; + if (NULL != connection->resp_iov.iov) return MHD_YES; - copy_size = response->data_iovcnt * sizeof(MHD_iovec_); - connection->resp_iov.iov = MHD_pool_allocate (connection->pool, copy_size, + connection->resp_iov.iov = MHD_pool_allocate (connection->pool, + copy_size, true); if (NULL == connection->resp_iov.iov) { @@ -814,7 +815,9 @@ try_ready_normal_body (struct MHD_Connection *connection) _ ("Closing connection (out of memory).\n")); return MHD_NO; } - memcpy (connection->resp_iov.iov, response->data_iov, copy_size); + memcpy (connection->resp_iov.iov, + response->data_iov, + copy_size); connection->resp_iov.cnt = response->data_iovcnt; connection->resp_iov.sent = 0; return MHD_YES; @@ -3047,7 +3050,9 @@ MHD_connection_handle_write (struct MHD_Connection *connection) #endif /* _MHD_HAVE_SENDFILE */ if (NULL != response->data_iov) { - ret = MHD_send_iovec_ (connection, &connection->resp_iov, true); + ret = MHD_send_iovec_ (connection, + &connection->resp_iov, + true); } else { diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -92,8 +92,9 @@ * @param fd the FD to close */ #define MHD_fd_close_chk_(fd) do { \ - if ( (0 != close ((fd)) && (EBADF == errno)) ) \ - MHD_PANIC (_ ("Failed to close FD.\n")); \ + if ( (0 != close ((fd)) && (EBADF == errno)) ) { \ + MHD_PANIC (_ ("Failed to close FD.\n")); \ + } \ } while (0) /* @@ -509,7 +510,7 @@ struct MHD_Response /** * Number of elements in data_iov. */ - size_t data_iovcnt; + unsigned int data_iovcnt; }; @@ -1933,12 +1934,13 @@ struct MHD_Daemon #define DLL_insert(head,tail,element) do { \ mhd_assert (NULL == (element)->next); \ mhd_assert (NULL == (element)->prev); \ - (element)->next = (head); \ - (element)->prev = NULL; \ - if ((tail) == NULL) \ - (tail) = element; \ - else \ - (head)->prev = element; \ + (element)->next = (head); \ + (element)->prev = NULL; \ + if ((tail) == NULL) { \ + (tail) = element; \ + } else { \ + (head)->prev = element; \ + } \ (head) = (element); } while (0) @@ -1954,15 +1956,17 @@ struct MHD_Daemon #define DLL_remove(head,tail,element) do { \ mhd_assert ( (NULL != (element)->next) || ((element) == (tail))); \ mhd_assert ( (NULL != (element)->prev) || ((element) == (head))); \ - if ((element)->prev == NULL) \ - (head) = (element)->next; \ - else \ + if ((element)->prev == NULL) { \ + (head) = (element)->next; \ + } else { \ (element)->prev->next = (element)->next; \ - if ((element)->next == NULL) \ - (tail) = (element)->prev; \ - else \ + } \ + if ((element)->next == NULL) { \ + (tail) = (element)->prev; \ + } else { \ (element)->next->prev = (element)->prev; \ - (element)->next = NULL; \ + } \ + (element)->next = NULL; \ (element)->prev = NULL; } while (0) @@ -1977,12 +1981,13 @@ struct MHD_Daemon #define XDLL_insert(head,tail,element) do { \ mhd_assert (NULL == (element)->nextX); \ mhd_assert (NULL == (element)->prevX); \ - (element)->nextX = (head); \ - (element)->prevX = NULL; \ - if (NULL == (tail)) \ - (tail) = element; \ - else \ - (head)->prevX = element; \ + (element)->nextX = (head); \ + (element)->prevX = NULL; \ + if (NULL == (tail)) { \ + (tail) = element; \ + } else { \ + (head)->prevX = element; \ + } \ (head) = (element); } while (0) @@ -1998,15 +2003,17 @@ struct MHD_Daemon #define XDLL_remove(head,tail,element) do { \ mhd_assert ( (NULL != (element)->nextX) || ((element) == (tail))); \ mhd_assert ( (NULL != (element)->prevX) || ((element) == (head))); \ - if (NULL == (element)->prevX) \ - (head) = (element)->nextX; \ - else \ + if (NULL == (element)->prevX) { \ + (head) = (element)->nextX; \ + } else { \ (element)->prevX->nextX = (element)->nextX; \ - if (NULL == (element)->nextX) \ - (tail) = (element)->prevX; \ - else \ + } \ + if (NULL == (element)->nextX) { \ + (tail) = (element)->prevX; \ + } else { \ (element)->nextX->prevX = (element)->prevX; \ - (element)->nextX = NULL; \ + } \ + (element)->nextX = NULL; \ (element)->prevX = NULL; } while (0) @@ -2020,11 +2027,12 @@ struct MHD_Daemon */ #define EDLL_insert(head,tail,element) do { \ (element)->nextE = (head); \ - (element)->prevE = NULL; \ - if ((tail) == NULL) \ - (tail) = element; \ - else \ + (element)->prevE = NULL; \ + if ((tail) == NULL) { \ + (tail) = element; \ + } else { \ (head)->prevE = element; \ + } \ (head) = (element); } while (0) @@ -2037,16 +2045,18 @@ struct MHD_Daemon * @param tail pointer to the tail of the EDLL * @param element element to remove */ -#define EDLL_remove(head,tail,element) do { \ - if ((element)->prevE == NULL) \ - (head) = (element)->nextE; \ - else \ +#define EDLL_remove(head,tail,element) do { \ + if ((element)->prevE == NULL) { \ + (head) = (element)->nextE; \ + } else { \ (element)->prevE->nextE = (element)->nextE; \ - if ((element)->nextE == NULL) \ - (tail) = (element)->prevE; \ - else \ + } \ + if ((element)->nextE == NULL) { \ + (tail) = (element)->prevE; \ + } else { \ (element)->nextE->prevE = (element)->prevE; \ - (element)->nextE = NULL; \ + } \ + (element)->nextE = NULL; \ (element)->prevE = NULL; } while (0) diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -1,6 +1,6 @@ /* This file is part of libmicrohttpd - Copyright (C) 2007, 2009, 2010, 2016, 2017 Daniel Pittman and Christian Grothoff + Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -861,135 +861,128 @@ MHD_create_response_from_buffer_with_free_callback (size_t size, */ _MHD_EXTERN struct MHD_Response * MHD_create_response_from_iovec (const struct MHD_IoVec *iov, - int iovcnt, + unsigned int iovcnt, MHD_ContentReaderFreeCallback free_cb, void *cls) { struct MHD_Response *response; + unsigned int i; + int i_cp = 0; /**< Index in the copy of iov */ + uint64_t total_size = 0; + const void *last_valid_buffer = NULL; if ((NULL == iov) && (0 < iovcnt)) return NULL; response = MHD_calloc_ (1, sizeof (struct MHD_Response)); - if (NULL != response) + if (NULL == response) + return NULL; + if (! MHD_mutex_init_ (&response->mutex)) + { + free (response); + return NULL; + } + /* Calculate final size, number of valid elements, and check 'iov' */ + for (i = 0; i < iovcnt; ++i) { - if (MHD_mutex_init_ (&response->mutex)) + if (0 == iov[i].iov_len) + continue; /* skip zero-sized elements */ + if (NULL == iov[i].iov_base) { - int i; - int i_cp; /**< Index in the copy of iov */ - uint64_t total_size; - void *last_valid_buffer; - - i_cp = 0; - total_size = 0; - last_valid_buffer = NULL; - /* Calculate final size, number of valid elements, and check 'iov' */ - for (i = 0; iovcnt > i; ++i) - { -#if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) - int64_t i_add; -#endif /* ! MHD_WINSOCK_SOCKETS && _WIN64 */ - if (0 == iov[i].iov_len) - continue; /* skip zero-sized elements */ - - if (NULL == iov[i].iov_base) - { - i_cp = -1; /* error */ - break; - } - if ( (total_size > (total_size + iov[i].iov_len)) || - (INT_MAX == i_cp) || - (SSIZE_MAX < iov[i].iov_len) ) - { - i_cp = -1; /* overflow */ - break; - } - last_valid_buffer = iov[i].iov_base; - total_size += iov[i].iov_len; + i_cp = -1; /* error */ + break; + } + if ( (total_size > (total_size + iov[i].iov_len)) || + (INT_MAX == i_cp) || + (SSIZE_MAX < iov[i].iov_len) ) + { + i_cp = -1; /* overflow */ + break; + } + last_valid_buffer = iov[i].iov_base; + total_size += iov[i].iov_len; #if defined(MHD_POSIX_SOCKETS) || ! defined(_WIN64) - i_cp++; + i_cp++; #else /* ! MHD_POSIX_SOCKETS && _WIN64 */ - i_add = iov[i].iov_len / ULONG_MAX; - if (0 != iov[i].iov_len % ULONG_MAX) - i_add++; - if (INT_MAX < (i_add + i_cp)) - { - i_cp = -1; /* overflow */ - break; - } - i_cp += (int) i_add; -#endif /* ! MHD_POSIX_SOCKETS && _WIN64 */ - } - if (0 <= i_cp) + { + int64_t i_add; + + i_add = iov[i].iov_len / ULONG_MAX; + if (0 != iov[i].iov_len % ULONG_MAX) + i_add++; + if (INT_MAX < (i_add + i_cp)) { - response->fd = -1; - response->reference_count = 1; - response->total_size = total_size; - response->crc_cls = cls; - response->crfc = free_cb; - if (1 < i_cp) - { - MHD_iovec_ *iov_copy; - int num_copy_elements = i_cp; - - iov_copy = MHD_calloc_ (num_copy_elements, sizeof(MHD_iovec_)); - if (NULL != iov_copy) - { - i_cp = 0; - for (i = 0; iovcnt > i; ++i) - { - size_t element_size; - uint8_t *buf; - - if (0 == iov[i].iov_len) - continue; /* skip zero-sized elements */ - - buf = (uint8_t*) iov[i].iov_base; - element_size = iov[i].iov_len; -#if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) - while (ULONG_MAX < element_size) - { - iov_copy[i_cp].iov_base = (void*) buf; - iov_copy[i_cp].iov_len = ULONG_MAX; - buf += ULONG_MAX; - element_size -= ULONG_MAX; - i_cp++; - } -#endif /* MHD_WINSOCK_SOCKETS && _WIN64 */ - iov_copy[i_cp].iov_base = (void*) buf; - iov_copy[i_cp].iov_len = element_size; - i_cp++; - } - - mhd_assert (num_copy_elements == i_cp); - response->data_iov = iov_copy; - response->data_iovcnt = i_cp; - - return response; - } - - } - else if (1 == i_cp) - { - mhd_assert (NULL != last_valid_buffer); - response->data = last_valid_buffer; - response->data_size = total_size; - - return response; - } - else /* if (0 == i_nz) */ - { - mhd_assert (0 == total_size); - - return response; - } + i_cp = -1; /* overflow */ + break; } - /* Some error condition */ - MHD_mutex_destroy_chk_ (&response->mutex); + i_cp += (int) i_add; } +#endif /* ! MHD_POSIX_SOCKETS && _WIN64 */ + } + if (-1 == i_cp) + { + /* Some error condition */ + MHD_mutex_destroy_chk_ (&response->mutex); free (response); + return NULL; + } + response->fd = -1; + response->reference_count = 1; + response->total_size = total_size; + response->crc_cls = cls; + response->crfc = free_cb; + if (0 == i_cp) + { + mhd_assert (0 == total_size); + return response; + } + if (1 == i_cp) + { + mhd_assert (NULL != last_valid_buffer); + response->data = (void *) last_valid_buffer; + response->data_size = total_size; + return response; + } + mhd_assert (1 < i_cp); + { + MHD_iovec_ *iov_copy; + int num_copy_elements = i_cp; + + iov_copy = MHD_calloc_ (num_copy_elements, + sizeof(MHD_iovec_)); + if (NULL == iov_copy) + { + MHD_mutex_destroy_chk_ (&response->mutex); + free (response); + return NULL; + } + i_cp = 0; + for (i = 0; i < iovcnt; ++i) + { + size_t element_size = iov[i].iov_len; + const void *buf = iov[i].iov_base; + + if (0 == element_size) + continue; /* skip zero-sized elements */ +#if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) + while (ULONG_MAX < element_size) + { + iov_copy[i_cp].iov_base = buf; + iov_copy[i_cp].iov_len = ULONG_MAX; + buf += ULONG_MAX; + element_size -= ULONG_MAX; + i_cp++; + } +#endif /* MHD_WINSOCK_SOCKETS && _WIN64 */ + iov_copy[i_cp].iov_base = (void *) buf; + iov_copy[i_cp].iov_len = element_size; + i_cp++; + } + mhd_assert (num_copy_elements == i_cp); + response->data_iov = iov_copy; + response->data_iovcnt = i_cp; + return response; } - return NULL; }