refactor(http): improve http_parse()

This commit is contained in:
2025-10-25 01:32:15 +02:00
parent 13b2c93583
commit 8ae9219d7a
3 changed files with 77 additions and 87 deletions

View File

@@ -24,13 +24,13 @@
#endif #endif
#ifdef EVELOPER #ifdef EVELOPER
#define CWS_LOG_DEBUG(msg, ...) \ #define CWS_LOG_DEBUG(msg, ...) \
fprintf(stdout, _DEBUG " [%s:%d] " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__) fprintf(stdout, _DEBUG " [%s:%d] " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else #else
#define CWS_LOG_DEBUG(msg, ...) #define CWS_LOG_DEBUG(msg, ...)
#endif #endif
#define CWS_LOG_ERROR(msg, ...) \ #define CWS_LOG_ERROR(msg, ...) \
fprintf(stderr, _ERR " [%s:%d] " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__) fprintf(stderr, _ERR " [%s:%d] " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define CWS_LOG_WARNING(msg, ...) fprintf(stdout, _WARNING " " msg "\n", ##__VA_ARGS__) #define CWS_LOG_WARNING(msg, ...) fprintf(stdout, _WARNING " " msg "\n", ##__VA_ARGS__)
#define CWS_LOG_INFO(msg, ...) fprintf(stdout, _INFO " " msg "\n", ##__VA_ARGS__) #define CWS_LOG_INFO(msg, ...) fprintf(stdout, _INFO " " msg "\n", ##__VA_ARGS__)

View File

@@ -1,16 +1,15 @@
#define _XOPEN_SOURCE 1
#include "http/http.h" #include "http/http.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <myclib/mysocket.h> #include <myclib/mystring.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "utils/debug.h" #include "utils/debug.h"
#include "utils/socket.h"
#include "utils/utils.h" #include "utils/utils.h"
static cws_http_s *http_new() { static cws_http_s *http_new() {
@@ -135,19 +134,8 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
size_t response_len = size_t response_len =
http_response_builder(&response, HTTP_OK, content_type, data, content_length); http_response_builder(&response, HTTP_OK, content_type, data, content_length);
size_t total_sent = 0; ssize_t sent = cws_send_data(request->sockfd, response, response_len, 0);
while (total_sent < response_len) { CWS_LOG_DEBUG("Sent %zd bytes", sent);
ssize_t sent =
send(request->sockfd, response + total_sent, response_len - total_sent, MSG_NOSIGNAL);
if (sent < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
continue;
}
break;
}
total_sent += sent;
}
CWS_LOG_DEBUG("Sent %zd bytes", total_sent);
free(response); free(response);
free(data); free(data);
@@ -178,7 +166,7 @@ static size_t http_simple_html(char **response, cws_http_status_e status, char *
} }
cws_http_s *cws_http_parse(string_s *request_str) { cws_http_s *cws_http_parse(string_s *request_str) {
if (!request_str) { if (!request_str || !request_str->data) {
return NULL; return NULL;
} }
@@ -187,43 +175,50 @@ cws_http_s *cws_http_parse(string_s *request_str) {
return NULL; return NULL;
} }
char *request_copy = strdup(request_str->data); char *str = strdup(request_str->data);
if (request_copy == NULL) { if (!str) {
cws_http_free(request); cws_http_free(request);
return NULL; return NULL;
} }
char *str_free = str;
char *saveptr = NULL;
char *pch = NULL;
/* Parse HTTP method */ /* Parse HTTP method */
pch = strtok_r(request_copy, " ", &saveptr); str += strspn(str, " ");
if (pch == NULL) { if (*str == '\0') {
free(str_free);
cws_http_free(request); cws_http_free(request);
free(request_copy);
return NULL; return NULL;
} }
CWS_LOG_DEBUG("method: %s", pch); size_t len = strcspn(str, " ");
if (str[len] == '\0') {
request->method = http_parse_method(pch); free(str_free);
if (request->method == HTTP_UNKNOWN) {
cws_http_free(request); cws_http_free(request);
free(request_copy);
return NULL; return NULL;
} }
str[len] = '\0';
CWS_LOG_DEBUG("method: %s", str);
request->method = http_parse_method(str);
str += len + 1;
/* Parse location */ /* Parse location */
pch = strtok_r(NULL, " ", &saveptr); str += strspn(str, " ");
if (pch == NULL) { if (*str == '\0') {
free(str_free);
cws_http_free(request); cws_http_free(request);
free(request_copy);
return NULL; return NULL;
} }
string_append(request->location, pch); len = strcspn(str, " ");
if (str[len] == '\0') {
free(str_free);
cws_http_free(request);
return NULL;
}
str[len] = '\0';
string_append(request->location, str);
str += len + 1;
/* Adjust location path */ /* Adjust location path */
string_append(request->location_path, "www"); string_append(request->location_path, "www");
if (strcmp(request->location->data, "/") == 0) { if (strcmp(request->location->data, "/") == 0) {
string_append(request->location_path, "/index.html"); string_append(request->location_path, "/index.html");
} else { } else {
@@ -232,51 +227,68 @@ cws_http_s *cws_http_parse(string_s *request_str) {
CWS_LOG_DEBUG("location path: %s", request->location_path->data); CWS_LOG_DEBUG("location path: %s", request->location_path->data);
/* Parse HTTP version */ /* Parse HTTP version */
pch = strtok_r(NULL, " \r\n", &saveptr); str += strspn(str, " \t");
if (pch == NULL) { if (*str == '\0') {
free(request_copy); free(str_free);
cws_http_free(request); cws_http_free(request);
return NULL; return NULL;
} }
CWS_LOG_DEBUG("version: %s", pch); len = strcspn(str, "\r\n");
string_append(request->http_version, pch); if (len == 0) {
free(str_free);
cws_http_free(request);
return NULL;
}
str[len] = '\0';
CWS_LOG_DEBUG("version: %s", str);
string_append(request->http_version, str);
str += len;
/* Parse headers until a \r\n */ /* Parse headers until a blank line (\r\n\r\n) */
request->headers = request->headers =
hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn,
sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX); sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
char *header_colon;
while (pch) { str += strspn(str, "\r\n");
/* Get header line */
pch = strtok_r(NULL, "\r\n", &saveptr); while (*str != '\0' && *str != '\r') {
if (pch == NULL) { char *line_end = strstr(str, "\r\n");
if (!line_end) {
break; break;
} }
/* Find ":" */ *line_end = '\0';
header_colon = strchr(pch, ':');
if (header_colon != NULL) { char *colon = strchr(str, ':');
*header_colon = '\0'; if (!colon) {
} else { str = line_end + 2;
break; continue;
} }
*colon = '\0';
char *header_key = str;
char *header_value = colon + 1;
header_value += strspn(header_value, " \t");
char hk[CWS_HTTP_HEADER_MAX]; char hk[CWS_HTTP_HEADER_MAX];
char hv[CWS_HTTP_HEADER_CONTENT_MAX]; char hv[CWS_HTTP_HEADER_CONTENT_MAX];
/* Header key */ strncpy(hk, header_key, sizeof(hk) - 1);
strncpy(hk, pch, sizeof(hk)); hk[sizeof(hk) - 1] = '\0';
/* Header value (starting from ": ") */
char *hvalue = header_colon + 2; strncpy(hv, header_value, sizeof(hv) - 1);
strncpy(hv, hvalue, sizeof(hv)); hv[sizeof(hv) - 1] = '\0';
// CWS_LOG_DEBUG("hkey: %s -> %s", hk, hv);
hm_set(request->headers, hk, hv); hm_set(request->headers, hk, hv);
/* Move to the next line */
str = line_end + 2;
} }
free(request_copy); free(str_free);
/* TODO: Parse body */ /* TODO: Parse body */
/* str is at the beginning of the body */
return request; return request;
} }
@@ -308,13 +320,11 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
snprintf(*response, header_len + 1, snprintf(*response, header_len + 1,
"HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n", "HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n",
status_code, content_type, body_len_bytes); status_code, content_type, body_len_bytes);
// CWS_LOG_DEBUG("response: %s", *response);
/* Only append body if we have it */ /* Only append body if we have it */
if (body && body_len_bytes > 0) { if (body && body_len_bytes > 0) {
memcpy(*response + header_len, body, body_len_bytes); memcpy(*response + header_len, body, body_len_bytes);
} }
// CWS_LOG_DEBUG("response: %s", *response);
(*response)[total_len] = '\0'; (*response)[total_len] = '\0';
@@ -331,14 +341,14 @@ void cws_http_send_response(cws_http_s *request, cws_http_status_e status) {
case HTTP_NOT_FOUND: { case HTTP_NOT_FOUND: {
size_t len = http_simple_html(&response, HTTP_NOT_FOUND, "404 Not Found", size_t len = http_simple_html(&response, HTTP_NOT_FOUND, "404 Not Found",
"Resource not found, 404."); "Resource not found, 404.");
sock_writeall(request->sockfd, response, len); cws_send_data(request->sockfd, response, len, 0);
break; break;
} }
case HTTP_NOT_IMPLEMENTED: { case HTTP_NOT_IMPLEMENTED: {
size_t len = http_simple_html(&response, HTTP_NOT_IMPLEMENTED, "501 Not Implemented", size_t len = http_simple_html(&response, HTTP_NOT_IMPLEMENTED, "501 Not Implemented",
"Method not implemented, 501."); "Method not implemented, 501.");
sock_writeall(request->sockfd, response, len); cws_send_data(request->sockfd, response, len, 0);
break; break;
} }

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>
@@ -10,6 +9,7 @@
#include "http/http.h" #include "http/http.h"
#include "server/epoll_utils.h" #include "server/epoll_utils.h"
#include "utils/socket.h"
#include "utils/utils.h" #include "utils/utils.h"
static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) { static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) {
@@ -21,26 +21,6 @@ static cws_server_ret cws_worker_setup_epoll(cws_worker_s *worker) {
return CWS_SERVER_OK; return CWS_SERVER_OK;
} }
static ssize_t cws_read_data(int sockfd, string_s *str) {
char tmp[4096] = {0};
ssize_t n = recv(sockfd, tmp, sizeof tmp, MSG_PEEK);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
return -1;
}
if (n == 0) {
/* Connection closed */
return -1;
}
string_append(str, tmp);
return n;
}
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) {
cws_worker_s **workers = malloc(workers_num * sizeof *workers); cws_worker_s **workers = malloc(workers_num * sizeof *workers);
if (workers == NULL) { if (workers == NULL) {