aboutsummaryrefslogtreecommitdiff
path: root/src/routing.c
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-08-15 18:06:44 -0400
committerfrosty <gabriel@bwaaa.monster>2026-08-15 18:06:44 -0400
commitbc42e791cf4b56f2854e87666e3295d8b167ad24 (patch)
treefc371a9bc1268fe6d1b43f62e66a44a2546216fa /src/routing.c
parentd3c9f9384fa3e743f48e1771a8519e3e4bb04ad6 (diff)
downloadbeaker-bc42e791cf4b56f2854e87666e3295d8b167ad24.tar.gz
fix: harden socket I/O
Diffstat (limited to 'src/routing.c')
-rw-r--r--src/routing.c20
1 files changed, 8 insertions, 12 deletions
diff --git a/src/routing.c b/src/routing.c
index 9894768..d5f8285 100644
--- a/src/routing.c
+++ b/src/routing.c
@@ -315,7 +315,8 @@ bool serve_static_file_with_mime(const char *request_path_relative_to_static,
"\r\n",
mime_type, file_size);
- if (send(current_client_socket, http_header, strlen(http_header), 0) < 0) {
+ if (beaker_send_all(current_client_socket, http_header, strlen(http_header)) <
+ 0) {
beaker_log_errno_format(
"ERROR",
"serve_static_file_with_mime: Failed to send header for '%s'.\n",
@@ -330,7 +331,7 @@ bool serve_static_file_with_mime(const char *request_path_relative_to_static,
while (!send_error && !feof(fp) && !ferror(fp) &&
(bytes_read = fread(file_buffer, 1, sizeof(file_buffer), fp)) > 0) {
- if (send(current_client_socket, file_buffer, bytes_read, 0) < 0) {
+ if (beaker_send_all(current_client_socket, file_buffer, bytes_read) < 0) {
beaker_log_errno_format(
"ERROR",
"serve_static_file_with_mime: Failed to send content for '%s'.\n",
@@ -380,20 +381,15 @@ bool serve_data(const char *data, size_t size, const char *mime_type) {
"\r\n",
mime_type, size);
- if (send(current_client_socket, http_header, strlen(http_header), 0) < 0) {
+ if (beaker_send_all(current_client_socket, http_header, strlen(http_header)) <
+ 0) {
beaker_log_errno_format("ERROR", "serve_data: Failed to send header.\n");
return false;
}
- size_t bytes_sent = 0;
- while (bytes_sent < size) {
- size_t chunk =
- (size - bytes_sent > BUFFER_SIZE) ? BUFFER_SIZE : (size - bytes_sent);
- if (send(current_client_socket, data + bytes_sent, chunk, 0) < 0) {
- beaker_log_errno_format("ERROR", "serve_data: Failed to send content.\n");
- return false;
- }
- bytes_sent += chunk;
+ if (beaker_send_all(current_client_socket, data, size) < 0) {
+ beaker_log_errno_format("ERROR", "serve_data: Failed to send content.\n");
+ return false;
}
return true;