Added gallery site section and automatic directory scanning. Fixed some memory errors, removed obsolete logs

This commit is contained in:
Dm1tr1y147 2020-07-28 06:06:00 +05:00
parent fd6007b22b
commit 21f5a392fa
19 changed files with 388 additions and 33 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ build/
.vscode/ .vscode/
a.out a.out
test_* test_*
**/exif.?

View File

@ -28,5 +28,6 @@ typedef struct
int list_articles(article_info **articles); int list_articles(article_info **articles);
long get_article_contents(article_info *article); long get_article_contents(article_info *article);
void free_article_info_arr(article_info **articles, int length);
#endif #endif

View File

@ -0,0 +1,31 @@
#ifndef _LIST_GAL_H
#define _LIST_GAL_H
#include <stdlib.h>
#include <stdio.h>
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

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h>
void err_msg(char *msg); void err_msg(char *msg);
char *concat_to_front(char **str1, char *str2); 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 *to_lower(char *str);
char *trim(char *str); char *trim(char *str);
char *repair_spaces(char *str); char *repair_spaces(char *str);
ssize_t get_dir_list(char ***dir_list, char *path);
#endif #endif

View File

@ -43,9 +43,11 @@ int list_articles(article_info **articles)
if (strlen(rest) > 0) if (strlen(rest) > 0)
(*articles)[articles_amount - 1].title = strdup(rest); (*articles)[articles_amount - 1].title = strdup(rest);
else 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); free(buff);
@ -99,6 +101,17 @@ long get_article_contents(article_info *article)
return article->length; 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 */ // /* Only for testing purposes */
// int main() // int main()
// { // {

View File

@ -250,8 +250,6 @@ int process_md(article_info article, char **out)
free(href); free(href);
free(internal_text); free(internal_text);
printf("++%d-%c++\n", i, buff[i]);
continue; continue;
} }
@ -265,8 +263,6 @@ int process_md(article_info article, char **out)
*out = tmp_out; *out = tmp_out;
(*out)[len] = buff[i]; (*out)[len] = buff[i];
(*out)[len + 1] = '\0'; (*out)[len + 1] = '\0';
printf("**%d-%c**\n", i, buff[i]);
} }
tmp_out = realloc(*out, strlen(*out) + strlen("</p>") + 1); tmp_out = realloc(*out, strlen(*out) + strlen("</p>") + 1);

View File

@ -9,13 +9,12 @@
*/ */
char *gen_file_path(char *req_path) char *gen_file_path(char *req_path)
{ {
char *path = (char *)malloc(strlen(req_path) + 1); char *path = strdup(req_path);
strcpy(path, req_path);
if (strchr(req_path, '.') == NULL) if (strchr(req_path, '.') == NULL)
{ {
if (req_path[strlen(req_path) - 1] != '/') if (req_path[strlen(req_path) - 1] != '/')
{ {
path = realloc(path, strlen(path) + 1); path = realloc(path, strlen(path) + 2);
path = strcat(path, "/"); path = strcat(path, "/");
} }
path = realloc(path, strlen(path) + strlen("index.html") + 1); path = realloc(path, strlen(path) + strlen("index.html") + 1);
@ -24,7 +23,7 @@ char *gen_file_path(char *req_path)
char *webroot = "static"; char *webroot = "static";
path = realloc(path, strlen(path) + strlen(webroot)); path = realloc(path, strlen(path) + strlen(webroot) + 1);
path = concat_to_front(&path, webroot); path = concat_to_front(&path, webroot);
return path; return path;
@ -46,7 +45,6 @@ int send_file(int cli_fd, struct file_s *file)
} }
close(file->fd); close(file->fd);
free(file);
return 0; 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 *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) if (file == NULL)
{ {

194
src/gallery_op/gallery.c Normal file
View File

@ -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;
}

View File

@ -4,6 +4,7 @@
#include "../../include/file_op/mime.h" #include "../../include/file_op/mime.h"
#include "../../include/articles_op/html.h" #include "../../include/articles_op/html.h"
#include "../../include/articles_op/article.h" #include "../../include/articles_op/article.h"
#include "../../include/gallery_op/gallery.h"
/** /**
* @brief Send 404 response * @brief Send 404 response
@ -15,19 +16,23 @@ void res_404(int fd, char *path)
{ {
FILE *fp = fopen("static/404.html", "r"); FILE *fp = fopen("static/404.html", "r");
const ssize_t fsize = 512; const ssize_t fsize = 512;
char buff[fsize], msg[fsize]; char *buff = malloc(fsize), *msg = malloc(fsize);
fread(buff, fsize, 1, fp); fread(buff, fsize, 1, fp);
fclose(fp); fclose(fp);
sprintf(msg, buff, path, path); sprintf(msg, buff, path, path);
free(buff);
struct header_s *header = gen_header(404, strlen(msg), "text/html"); struct header_s *header = gen_header(404, strlen(msg), "text/html");
send(fd, header->str, header->size - 1, 0); send(fd, header->str, header->size - 1, 0);
free(header->str);
free(header);
send(fd, msg, strlen(msg), 0); send(fd, msg, strlen(msg), 0);
close(fd); close(fd);
free(msg);
printf("404 ERROR\n"); 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); 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; 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); 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->size = header_length;
header->str = header_string; header->str = header_string;
@ -102,6 +107,7 @@ int send_response(int fd, char *req_path)
{ {
res_500(fd); res_500(fd);
free(file_path);
return 0; return 0;
} }
@ -109,6 +115,8 @@ int send_response(int fd, char *req_path)
{ {
res_404(fd, req_path); res_404(fd, req_path);
free(file_path);
free(file);
return 0; return 0;
} }
@ -132,6 +140,11 @@ int send_response(int fd, char *req_path)
close(fd); close(fd);
free(header->str);
free(header);
free(file);
free(file_path);
return 0; return 0;
} }
@ -203,12 +216,20 @@ void handle_get_request(int fd, char *request)
printf("Sent home page\n"); 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; return;
} }
if (strncmp(path, "/article/", strlen("/article/")) == 0) 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); memmove(id_str, path + strlen("/article/"), strlen(path) - strlen("/article/") + 1);
char *remaining; char *remaining;
@ -227,7 +248,7 @@ void handle_get_request(int fd, char *request)
return; return;
} }
if (id > amount-1) if (id > amount - 1)
{ {
res_404(fd, path); res_404(fd, path);
return; return;
@ -252,6 +273,38 @@ void handle_get_request(int fd, char *request)
return; 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) if (send_response(fd, path) < 0)
{ {
err_msg("couldn't send response"); err_msg("couldn't send response");

View File

@ -24,6 +24,8 @@ char *concat_to_front(char **str1, char *str2)
strcpy(*str1, str2); strcpy(*str1, str2);
strcat(*str1, tmp); strcat(*str1, tmp);
free(tmp);
return *str1; return *str1;
} }
@ -99,3 +101,43 @@ char *repair_spaces(char *str)
return 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;
}

View File

@ -13,8 +13,8 @@
<body> <body>
<header> <header>
<a href="/" id="logo"><img src="/logo.png" alt="logo" /></a> <a href="/" id="logo"><img src="/logo.png" alt="logo" /></a>
<a id="modern" href="/modern" <!-- <a id="modern" href="/modern"
title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> -->
</header> </header>
<h1>About me</h1> <h1>About me</h1>
</body> </body>

View File

@ -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. 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) ![Data flow](/articles/My_first_article_on_this_site/P5RIaj.png)

View File

@ -13,8 +13,8 @@
<body> <body>
<header> <header>
<a href="/" id="logo"><img src="/logo.png" alt="logo" /></a> <a href="/" id="logo"><img src="/logo.png" alt="logo" /></a>
<a id="modern" href="/modern" <!-- <a id="modern" href="/modern"
title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> -->
</header> </header>
<main> <main>
<h1 id="header">%s</h1> <h1 id="header">%s</h1>

View File

@ -1 +1 @@
1593768639 My_first_article_on_this_site 1594048366 My_first_article_on_this_site

View File

@ -0,0 +1,6 @@
<details>
<summary title="Press to expand">
<h2>%s</h2>
</summary>
<ul>%s</ul>
</details>

View File

@ -0,0 +1,3 @@
<li>
<a href="%s"><img src="%s"></a>
</li>

View File

@ -12,11 +12,28 @@
<body> <body>
<header> <header>
<a href="/" id="logo"><img src="./logo.png" alt="logo" /></a> <a href="/" id="logo"><img src="/logo.png" alt="logo" /></a>
<a id="modern" href="/modern" <!-- <a id="modern" href="/modern"
title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> -->
</header> </header>
<h1>Work in porgress</h1> <main>
<p>Heare are some of my photos.</p>
%s
</main>
<style>
summary h2 {
display: inline;
}
details>ul img {
margin-bottom: 30px;
}
details>ul li {
list-style-type: none;
display: inline;
}
</style>
</body> </body>
</html> </html>

View File

@ -13,8 +13,8 @@
<body> <body>
<header> <header>
<a href="/" id="logo"><img src="/logo.png" alt="logo" /></a> <a href="/" id="logo"><img src="/logo.png" alt="logo" /></a>
<a id="modern" href="/modern" <!-- <a id="modern" href="/modern"
title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> title="If you have modern browser, you can visit more fancy version of my site">Modernize</a> -->
</header> </header>
<main> <main>
<h1>Home</h1> <h1>Home</h1>
@ -22,7 +22,6 @@
<li><a href="/about">About me</a></li> <li><a href="/about">About me</a></li>
<li><a href="/gallery">Gallery</a></li> <li><a href="/gallery">Gallery</a></li>
<li><a href="/projects">Projects</a></li> <li><a href="/projects">Projects</a></li>
<li><a href="/philosophy">Philosophy</a></li>
</ul> </ul>
<h1>Articles</h1> <h1>Articles</h1>
%s %s

View File

@ -34,7 +34,7 @@ header img {
height: 100%; height: 100%;
} }
header #modern { /* header #modern {
line-height: 65px; line-height: 65px;
text-align: center; text-align: center;
vertical-align: top; vertical-align: top;
@ -46,7 +46,7 @@ header #modern {
header #modern:hover header #modern:hover
{ {
text-decoration: none; text-decoration: none;
} } */
main { main {
max-width: 800px; max-width: 800px;