diff --git a/include/articles_op/article.h b/include/articles_p/article.h similarity index 91% rename from include/articles_op/article.h rename to include/articles_p/article.h index b0fdb4d..f03b932 100644 --- a/include/articles_op/article.h +++ b/include/articles_p/article.h @@ -9,7 +9,7 @@ #include "../../include/file_op/file.h" #include "../../include/utils_op/utils.h" -#include "../../include/articles_op/process_md.h" +#include "../../include/articles_p/process_md.h" #define LINE_LENGTH 512 diff --git a/include/articles_op/html.h b/include/articles_p/html.h similarity index 100% rename from include/articles_op/html.h rename to include/articles_p/html.h diff --git a/include/articles_op/process_md.h b/include/articles_p/process_md.h similarity index 100% rename from include/articles_op/process_md.h rename to include/articles_p/process_md.h diff --git a/include/gallery_op/gallery.h b/include/gallery_p/gallery.h similarity index 100% rename from include/gallery_op/gallery.h rename to include/gallery_p/gallery.h diff --git a/include/projects_p/projects.h b/include/projects_p/projects.h new file mode 100644 index 0000000..2b29470 --- /dev/null +++ b/include/projects_p/projects.h @@ -0,0 +1,22 @@ +#ifndef _PROJECTS_H +#define _PROJECTS_H + +#include +#include +#include + +#define LINE_LENGTH 512 + +typedef struct { + char *title; + char *description; + char *lang; + char *license; + char *url; +} project_t; + +ssize_t read_list(project_t **list); +char *gen_project_html(); +void free_proj_list(project_t **list, int len); + +#endif \ No newline at end of file diff --git a/src/articles_op/article.c b/src/articles_p/article.c similarity index 96% rename from src/articles_op/article.c rename to src/articles_p/article.c index 72c9dfe..06846fa 100644 --- a/src/articles_op/article.c +++ b/src/articles_p/article.c @@ -1,4 +1,4 @@ -#include "../../include/articles_op/article.h" +#include "../../include/articles_p/article.h" int list_articles(article_info **articles) { @@ -50,6 +50,8 @@ int list_articles(article_info **articles) // printf("Found article \"%s\" posted on %ld\n", (*articles)[articles_amount - 1].time, (*articles)[articles_amount - 1].title); } + fclose(file); + free(buff); return articles_amount; @@ -61,6 +63,7 @@ long get_article_contents(article_info *article) if (strcmp(article->title, "No title") == 0) { int line_length = snprintf(NULL, 0, "%ld", article->time) + 1; + free(name); name = malloc(line_length); if (name == NULL) { @@ -82,6 +85,7 @@ long get_article_contents(article_info *article) snprintf(path, line_length, "./static/articles/%s/%s.md", name, name); FILE *file = fopen(path, "r"); + free(path); if (file == NULL) { perror("Couldn't open article file"); @@ -98,6 +102,9 @@ long get_article_contents(article_info *article) article->content[article->length] = '\0'; + free(name); + fclose(file); + return article->length; } diff --git a/src/articles_op/html.c b/src/articles_p/html.c similarity index 94% rename from src/articles_op/html.c rename to src/articles_p/html.c index 77e7494..5cb7b06 100644 --- a/src/articles_op/html.c +++ b/src/articles_p/html.c @@ -1,5 +1,5 @@ -#include "../../include/articles_op/html.h" -#include "../../include/articles_op/article.h" +#include "../../include/articles_p/html.h" +#include "../../include/articles_p/article.h" #include "../../include/utils_op/utils.h" int gen_html_article(article_info article, char **out) @@ -20,8 +20,6 @@ int gen_html_article(article_info article, char **out) template_str[i] = fgetc(template); template_str[template_size] = '\0'; - *out = malloc(template_size + article.length + 1); - char *content; process_md(article, &content); @@ -38,7 +36,9 @@ int gen_html_article(article_info article, char **out) snprintf(*out, line_length, template_str, article.title, article.title, content); + fclose(template); free(template_str); + free(content); return line_length; } diff --git a/src/articles_op/process_md.c b/src/articles_p/process_md.c similarity index 98% rename from src/articles_op/process_md.c rename to src/articles_p/process_md.c index 4c17edf..8a8b70d 100644 --- a/src/articles_op/process_md.c +++ b/src/articles_p/process_md.c @@ -1,10 +1,10 @@ -#include "../../include/articles_op/process_md.h" +#include "../../include/articles_p/process_md.h" int process_md(article_info article, char **out) { *out = malloc(1); (*out)[0] = '\0'; - char *rest = strdup(article.content); + char *rest = strdup(article.content), *free_rest = rest; char *buff; int is_in_list = 0; @@ -291,5 +291,7 @@ int process_md(article_info article, char **out) } (*out)[strlen(*out) - 1] = '\0'; + free(free_rest); + return strlen(*out); } diff --git a/src/gallery_op/gallery.c b/src/gallery_p/gallery.c similarity index 97% rename from src/gallery_op/gallery.c rename to src/gallery_p/gallery.c index 8b1e6bc..a34eae6 100644 --- a/src/gallery_op/gallery.c +++ b/src/gallery_p/gallery.c @@ -1,4 +1,4 @@ -#include "../../include/gallery_op/gallery.h" +#include "../../include/gallery_p/gallery.h" #include "../../include/utils_op/utils.h" #include "../../include/file_op/file.h" #include "../../include/utils_op/arr.h" @@ -12,7 +12,7 @@ gallery_t *get_album_list() { gallery_t *list = NULL, *curr = list; - char **albums_titles_list = calloc(0, sizeof(char *)); + char **albums_titles_list = malloc(0); ssize_t albums_am = get_dir_list(&albums_titles_list, GALLERY_ROOT); @@ -184,11 +184,13 @@ char *gen_gallery_html() free(album_content); free(album_html); + free(image_template); albums_list_item = albums_list_item->next; } free(album_template); + free(image_template); return gallery_content; } \ No newline at end of file diff --git a/src/netw_op/request.c b/src/netw_op/request.c index f2f4470..f302e4a 100644 --- a/src/netw_op/request.c +++ b/src/netw_op/request.c @@ -2,9 +2,10 @@ #include "../../include/utils_op/utils.h" #include "../../include/file_op/file.h" #include "../../include/file_op/mime.h" -#include "../../include/articles_op/html.h" -#include "../../include/articles_op/article.h" -#include "../../include/gallery_op/gallery.h" +#include "../../include/articles_p/html.h" +#include "../../include/articles_p/article.h" +#include "../../include/gallery_p/gallery.h" +#include "../../include/projects_p/projects.h" /** * @brief Send 404 response @@ -237,8 +238,11 @@ void handle_get_request(int fd, char *request) if (id < 0 || strcmp(id_str, remaining) == 0) { res_404(fd, path); + + free(id_str); return; } + free(id_str); article_info *articles = malloc(0); int amount = list_articles(&articles); @@ -256,7 +260,7 @@ void handle_get_request(int fd, char *request) get_article_contents(&(articles[id])); - char *html; + char *html = NULL; gen_html_article(articles[id], &html); struct header_s *header = gen_header(200, strlen(html), "text/html"); @@ -267,7 +271,10 @@ void handle_get_request(int fd, char *request) printf("Sent article with id=%d\n", id); - free(articles); + free(header->str); + free(header); + + free_article_info_arr(&articles, amount); free(html); return; @@ -300,9 +307,55 @@ void handle_get_request(int fd, char *request) send(fd, res_page, line_length, 0); close(fd); + printf("Sent gallery page\n"); + + free(header->str); + free(header); + free(page_template); free(gallery_content); free(res_page); + + return; + } + + if (strcmp(path, "/projects") == 0 || strcmp(path, "/projects/") == 0) + { + FILE *page_template_fp = fopen("static/projects/index.html", "r"); + if (page_template_fp == NULL) + { + res_404(fd, path); + return; + } + + size_t page_file_size = get_file_size(page_template_fp) + 1; + char *page_template = calloc(1, page_file_size); + + fread(page_template, page_file_size, 1, page_template_fp); + fclose(page_template_fp); + + char *projects_content = gen_project_html(); + + size_t line_length = snprintf(NULL, 0, page_template, projects_content) + 1; + char *res_page = calloc(1, line_length); + sprintf(res_page, page_template, projects_content); + + struct header_s *header = gen_header(200, line_length, "text/html"); + send(fd, header->str, header->size - 1, 0); + + send(fd, res_page, line_length, 0); + close(fd); + + printf("Sent projects page\n"); + + free(header->str); + free(header); + + free(page_template); + free(projects_content); + free(res_page); + + return; } if (send_response(fd, path) < 0) diff --git a/src/projects_p/projects.c b/src/projects_p/projects.c new file mode 100644 index 0000000..5a4ecb3 --- /dev/null +++ b/src/projects_p/projects.c @@ -0,0 +1,104 @@ +#include "../../include/projects_p/projects.h" +#include "../../include/utils_op/arr.h" +#include "../../include/file_op/file.h" + +ssize_t read_list(project_t **list) +{ + FILE *fp = fopen("static/projects/list.db", "r"); + if (fp == NULL) + { + perror("couldn't open projectDB file"); + return -1; + } + + char *buff = NULL; + size_t length = LINE_LENGTH; + size_t projects_amount = 0; + + while (getline(&buff, &length, fp) > 0) + { + if (buff[strlen(buff) - 1] == '\n') + buff[strlen(buff) - 1] = '\0'; + + if (strlen(buff) == 0) + continue; + + projects_amount++; + + *list = realloc(*list, sizeof(project_t) * projects_amount); + + char *tmp = strdup(buff), *rest = tmp; + + (*list)[projects_amount - 1].title = strdup(strtok_r(tmp, ";", &rest)); + + (*list)[projects_amount - 1].description = strdup(strtok_r(rest, ";", &rest)); + + (*list)[projects_amount - 1].lang = strdup(strtok_r(rest, ";", &rest)); + + (*list)[projects_amount - 1].license = strdup(strtok_r(rest, ";", &rest)); + + (*list)[projects_amount - 1].url = strdup(strtok_r(rest, ";", &rest)); + + free(tmp); + } + + fclose(fp); + + free(buff); + + return projects_amount; +} + +char *gen_project_html() +{ + FILE *template_fp = fopen("static/projects/item.html", "r"); + if (template_fp == NULL) + { + perror("couldn't open project tesplate"); + return NULL; + } + + size_t template_file_size = get_file_size(template_fp) + 1; + char *template = calloc(1, template_file_size); + + fread(template, template_file_size, 1, template_fp); + fclose(template_fp); + + project_t *list = malloc(0); + size_t length = read_list(&list); + + char *content = strdup(""); + + for (size_t i = 0; i < length; i++) + { + int line_length = snprintf(NULL, 0, template, list[i].url, list[i].title, list[i].description, list[i].lang, list[i].license) + 1; + char *project_content = calloc(1, line_length); + + sprintf(project_content, template, list[i].url, list[i].title, list[i].description, list[i].lang, list[i].license); + + content = realloc(content, strlen(content) + line_length); + + content = strcat(content, project_content); + + free(project_content); + } + + free_proj_list(&list, length); + free(template); + + return content; +} + +void free_proj_list(project_t **list, int len) +{ + for (int i = 0; i < len; i++) + { + free((*list)[i].description); + free((*list)[i].lang); + free((*list)[i].license); + free((*list)[i].title); + free((*list)[i].url); + } + + free(*list); +} \ No newline at end of file diff --git a/static/projects/index.html b/static/projects/index.html new file mode 100644 index 0000000..cf3607c --- /dev/null +++ b/static/projects/index.html @@ -0,0 +1,36 @@ + + + + + + + + Projects + + + + + +
+ + +
+ +
+

My projects

+
    %s
+
+ + + + + \ No newline at end of file diff --git a/static/projects/item.html b/static/projects/item.html new file mode 100644 index 0000000..27b08f4 --- /dev/null +++ b/static/projects/item.html @@ -0,0 +1,7 @@ +
  • + +

    %s

    +
    +

    %s - %s

    + %s +
  • \ No newline at end of file diff --git a/static/projects/list.db b/static/projects/list.db new file mode 100644 index 0000000..5988b2e --- /dev/null +++ b/static/projects/list.db @@ -0,0 +1,3 @@ +Mshell;A simple yet comfortable UNIX shell;C;MIT License;https://github.com/Dm1tr1y147/mshell +thetriangle;A simple program for drawing triangles;C;None;https://github.com/Dm1tr1y147/thetriangle +md_offliner;Tool for uploading articles to the server;Lua;MIT License;https://github.com/Dm1tr1y147/md_offliner