fix(http): response len

This commit is contained in:
2025-10-17 02:02:41 +02:00
parent 78ebc7b6d0
commit 8743617649
2 changed files with 10 additions and 6 deletions

View File

@@ -274,9 +274,9 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
char *status_code = http_status_string(status); char *status_code = http_status_string(status);
size_t header_len = http_header_len(status_code, content_type, body_len_bytes); size_t header_len = http_header_len(status_code, content_type, body_len_bytes);
size_t total_len = header_len + body_len_bytes + 1; size_t total_len = header_len + body_len_bytes;
*response = malloc(total_len); *response = malloc(total_len + 1);
if (*response == NULL) { if (*response == NULL) {
return 0; return 0;
} }
@@ -288,6 +288,8 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
memcpy(*response + header_len, body, body_len_bytes); memcpy(*response + header_len, body, body_len_bytes);
} }
(*response)[total_len] = '\0';
return total_len; return total_len;
} }

View File

@@ -38,6 +38,9 @@ static int cws_read_data(int sockfd, string_s *str) {
memset(tmp, 0, sizeof tmp); memset(tmp, 0, sizeof tmp);
int bytes = sock_readall(sockfd, tmp, sizeof(tmp)); int bytes = sock_readall(sockfd, tmp, sizeof(tmp));
if (bytes < 0) {
return -1;
}
string_append(str, tmp); string_append(str, tmp);
return bytes; return bytes;
@@ -139,7 +142,7 @@ void cws_server_close_client(int epfd, int client_fd) {
cws_server_ret cws_epoll_add(int epfd, int sockfd) { cws_server_ret cws_epoll_add(int epfd, int sockfd) {
struct epoll_event event; struct epoll_event event;
event.events = EPOLLIN | EPOLLET; event.events = EPOLLIN;
event.data.fd = sockfd; event.data.fd = sockfd;
const int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event); const int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
@@ -167,9 +170,7 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
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) { string_free(data);
string_free(data);
}
cws_server_close_client(epfd, client_fd); cws_server_close_client(epfd, client_fd);
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR; return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
@@ -187,6 +188,7 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
cws_http_send_response(request, HTTP_OK); cws_http_send_response(request, HTTP_OK);
cws_http_free(request); cws_http_free(request);
cws_server_close_client(epfd, client_fd);
return CWS_SERVER_OK; return CWS_SERVER_OK;
} }