From 4796ea7640f98e5ce9dc88ea2cc168b016aa755f Mon Sep 17 00:00:00 2001 From: Francesco Date: Wed, 14 Jan 2026 02:17:25 +0100 Subject: [PATCH] refactor(socket/mime): change function sign --- include/core/socket.h | 4 ++-- include/http/mime.h | 4 +--- src/core/socket.c | 13 ++++--------- src/http/mime.c | 1 - 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/include/core/socket.h b/include/core/socket.h index 3a30343..af7580d 100644 --- a/include/core/socket.h +++ b/include/core/socket.h @@ -4,8 +4,8 @@ #include #include -size_t cws_socket_read(int sockfd, string_s *str); +ssize_t cws_socket_read(int sockfd, string_s *str); -size_t cws_socket_send(int sockfd, char *buffer, size_t len, int flags); +ssize_t cws_socket_send(int sockfd, const char *buffer, size_t len, int flags); #endif diff --git a/include/http/mime.h b/include/http/mime.h index 0e0347d..20b626d 100644 --- a/include/http/mime.h +++ b/include/http/mime.h @@ -8,9 +8,7 @@ typedef struct mimetype { const char *type; } mimetype; -/* - * Retrieve Content Type - */ +/* Retrieve Content Type */ int cws_mime_get_ct(const char *location_path, char *content_type); #endif diff --git a/src/core/socket.c b/src/core/socket.c index 982d498..9244b82 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -3,21 +3,18 @@ #include #include -size_t cws_socket_read(int sockfd, string_s *str) { +ssize_t cws_socket_read(int sockfd, string_s *str) { char tmp[4096] = {0}; - size_t n = recv(sockfd, tmp, sizeof tmp, 0); + ssize_t n = recv(sockfd, tmp, sizeof tmp, 0); if (n < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - /* No data available right now */ return 0; } - return -1; } if (n == 0) { - /* Connection closed */ return -1; } @@ -26,19 +23,17 @@ size_t cws_socket_read(int sockfd, string_s *str) { return n; } -size_t cws_socket_send(int sockfd, char *buffer, size_t len, int flags) { +ssize_t cws_socket_send(int sockfd, const char *buffer, size_t len, int flags) { size_t total_sent = 0; - size_t n; + ssize_t n; while (total_sent < len) { n = send(sockfd, buffer + total_sent, len - total_sent, flags); if (n < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - /* Buffer full, return bytes sent */ break; } - return -1; } diff --git a/src/http/mime.c b/src/http/mime.c index 6544646..a9fbedd 100644 --- a/src/http/mime.c +++ b/src/http/mime.c @@ -10,7 +10,6 @@ static mimetype mimetypes[] = {{"html", "text/html"}, {"css", "text/css"}, {"js" {"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}}; int cws_mime_get_ct(const char *location_path, char *content_type) { - /* Find last occurrence of a string */ char *ptr = strrchr(location_path, '.'); if (ptr == NULL) { return CWS_CONTENT_TYPE_ERROR;