refactor(http): use sock_readall

This commit is contained in:
2025-09-16 22:48:27 +02:00
parent 883c64a012
commit f406e1f648
2 changed files with 12 additions and 29 deletions

View File

@@ -195,6 +195,7 @@ cws_http_s *cws_http_parse(string_s *request_str) {
} }
CWS_LOG_DEBUG("location: %s", pch); CWS_LOG_DEBUG("location: %s", pch);
string_append(request->location, pch); string_append(request->location, pch);
// TODO: fix www
request->location_path = string_format("%s/%s", "www", request->location->data); request->location_path = string_format("%s/%s", "www", request->location->data);
/* Adjust location path */ /* Adjust location path */

View File

@@ -1,6 +1,5 @@
#include "server/worker.h" #include "server/worker.h"
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -34,35 +33,14 @@ static int cws_worker_setup_epoll(cws_worker_s *worker) {
return 0; return 0;
} }
static size_t cws_read_data(int sockfd, string_s *str) { static int cws_read_data(int sockfd, string_s *str) {
size_t total_bytes = 0;
ssize_t bytes_read;
char tmp[4096]; char tmp[4096];
memset(tmp, 0, sizeof(tmp)); memset(tmp, 0, sizeof(tmp));
while (1) { int bytes = sock_readall(sockfd, tmp, sizeof(tmp));
bytes_read = recv(sockfd, tmp, sizeof(tmp), 0); string_append(str, tmp);
if (bytes_read == -1) { return bytes;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
} else if (errno == ECONNRESET) {
return 0;
}
CWS_LOG_ERROR("recv(): %s", strerror(errno));
return -1;
} else if (bytes_read == 0) {
return -1;
}
total_bytes += bytes_read;
string_append(str, tmp);
}
return total_bytes;
} }
cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) { cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) {
@@ -168,7 +146,7 @@ cws_server_ret cws_epoll_add(int epfd, int sockfd) {
const int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event); const int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
if (status != 0) { if (status != 0) {
CWS_LOG_ERROR("epoll_ctl_add(): %s", strerror(errno)); CWS_LOG_ERROR("epoll_ctl_add()");
return CWS_SERVER_EPOLL_ADD_ERROR; return CWS_SERVER_EPOLL_ADD_ERROR;
} }
@@ -179,7 +157,7 @@ cws_server_ret cws_epoll_del(int epfd, int sockfd) {
const int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL); const int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
if (status != 0) { if (status != 0) {
CWS_LOG_ERROR("epoll_ctl_del(): %s", strerror(errno)); CWS_LOG_ERROR("epoll_ctl_del()");
return CWS_SERVER_EPOLL_DEL_ERROR; return CWS_SERVER_EPOLL_DEL_ERROR;
} }
@@ -188,6 +166,7 @@ cws_server_ret cws_epoll_del(int epfd, int sockfd) {
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) { cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
string_s *data = string_new("", 4096); string_s *data = string_new("", 4096);
size_t total_bytes = cws_read_data(client_fd, data); size_t total_bytes = cws_read_data(client_fd, data);
if (total_bytes <= 0) { if (total_bytes <= 0) {
if (data) { if (data) {
@@ -198,10 +177,13 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR; return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
} }
/* TODO: set sockfd to request */
cws_http_s *request = cws_http_parse(data); cws_http_s *request = cws_http_parse(data);
request->sockfd = client_fd;
string_free(data); string_free(data);
// TODO: fix response
if (request == NULL) { if (request == NULL) {
cws_server_close_client(epfd, client_fd); cws_server_close_client(epfd, client_fd);