From cca532c564b8cf561fa57e8c3e9ec70f05498e49 Mon Sep 17 00:00:00 2001 From: Mikhail Burakov Date: Fri, 6 Jan 2023 21:16:39 +0100 Subject: Allocate body chunks on heap --- http_utils.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/http_utils.c b/http_utils.c index 149760e..d972b84 100644 --- a/http_utils.c +++ b/http_utils.c @@ -18,6 +18,8 @@ #include "http_utils.h" #include +#include +#include #include #include "toolbox/utils.h" @@ -75,8 +77,20 @@ bool SendHttpReply(int fd, const char* status, const char* content_type, content_length += chunks[i].iov_len; } + size_t iov_count = 5 + chunks_count; + if (content_length && content_type) iov_count += 2; + if (iov_count > INT_MAX) { + LOGW("Too many body chunks"); + return false; + } + + struct iovec* iov = malloc(iov_count * sizeof(struct iovec)); + if (!iov) { + LOGW("Failed to copy chunks (%s)", strerror(errno)); + return false; + } + char buffer[24]; - struct iovec iov[7 + chunks_count]; struct iovec* ptr = iov; AppendIov(&ptr, part1, sizeof(part1)); AppendIov(&ptr, status, strlen(status)); @@ -87,9 +101,8 @@ bool SendHttpReply(int fd, const char* status, const char* content_type, AppendIov(&ptr, content_type, strlen(content_type)); } AppendIov(&ptr, part4, sizeof(part4)); - for (size_t i = 0; i < chunks_count; i++) { - *ptr++ = *chunks++; - } - - return SendAll(fd, iov, (int)(ptr - iov)); + memcpy(ptr, chunks, chunks_count * sizeof(struct iovec)); + bool result = SendAll(fd, iov, (int)iov_count); + free(iov); + return result; } -- cgit v1.2.3