Skip to content

Commit

Permalink
Merge pull request #94 from thenetrunna/master
Browse files Browse the repository at this point in the history
sonic: fix exit bugs
  • Loading branch information
thenetrunna authored Apr 19, 2024
2 parents 6bf95c5 + e911ba7 commit b34e19d
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 357 deletions.
2 changes: 2 additions & 0 deletions netlibc/include/netlibc/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ char *escape_character(char *input, char character);

char *nstringify(char *str);

char *nstrdup(char *str);

#endif
6 changes: 6 additions & 0 deletions netlibc/src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ char *nstringify(char *str) {
return new_str;
}

char *nstrdup(char *str) {
char *duped = strdup(str);

return nstringify(duped);
}

char *escape_character(char *input, char character) {
if (input == NULL) {
return NULL; // Return NULL if the input string is not valid.
Expand Down
12 changes: 6 additions & 6 deletions sonic/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "../sonic.h"

char *utils_status_code_to_string(sonic_status_t status_code);
result_t utils_status_code_to_string(sonic_status_t status_code);

char *utils_get_header_value(sonic_header_t *headers, u64 headers_count,
char *key);
Expand All @@ -36,7 +36,7 @@ void list_files_recursive(files_list_t *result, char *dir_path);

char *concat_path(char *path1, char *path2);

sonic_method_t str_to_http_method(const char *method_str);
result_t str_to_http_method(const char *method_str);

void extract_scheme_from_url(const char *url, char scheme[]);

Expand All @@ -53,10 +53,10 @@ int resolve_domain_to_ip(const char *domain, char *ip_buffer,

bool is_ip_address(const char *str);

char *http_method_to_str(sonic_method_t method);
result_t http_method_to_str(sonic_method_t method);

char *content_type_to_string(sonic_content_type_t content_type);
result_t content_type_to_string(sonic_content_type_t content_type);

sonic_status_t number_to_status_code(u16 status);
result_t number_to_status_code(u16 status);

#endif
#endif
2 changes: 1 addition & 1 deletion sonic/sonic.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void sonic_add_header(sonic_client_request_t *req, char *key, char *value);
void sonic_set_body(sonic_client_request_t *req, char *request_body,
u64 request_body_size);

char *sonic_status_to_string(sonic_status_t status_code);
result_t sonic_status_to_string(sonic_status_t status_code);

// ===================================================================================================================

Expand Down
7 changes: 5 additions & 2 deletions sonic/src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ void http_parse_response_head(sonic_client_response_t *resp,
char *head_buffer) {
u16 status_code = 0;
http_extract_status_code(head_buffer, &status_code);
resp->status = number_to_status_code(status_code);

result_t res_status = number_to_status_code(status_code);
resp->status = UNWRAP_INT(res_status);

const char *header_start =
strstr(head_buffer, "\r\n") + 2; // Skip the first line
Expand Down Expand Up @@ -593,7 +595,8 @@ result_t actually_send_request(sonic_client_request_t *req,
client_request_add_header(req, "Connection", "close");
client_request_add_header(req, "Host", req->domain_name);

char *method_str = http_method_to_str(req->method);
result_t res_method_str = http_method_to_str(req->method);
char *method_str = PROPAGATE(res_method_str);

char *head_str = NULL;

Expand Down
52 changes: 36 additions & 16 deletions sonic/src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ void free_server_response(sonic_server_response_t *resp) {
****/
result_t server_send_response(sonic_server_request_t *req,
sonic_server_response_t *resp) {
char *content_type = content_type_to_string(resp->content_type);
char *status_code = utils_status_code_to_string(resp->status);
result_t res_content_type = content_type_to_string(resp->content_type);
char *content_type = PROPAGATE(res_content_type);

result_t res_status_code = utils_status_code_to_string(resp->status);
char *status_code = PROPAGATE(res_status_code);

char content_length[20];
if (resp->is_file) {
Expand Down Expand Up @@ -511,22 +514,22 @@ void serve_file(sonic_server_request_t *req, char *file_path,
fd = open(file_path, O_RDONLY);
if (fd == -1) {
perror("Error opening file");
exit(1);
PANIC("Error opening file");
}

// Get information about the file (e.g., its size)
if (fstat(fd, &file_info) == -1) {
perror("Error getting file information");
close(fd);
exit(1);
PANIC("Error getting file information");
}

// Map the file into memory
file_contents = mmap(0, file_info.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (file_contents == MAP_FAILED) {
perror("Error mapping file to memory");
close(fd);
exit(1);
PANIC("Error mapping file to memory");
}

sonic_server_response_t *resp = new_server_response(STATUS_200, content_type);
Expand Down Expand Up @@ -746,7 +749,9 @@ void server_add_server_rate_limiter_middleware(sonic_server_t *server,
* extracts the method from a buffer containing a http request head
*
****/
void extract_request_method(const char *head_buffer, char request_method[]) {
result_t extract_request_method(const char *head_buffer) {
char request_method[64];

// Find the first space in the HTTP request header
const char *space_ptr = strchr(head_buffer, ' ');

Expand All @@ -759,29 +764,35 @@ void extract_request_method(const char *head_buffer, char request_method[]) {
request_method[method_length] = '\0'; // Null-terminate the string
} else {
// No space found in the header; invalid HTTP request
printf("EROR: Invalid HTTP request header.\n");
return ERR("Invalid HTTP request header.\n");
}

char *res_request_method = nstrdup(request_method);

return OK(res_request_method);
}

/****
* extracts the path from a buffer containing a http request head
*
****/
void extract_request_path(const char *head_buffer, char request_path[]) {
result_t extract_request_path(const char *head_buffer) {
char request_path[256];

// Find the start and end positions of the request path
const char *start = strchr(head_buffer, ' '); // Find the first space
if (start == NULL) {
// If no space is found, return an empty string
printf("ERROR: when parsing request path, no space was found in head \n");
return ERR("when parsing request path, no space was found in head \n");
}

start++; // Move to the character after the first space

const char *end = strchr(start, ' '); // Find the next space
if (end == NULL) {
// If no second space is found, return an empty string
printf("ERROR: when parsing request path, no second space was found in "
"head \n");
return ERR("when parsing request path, no second space was found in "
"head \n");
}

// Calculate the length of the request path
Expand All @@ -790,6 +801,10 @@ void extract_request_path(const char *head_buffer, char request_path[]) {
// Copy the request path to the output string
strncpy(request_path, start, length);
request_path[length] = '\0';

char *res_request_path = nstrdup(request_path);

return OK(res_request_path);
}

/****
Expand All @@ -798,14 +813,19 @@ void extract_request_path(const char *head_buffer, char request_path[]) {
*
****/
result_t parse_request_headers(sonic_server_request_t *req, char *head_buffer) {
char request_method[64];
extract_request_method(head_buffer, request_method);
req->method = str_to_http_method(request_method);
result_t res_extract_method = extract_request_method(head_buffer);
char *request_method = PROPAGATE(res_extract_method);

char request_path[256];
extract_request_path(head_buffer, request_path);
result_t res_method = str_to_http_method(request_method);
nfree(request_method);

req->method = PROPAGATE_INT(res_method);

result_t res_extract_path = extract_request_path(head_buffer);
char *request_path = PROPAGATE(res_extract_path);

result_t res_path = raw_path_to_sonic_path(request_path);
nfree(request_path);
if (is_err(res_path)) {
return res_path;
}
Expand Down
2 changes: 1 addition & 1 deletion sonic/src/sonic.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void sonic_add_directory_route(sonic_server_t *server, char *path,

void sonic_stop_server(sonic_server_t *server) { return stop_server(server); }

char *sonic_status_to_string(sonic_status_t status_code) {
result_t sonic_status_to_string(sonic_status_t status_code) {
return utils_status_code_to_string(status_code);
}

Expand Down
Loading

0 comments on commit b34e19d

Please sign in to comment.