diff --git a/.gitignore b/.gitignore index 7eceb7f..d0d1599 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ tmp/ build/ .vscode/ a.out -test_* \ No newline at end of file +test_* +**/exif.? \ No newline at end of file diff --git a/include/articles_op/article.h b/include/articles_op/article.h index 6a7b6bd..b0fdb4d 100644 --- a/include/articles_op/article.h +++ b/include/articles_op/article.h @@ -28,5 +28,6 @@ typedef struct int list_articles(article_info **articles); long get_article_contents(article_info *article); +void free_article_info_arr(article_info **articles, int length); #endif \ No newline at end of file diff --git a/include/gallery_op/gallery.h b/include/gallery_op/gallery.h new file mode 100644 index 0000000..3507344 --- /dev/null +++ b/include/gallery_op/gallery.h @@ -0,0 +1,31 @@ +#ifndef _LIST_GAL_H +#define _LIST_GAL_H + +#include +#include + +typedef struct { + char *description; + char *path; + char **tags; + time_t date_taken; + short int rating; +} img_t; + +typedef struct album_s +{ + char *title; + img_t *images; + int img_am; + struct album_s *next; +} gallery_t; + +gallery_t *get_album_list(); +gallery_t *new_album_item(char *title); +img_t new_img_item(char *path); +int get_album_imgs(img_t **images_arr, int *size, char *title); +char *gen_gallery_html(); + +#define GALLERY_ROOT "static/gallery/albums/" + +#endif \ No newline at end of file diff --git a/include/utils_op/utils.h b/include/utils_op/utils.h index b74f8c5..5304b78 100644 --- a/include/utils_op/utils.h +++ b/include/utils_op/utils.h @@ -5,6 +5,7 @@ #include #include #include +#include void err_msg(char *msg); char *concat_to_front(char **str1, char *str2); @@ -12,5 +13,6 @@ char *get_status_message(int status_code); char *to_lower(char *str); char *trim(char *str); char *repair_spaces(char *str); +ssize_t get_dir_list(char ***dir_list, char *path); #endif \ No newline at end of file diff --git a/src/articles_op/article.c b/src/articles_op/article.c index dcf701c..72c9dfe 100644 --- a/src/articles_op/article.c +++ b/src/articles_op/article.c @@ -43,9 +43,11 @@ int list_articles(article_info **articles) if (strlen(rest) > 0) (*articles)[articles_amount - 1].title = strdup(rest); else - (*articles)[articles_amount - 1].title = "No title"; + (*articles)[articles_amount - 1].title = strdup("No title"); - printf("%ld - \"%s\"\n", (*articles)[articles_amount - 1].time, (*articles)[articles_amount - 1].title); + free(tmp); + + // printf("Found article \"%s\" posted on %ld\n", (*articles)[articles_amount - 1].time, (*articles)[articles_amount - 1].title); } free(buff); @@ -99,6 +101,17 @@ long get_article_contents(article_info *article) return article->length; } +void free_article_info_arr(article_info **articles, int length) +{ + for (int i = 0; i < length; i++) + { + free((*articles)[i].content); + free((*articles)[i].title); + } + + free(*articles); +} + // /* Only for testing purposes */ // int main() // { diff --git a/src/articles_op/process_md.c b/src/articles_op/process_md.c index b05db32..4c17edf 100644 --- a/src/articles_op/process_md.c +++ b/src/articles_op/process_md.c @@ -250,8 +250,6 @@ int process_md(article_info article, char **out) free(href); free(internal_text); - printf("++%d-%c++\n", i, buff[i]); - continue; } @@ -265,8 +263,6 @@ int process_md(article_info article, char **out) *out = tmp_out; (*out)[len] = buff[i]; (*out)[len + 1] = '\0'; - - printf("**%d-%c**\n", i, buff[i]); } tmp_out = realloc(*out, strlen(*out) + strlen("

") + 1); diff --git a/src/file_op/file.c b/src/file_op/file.c index f06aee1..9c60e1a 100644 --- a/src/file_op/file.c +++ b/src/file_op/file.c @@ -9,13 +9,12 @@ */ char *gen_file_path(char *req_path) { - char *path = (char *)malloc(strlen(req_path) + 1); - strcpy(path, req_path); + char *path = strdup(req_path); if (strchr(req_path, '.') == NULL) { if (req_path[strlen(req_path) - 1] != '/') { - path = realloc(path, strlen(path) + 1); + path = realloc(path, strlen(path) + 2); path = strcat(path, "/"); } path = realloc(path, strlen(path) + strlen("index.html") + 1); @@ -24,7 +23,7 @@ char *gen_file_path(char *req_path) char *webroot = "static"; - path = realloc(path, strlen(path) + strlen(webroot)); + path = realloc(path, strlen(path) + strlen(webroot) + 1); path = concat_to_front(&path, webroot); return path; @@ -46,7 +45,6 @@ int send_file(int cli_fd, struct file_s *file) } close(file->fd); - free(file); return 0; } @@ -58,7 +56,7 @@ int send_file(int cli_fd, struct file_s *file) */ struct file_s *get_file_info(char *file_path) { - struct file_s *file = (struct file_s *)malloc(sizeof(struct file_s)); + struct file_s *file = malloc(sizeof(struct file_s)); if (file == NULL) { diff --git a/src/gallery_op/gallery.c b/src/gallery_op/gallery.c new file mode 100644 index 0000000..8b1e6bc --- /dev/null +++ b/src/gallery_op/gallery.c @@ -0,0 +1,194 @@ +#include "../../include/gallery_op/gallery.h" +#include "../../include/utils_op/utils.h" +#include "../../include/file_op/file.h" +#include "../../include/utils_op/arr.h" + +/** + * @brief Get the list of albums on server + * + * @return gallery_t* + */ +gallery_t *get_album_list() +{ + gallery_t *list = NULL, *curr = list; + + char **albums_titles_list = calloc(0, sizeof(char *)); + + ssize_t albums_am = get_dir_list(&albums_titles_list, GALLERY_ROOT); + + if (albums_am < 0) + { + err_msg("couldn't read albums list"); + return list; + } + if (albums_am == 0) + return list; + + list = new_album_item(albums_titles_list[0]); + get_album_imgs(&list->images, &list->img_am, albums_titles_list[0]); + + curr = list; + + for (unsigned int i = 1; i < albums_am; i++) + { + curr->next = new_album_item(albums_titles_list[i]); + + curr = curr->next; + + get_album_imgs(&curr->images, &curr->img_am, albums_titles_list[i]); + } + + free_arr(albums_titles_list, albums_am); + + return list; +} + +/** + * @brief Generates new album item + * + * @param title + * @return gallery_t* + */ +gallery_t *new_album_item(char *folder_name) +{ + gallery_t *new = calloc(1, sizeof(gallery_t)); + + char *title = strdup(folder_name); + title[strlen(title) - 1] = '\0'; + + new->title = strdup(repair_spaces(title)); + free(title); + + new->img_am = 0; + + new->images = NULL; + new->next = NULL; + + return new; +} + +/** + * @brief Generates new image item + * + * @param path + * @return img_t + */ +img_t new_img_item(char *path) +{ + img_t img; + + img.date_taken = 0; + img.description = NULL; + img.path = strdup(path); + img.rating = 0; + img.tags = NULL; + + return img; +} + +int get_album_imgs(img_t **images_arr, int *size, char *title) +{ + char **photos_list = calloc(0, sizeof(char *)); + + char *album_path = strdup(GALLERY_ROOT); + album_path = realloc(album_path, strlen(album_path) + strlen(title) + 1); + album_path = strcat(album_path, title); + + ssize_t photos_am = get_dir_list(&photos_list, album_path); + free(album_path); + + *images_arr = calloc(photos_am, sizeof(img_t)); + + for (int j = 0; j < photos_am; j++) + { + char *img_path = strdup("/gallery/albums/"); + img_path = realloc(img_path, strlen(img_path) + strlen(title) + strlen(photos_list[j]) + 1); + img_path = strcat(img_path, title); + img_path = strcat(img_path, photos_list[j]); + + (*images_arr)[j] = new_img_item(img_path); + + free(img_path); + } + + *size = photos_am; + + free_arr(photos_list, photos_am); + + return photos_am; +} + +char *gen_gallery_html() +{ + FILE *album_template_fp = fopen("static/gallery/album.html", "r"); + FILE *image_template_fp = fopen("static/gallery/image.html", "r"); + + size_t album_file_size = get_file_size(album_template_fp) + 1; + char *album_template = calloc(1, album_file_size); + + size_t image_file_size = get_file_size(image_template_fp) + 1; + char *image_template = calloc(1, image_file_size); + + fread(album_template, album_file_size, 1, album_template_fp); + fclose(album_template_fp); + + fread(image_template, image_file_size, 1, image_template_fp); + fclose(image_template_fp); + + gallery_t *albums_list_item = get_album_list(); + + char *gallery_content = strdup(""); + + while (albums_list_item != NULL) + { + if (albums_list_item->img_am <= 0) + { + albums_list_item = albums_list_item->next; + continue; + } + + char *album_content = strdup(""); + + for (int i = 0; i < albums_list_item->img_am; i++) + { + char *link = albums_list_item->images[i].path; + size_t line_length = snprintf(NULL, 0, image_template, link, link) + 1; + + char *image_content = calloc(1, line_length); + if (image_content == NULL) + return "500 Internal Error\n"; + + sprintf(image_content, image_template, link, link); + + char *tmp = realloc(album_content, strlen(album_content) + strlen(image_content) + 1); + if (tmp == NULL) + return "500 Internal Error\n"; + + album_content = tmp; + album_content = strcat(album_content, image_content); + + free(image_content); + } + + size_t line_length = snprintf(NULL, 0, album_template, albums_list_item->title, album_content) + 1; + + char *album_html = calloc(1, line_length); + if (album_html == NULL) + return "500 Internal Error\n"; + + sprintf(album_html, album_template, albums_list_item->title, album_content); + + gallery_content = realloc(gallery_content, strlen(gallery_content) + line_length); + + gallery_content = strcat(gallery_content, album_html); + + free(album_content); + free(album_html); + + albums_list_item = albums_list_item->next; + } + + free(album_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 cd156c7..f2f4470 100644 --- a/src/netw_op/request.c +++ b/src/netw_op/request.c @@ -4,6 +4,7 @@ #include "../../include/file_op/mime.h" #include "../../include/articles_op/html.h" #include "../../include/articles_op/article.h" +#include "../../include/gallery_op/gallery.h" /** * @brief Send 404 response @@ -15,19 +16,23 @@ void res_404(int fd, char *path) { FILE *fp = fopen("static/404.html", "r"); const ssize_t fsize = 512; - char buff[fsize], msg[fsize]; + char *buff = malloc(fsize), *msg = malloc(fsize); fread(buff, fsize, 1, fp); fclose(fp); sprintf(msg, buff, path, path); + free(buff); struct header_s *header = gen_header(404, strlen(msg), "text/html"); send(fd, header->str, header->size - 1, 0); + free(header->str); + free(header); send(fd, msg, strlen(msg), 0); close(fd); + free(msg); printf("404 ERROR\n"); } @@ -74,10 +79,10 @@ struct header_s *gen_header(int status_code, size_t file_size, char *mime_type) char *curr_date = asctime(ptm); int header_length = snprintf(NULL, 0, "HTTP/1.1 %d %s\nDate: %sConnection: close\nContent-Length: %ld\nContent-Type: %s\r\n\r\n", status_code, status_message, curr_date, file_size, mime_type) + 1; - char *header_string = (char *)malloc(header_length); + char *header_string = malloc(header_length); snprintf(header_string, header_length, "HTTP/1.1 %d %s\nDate: %sConnection: close\nContent-Length: %ld\nContent-Type: %s\r\n\r\n", status_code, status_message, curr_date, file_size, mime_type); - struct header_s *header = (struct header_s *)malloc(header_length + sizeof(size_t)); + struct header_s *header = malloc(header_length + sizeof(size_t)); header->size = header_length; header->str = header_string; @@ -102,6 +107,7 @@ int send_response(int fd, char *req_path) { res_500(fd); + free(file_path); return 0; } @@ -109,6 +115,8 @@ int send_response(int fd, char *req_path) { res_404(fd, req_path); + free(file_path); + free(file); return 0; } @@ -132,6 +140,11 @@ int send_response(int fd, char *req_path) close(fd); + free(header->str); + free(header); + + free(file); + free(file_path); return 0; } @@ -203,12 +216,20 @@ void handle_get_request(int fd, char *request) printf("Sent home page\n"); + free(header->str); + free(header); + + free(msg); + free(template); + free_article_info_arr(&articles, amount); + free(articles_list_str); + return; } if (strncmp(path, "/article/", strlen("/article/")) == 0) { - char *id_str = (char *)malloc(strlen(path) - strlen("/article/") + 1); + char *id_str = malloc(strlen(path) - strlen("/article/") + 1); memmove(id_str, path + strlen("/article/"), strlen(path) - strlen("/article/") + 1); char *remaining; @@ -227,7 +248,7 @@ void handle_get_request(int fd, char *request) return; } - if (id > amount-1) + if (id > amount - 1) { res_404(fd, path); return; @@ -252,6 +273,38 @@ void handle_get_request(int fd, char *request) return; } + if (strcmp(path, "/gallery") == 0 || strcmp(path, "/gallery/") == 0 || strcmp(path, "/gallery/index.html") == 0) + { + FILE *page_template_fp = fopen("static/gallery/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 *gallery_content = gen_gallery_html(); + + size_t line_length = snprintf(NULL, 0, page_template, gallery_content) + 1; + char *res_page = calloc(1, line_length); + sprintf(res_page, page_template, gallery_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); + + free(page_template); + free(gallery_content); + free(res_page); + } + if (send_response(fd, path) < 0) { err_msg("couldn't send response"); diff --git a/src/utils_op/utils.c b/src/utils_op/utils.c index 819ef2f..32547ec 100644 --- a/src/utils_op/utils.c +++ b/src/utils_op/utils.c @@ -24,6 +24,8 @@ char *concat_to_front(char **str1, char *str2) strcpy(*str1, str2); strcat(*str1, tmp); + free(tmp); + return *str1; } @@ -99,3 +101,43 @@ char *repair_spaces(char *str) return str; } + +/** + * @brief Gets the list of files and directories at the specified path + * + * @param dir_list + * @param path + * @return ssize_t + */ +ssize_t get_dir_list(char ***dir_list, char *path) +{ + DIR *dir; + struct dirent *ent; + + if ((dir = opendir(path)) == NULL) + { + perror("\nOpendir"); + return -1; + } + + ssize_t n = 0; + while ((ent = readdir(dir)) != NULL) + { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) + continue; + + n++; + *dir_list = realloc(*dir_list, sizeof(char *) * n); + (*dir_list)[n - 1] = strdup(ent->d_name); + + if (ent->d_type == DT_DIR) + { + (*dir_list)[n - 1] = realloc((*dir_list)[n - 1], strlen((*dir_list)[n - 1]) + 2); + (*dir_list)[n - 1] = strcat((*dir_list)[n - 1], "/"); + } + } + + closedir(dir); + + return n; +} \ No newline at end of file diff --git a/static/about/index.html b/static/about/index.html index 14dcb72..d90d277 100644 --- a/static/about/index.html +++ b/static/about/index.html @@ -13,8 +13,8 @@
- Modernize +

About me

diff --git a/static/articles/My_first_article_on_this_site/My_first_article_on_this_site.md b/static/articles/My_first_article_on_this_site/My_first_article_on_this_site.md index 15a6800..4daf6dc 100644 --- a/static/articles/My_first_article_on_this_site/My_first_article_on_this_site.md +++ b/static/articles/My_first_article_on_this_site/My_first_article_on_this_site.md @@ -1,4 +1,3 @@ -# My first article on this site This is my own site written in pure C with templates in html and some css styles. All articles are stored in markdown format and are processed by server to convert them to html and send to client. Links list on the homepage is also generated dynamically to show all the articles, currntly avaliable on disk. ![Data flow](/articles/My_first_article_on_this_site/P5RIaj.png) diff --git a/static/articles/index.html b/static/articles/index.html index 937d301..ada4bcc 100644 --- a/static/articles/index.html +++ b/static/articles/index.html @@ -13,8 +13,8 @@
- Modernize +

%s

diff --git a/static/articles/list.db b/static/articles/list.db index a0e477a..f524eeb 100644 --- a/static/articles/list.db +++ b/static/articles/list.db @@ -1 +1 @@ -1593768639 My_first_article_on_this_site \ No newline at end of file +1594048366 My_first_article_on_this_site \ No newline at end of file diff --git a/static/gallery/album.html b/static/gallery/album.html new file mode 100644 index 0000000..cced385 --- /dev/null +++ b/static/gallery/album.html @@ -0,0 +1,6 @@ +
+ +

%s

+
+
    %s
+
\ No newline at end of file diff --git a/static/gallery/image.html b/static/gallery/image.html new file mode 100644 index 0000000..2a45a10 --- /dev/null +++ b/static/gallery/image.html @@ -0,0 +1,3 @@ +
  • + +
  • \ No newline at end of file diff --git a/static/gallery/index.html b/static/gallery/index.html index 58d7d46..8c15f40 100644 --- a/static/gallery/index.html +++ b/static/gallery/index.html @@ -12,11 +12,28 @@
    - - Modernize + +
    -

    Work in porgress

    +
    +

    Heare are some of my photos.

    + %s +
    + \ No newline at end of file diff --git a/static/index.html b/static/index.html index 3d5f9cd..6d64f10 100644 --- a/static/index.html +++ b/static/index.html @@ -13,8 +13,8 @@
    - Modernize +

    Home

    @@ -22,7 +22,6 @@
  • About me
  • Gallery
  • Projects
  • -
  • Philosophy
  • Articles

    %s diff --git a/static/style.css b/static/style.css index 281ca3c..920a41d 100644 --- a/static/style.css +++ b/static/style.css @@ -34,7 +34,7 @@ header img { height: 100%; } -header #modern { +/* header #modern { line-height: 65px; text-align: center; vertical-align: top; @@ -46,7 +46,7 @@ header #modern { header #modern:hover { text-decoration: none; -} +} */ main { max-width: 800px;