summaryrefslogtreecommitdiff
path: root/http_utils.c
diff options
context:
space:
mode:
authorMikhail Burakov <mburakov@mailbox.org>2023-01-06 21:16:39 +0100
committerMikhail Burakov <mburakov@mailbox.org>2023-01-06 21:16:39 +0100
commitcca532c564b8cf561fa57e8c3e9ec70f05498e49 (patch)
tree68cfb2541e88cf21df5f0fa91989b124213c7170 /http_utils.c
parent374eb4e9a31d5b8d6dabca65f8ffca43146cd627 (diff)
Allocate body chunks on heap
Diffstat (limited to 'http_utils.c')
-rw-r--r--http_utils.c25
1 files 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 <errno.h>
+#include <limits.h>
+#include <stdlib.h>
#include <string.h>
#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;
}