Fixed unfreed memory and files handling errors

This commit is contained in:
2025-05-30 22:35:11 +03:00
parent df3502ad40
commit d7fe6cd476
9 changed files with 78 additions and 31 deletions

View File

@ -8,7 +8,7 @@
#include <dirent.h>
void err_msg(char *msg);
char *concat_to_front(char **str1, char *str2);
char *prepend(char *dest, const char *prefix);
char *get_status_message(int status_code);
char *to_lower(char *str);
char *trim(char *str);

View File

@ -45,7 +45,7 @@ int list_articles(article_info **articles)
else
(*articles)[articles_amount - 1].title = strdup("No title");
(*articles)[articles_amount - 1].content = malloc(0);
(*articles)[articles_amount - 1].content = NULL;
free(tmp);
@ -124,7 +124,7 @@ void free_article_info_arr(article_info **articles, int length)
// /* Only for testing purposes */
// int main()
// {
// article_info *articles = malloc(0);
// article_info *articles = NULL;
// int n = list_articles(&articles);

View File

@ -84,7 +84,7 @@ int gen_html_article_list(article_info *articles, int n, char **out)
// /* Only for testing purposes */
// int main ()
// {
// article_info *articles = malloc(0);
// article_info *articles = NULL;
// int n = list_articles(&articles);

View File

@ -21,12 +21,30 @@ char *gen_file_path(char *req_path)
path = strcat(path, "index.html");
}
char *webroot = "static";
char webroot[PATH_MAX];
if (getcwd(webroot, PATH_MAX) == NULL)
goto exit_error;
path = realloc(path, strlen(path) + strlen(webroot) + 1);
path = concat_to_front(&path, webroot);
if (PATH_MAX < strlen(webroot) + strlen("/static"))
goto exit_error;
strcat(webroot, "/static");
path = prepend(path, webroot);
char resolved_path[PATH_MAX];
if (realpath(path, resolved_path) == NULL)
goto exit_error;
if (strncmp(resolved_path, webroot, strlen(webroot)) != 0) {
goto exit_error;
}
return path;
exit_error:
free(path);
return NULL;
}
/**

View File

@ -9,7 +9,7 @@
*/
char *get_mime_type(char *file_path)
{
char *ext = strchr(file_path, '.');
char *ext = strrchr(file_path, '.');
if (ext == NULL)
{

View File

@ -10,9 +10,9 @@
*/
gallery_t *get_album_list()
{
gallery_t *list = NULL, *curr = list;
gallery_t *list = NULL, *curr;
char **albums_titles_list = malloc(0);
char **albums_titles_list = NULL;
ssize_t albums_am = get_dir_list(&albums_titles_list, GALLERY_ROOT);
@ -50,18 +50,18 @@ gallery_t *get_album_list()
*/
void free_albums_list(gallery_t *albums_list)
{
gallery_t *curr_item = albums_list;
while (curr_item != NULL)
while (albums_list != NULL)
{
free(curr_item->title);
for (int i = 0; i < curr_item->img_am; i++)
free_img_item(curr_item->images[i]);
free(curr_item->images);
curr_item = curr_item->next;
}
free(albums_list->title);
for (int i = 0; i < albums_list->img_am; i++)
free_img_item(albums_list->images[i]);
free(albums_list->images);
gallery_t *next_item = albums_list->next;
free(albums_list);
albums_list = next_item;
}
}
/**
@ -154,7 +154,15 @@ int get_album_imgs(img_t **images_arr, int *size, char *title)
char *gen_gallery_html()
{
FILE *album_template_fp = fopen("static/gallery/album.html", "r");
if (album_template_fp == NULL) {
return "500 Internal Error\n";
}
FILE *image_template_fp = fopen("static/gallery/image.html", "r");
if (image_template_fp == NULL) {
fclose(album_template_fp);
return "500 Internal Error\n";
}
size_t album_file_size = get_file_size(album_template_fp) + 1;
char *album_template = calloc(1, album_file_size);

View File

@ -16,6 +16,12 @@
void res_404(int fd, char *path)
{
FILE *fp = fopen("static/404.html", "r");
if (fp == NULL) {
res_500(fd);
return;
}
const ssize_t fsize = 512;
char *buff = malloc(fsize), *msg = malloc(fsize);
@ -102,6 +108,12 @@ int send_response(int fd, char *req_path)
{
char *file_path = gen_file_path(req_path);
if (file_path == NULL) {
res_404(fd, req_path);
free(file_path);
return 0;
}
struct file_s *file = get_file_info(file_path);
if (file == NULL)
@ -121,6 +133,8 @@ int send_response(int fd, char *req_path)
return 0;
}
int return_code = 0;
char *mime_type = get_mime_type(file_path);
struct header_s *header = gen_header(200, file->size, mime_type);
@ -130,15 +144,18 @@ int send_response(int fd, char *req_path)
if (rv < 0)
{
err_msg("couldn't send header");
return -1;
return_code = -1;
goto exit_code;
}
if (send_file(fd, file) < 0)
{
err_msg("couldn't send file");
return -1;
return_code = -1;
goto exit_code;
}
exit_code:
close(fd);
free(header->str);
@ -146,7 +163,8 @@ int send_response(int fd, char *req_path)
free(file);
free(file_path);
return 0;
return return_code;
}
/**
@ -189,7 +207,7 @@ void handle_get_request(int fd, char *request)
fclose(fp);
template[file_size-1] = '\0';
article_info *articles = malloc(0);
article_info *articles = NULL;
int amount = list_articles(&articles);
if (amount < 0)
{
@ -245,7 +263,7 @@ void handle_get_request(int fd, char *request)
}
free(id_str);
article_info *articles = malloc(0);
article_info *articles = NULL;
int amount = list_articles(&articles);
if (amount < 0)
{
@ -255,6 +273,7 @@ void handle_get_request(int fd, char *request)
if (id > amount - 1)
{
free_article_info_arr(&articles, amount);
res_404(fd, path);
return;
}

View File

@ -64,7 +64,7 @@ char *gen_project_html()
fread(template, template_file_size, 1, template_fp);
fclose(template_fp);
project_t *list = malloc(0);
project_t *list = NULL;
size_t length = read_list(&list);
char *content = strdup("");

View File

@ -17,16 +17,18 @@ void err_msg(char *msg)
* @param str2
* @return char*
*/
char *concat_to_front(char **str1, char *str2)
char *prepend(char *dest, const char *prefix)
{
char *tmp = strdup(*str1);
size_t orig_len = strlen(dest);
size_t prefix_len = strlen(prefix);
strcpy(*str1, str2);
strcat(*str1, tmp);
dest = realloc(dest, orig_len + prefix_len + 1);
free(tmp);
memmove(dest + prefix_len, dest, orig_len + 1);
return *str1;
memcpy(dest, prefix, prefix_len);
return dest;
}
/**