refactor(socket/mime): change function sign

This commit is contained in:
2026-01-14 02:17:25 +01:00
parent 76d00654ed
commit 4796ea7640
4 changed files with 7 additions and 15 deletions

View File

@@ -4,8 +4,8 @@
#include <myclib/mystring.h> #include <myclib/mystring.h>
#include <sys/types.h> #include <sys/types.h>
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 #endif

View File

@@ -8,9 +8,7 @@ typedef struct mimetype {
const char *type; const char *type;
} mimetype; } mimetype;
/* /* Retrieve Content Type */
* Retrieve Content Type
*/
int cws_mime_get_ct(const char *location_path, char *content_type); int cws_mime_get_ct(const char *location_path, char *content_type);
#endif #endif

View File

@@ -3,21 +3,18 @@
#include <errno.h> #include <errno.h>
#include <sys/socket.h> #include <sys/socket.h>
size_t cws_socket_read(int sockfd, string_s *str) { ssize_t cws_socket_read(int sockfd, string_s *str) {
char tmp[4096] = {0}; 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 (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* No data available right now */
return 0; return 0;
} }
return -1; return -1;
} }
if (n == 0) { if (n == 0) {
/* Connection closed */
return -1; return -1;
} }
@@ -26,19 +23,17 @@ size_t cws_socket_read(int sockfd, string_s *str) {
return n; 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 total_sent = 0;
size_t n; ssize_t n;
while (total_sent < len) { while (total_sent < len) {
n = send(sockfd, buffer + total_sent, len - total_sent, flags); n = send(sockfd, buffer + total_sent, len - total_sent, flags);
if (n < 0) { if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* Buffer full, return bytes sent */
break; break;
} }
return -1; return -1;
} }

View File

@@ -10,7 +10,6 @@ static mimetype mimetypes[] = {{"html", "text/html"}, {"css", "text/css"}, {"js"
{"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}}; {"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}};
int cws_mime_get_ct(const char *location_path, char *content_type) { int cws_mime_get_ct(const char *location_path, char *content_type) {
/* Find last occurrence of a string */
char *ptr = strrchr(location_path, '.'); char *ptr = strrchr(location_path, '.');
if (ptr == NULL) { if (ptr == NULL) {
return CWS_CONTENT_TYPE_ERROR; return CWS_CONTENT_TYPE_ERROR;