diff --git a/include/articles_op/article.h b/include/articles_op/article.h index c6bc63e..6a7b6bd 100644 --- a/include/articles_op/article.h +++ b/include/articles_op/article.h @@ -26,4 +26,7 @@ typedef struct #endif +int list_articles(article_info **articles); +long get_article_contents(article_info *article); + #endif \ No newline at end of file diff --git a/include/articles_op/html.h b/include/articles_op/html.h new file mode 100644 index 0000000..28139c3 --- /dev/null +++ b/include/articles_op/html.h @@ -0,0 +1,23 @@ +#ifndef _HTML_H +#define _HTML_H + +#include +#include +#include + +#ifndef _ARTICLE_INFO +#define _ARTICLE_INFO + +typedef struct +{ + char *title; + long time; + char *content; + unsigned long length; +} article_info; +#endif + +int gen_html_article(article_info article, char **out); +int gen_html_article_list(article_info *articles, int n, char **out); + +#endif \ No newline at end of file diff --git a/include/netw_op/request.h b/include/netw_op/request.h index acc8b1d..b7eb1a5 100644 --- a/include/netw_op/request.h +++ b/include/netw_op/request.h @@ -11,8 +11,6 @@ #include #include -#define FILE_SIZE 1024 - struct header_s { char * str; size_t size; diff --git a/src/articles_op/article.c b/src/articles_op/article.c index 3a8cd4e..39dda7a 100644 --- a/src/articles_op/article.c +++ b/src/articles_op/article.c @@ -2,8 +2,7 @@ int list_articles(article_info **articles) { - // FILE *file = fopen("./static/articles_list.db", "r"); - FILE *file = fopen("./test_.db", "r"); + FILE *file = fopen("./static/articles_list.db", "r"); if (file == NULL) { perror("Couldn't open db file"); @@ -70,8 +69,7 @@ long get_article_contents(article_info *article) snprintf(name, line_length, "%ld", article->time); } - // snprintf(NULL, 0, "./static/articles/%s/%s.md", article->title, article->title); - int line_length = snprintf(NULL, 0, "./test_articles/%s/%s.md", name, name) + 1; + int line_length = snprintf(NULL, 0, "./static/articles/%s/%s.md", name, name) + 1; char *path = malloc(line_length); if (path == NULL) { @@ -79,8 +77,7 @@ long get_article_contents(article_info *article) return -1; } - // snprintf(path, line_length, "./static/articles/%s/%s.md", article->title, article->title); - snprintf(path, line_length, "./test_articles/%s/%s.md", name, name); + snprintf(path, line_length, "./static/articles/%s/%s.md", name, name); FILE *file = fopen(path, "r"); if (file == NULL) @@ -102,62 +99,24 @@ long get_article_contents(article_info *article) return article->length; } -int gen_html_article(article_info article, char **out) -{ - // FILE *template = fopen("./static/article.html", "r"); - FILE *template = fopen("./test_article.html", "r"); - if (template == NULL) - { - perror("Couldn't open article template"); - *out = "500"; - return -1; - } +// /* Only for testing purposes */ +// int main() +// { +// article_info *articles = malloc(0); - size_t template_size = get_file_size(template); +// int n = list_articles(&articles); - char *template_str = malloc(template_size + 1); +// printf("Read %d lines\n", n); +// printf("Last article's creation date is %ld and title is %s\n", articles[n - 1].time, articles[n - 1].title); - for (int i = 0; i < template_size; i++) - template_str[i] = fgetc(template); - template_str[template_size] = '\0'; +// for (int i = 0; i < n; i++) +// get_article_contents(&(articles[i])); - *out = malloc(template_size + article.length + 1); +// printf("Got content of %d files\n", n); - char *content; +// char **html = malloc(sizeof(char *) * n); +// for (int i = 0; i < n; i++) +// gen_html_article(articles[i], &(html[i])); - process_md(article, &content); - - int line_length = snprintf(NULL, 0, template_str, article.title, content) + 1; - *out = malloc(line_length); - if (*out == NULL) - { - *out = "500"; - return -1; - } - - snprintf(*out, line_length, template_str, article.title, content); - - return line_length; -} - -/* Only for testing purposes */ -int main() -{ - article_info *articles = malloc(0); - - int n = list_articles(&articles); - - printf("Read %d lines\n", n); - printf("Last article's creation date is %ld and title is %s\n", articles[n - 1].time, articles[n - 1].title); - - for (int i = 0; i < n; i++) - get_article_contents(&(articles[i])); - - printf("Got content of %d files\n", n); - - char **html = malloc(sizeof(char *) * n); - for (int i = 0; i < n; i++) - gen_html_article(articles[i], &(html[i])); - - return 0; -} \ No newline at end of file +// return 0; +// } \ No newline at end of file diff --git a/src/articles_op/html.c b/src/articles_op/html.c new file mode 100644 index 0000000..4f747f2 --- /dev/null +++ b/src/articles_op/html.c @@ -0,0 +1,100 @@ +#include "../../include/articles_op/html.h" +#include "../../include/articles_op/article.h" + +int gen_html_article(article_info article, char **out) +{ + FILE *template = fopen("./static/articles/index.html", "r"); + if (template == NULL) + { + perror("Couldn't open article template"); + *out = "500"; + return -1; + } + + size_t template_size = get_file_size(template); + + char *template_str = malloc(template_size + 1); + + for (int i = 0; i < template_size; i++) + template_str[i] = fgetc(template); + template_str[template_size] = '\0'; + + *out = malloc(template_size + article.length + 1); + + char *content; + + process_md(article, &content); + + int line_length = snprintf(NULL, 0, template_str, article.title, content) + 1; + *out = malloc(line_length); + if (*out == NULL) + { + *out = "500"; + return -1; + } + + snprintf(*out, line_length, template_str, article.title, content); + + free(template_str); + + return line_length; +} + +int gen_html_article_list(article_info *articles, int n, char **out) +{ + if (n == 0) + { + *out = "No articles found"; + return 0; + } + + *out = strdup("
    \n"); + char *insert; + + for (int i = 0; i < n; i++) + { + int line_length = snprintf(NULL, 0, "
  • %s
  • \n", i, articles[i].title) + 1; + + insert = malloc(line_length); + *out = realloc(*out, strlen(*out) + line_length); + if (insert == NULL || *out == NULL) + { + *out = "500 Memory error"; + return -1; + } + + snprintf(insert, line_length, "
  • %s
  • \n", i, articles[i].title); + + strcat(*out, insert); + } + + *out = realloc(*out, strlen(*out) + strlen("
") + 1); + strcat(*out, ""); + + free(insert); + + return 0; +} + +// /* Only for testing purposes */ +// int main () +// { +// article_info *articles = malloc(0); + +// int n = list_articles(&articles); + +// printf("Read %d lines\n", n); +// printf("Last article's creation date is %ld and title is %s\n", articles[n - 1].time, articles[n - 1].title); + +// for (int i = 0; i < n; i++) +// get_article_contents(&(articles[i])); + +// printf("Got content of %d files\n", n); + +// char *html; +// gen_html_article_list(articles, n, &html); + +// printf("-%s-\n", html); + +// return 0; +// } \ No newline at end of file diff --git a/src/articles_op/process_md.c b/src/articles_op/process_md.c index 57eb0aa..2f2b891 100644 --- a/src/articles_op/process_md.c +++ b/src/articles_op/process_md.c @@ -202,7 +202,7 @@ int process_md(article_info article, char **out) snprintf(append, append_line_length, "\"%s\" ", src, internal_text); strcat(*out, append); - i += i + 2 + n + 2 + k + 1; + i += 2 + n + 2 + k + 1; free(append); free(src); @@ -223,7 +223,7 @@ int process_md(article_info article, char **out) int k = 0; while (buff[i + 1 + n + 2 + k] != ')') k++; - + char *internal_text = malloc(n + 1); memcpy(internal_text, buff + i + 1, n); internal_text[n] = '\0'; @@ -245,12 +245,14 @@ int process_md(article_info article, char **out) snprintf(append, append_line_length, "%s ", href, internal_text); strcat(*out, append); - i += i + 1 + n + 2 + k + 1; + i += 1 + n + 2 + k + 1; free(append); free(href); free(internal_text); + printf("++%d-%c++\n", i, buff[i]); + continue; } @@ -264,6 +266,8 @@ 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); @@ -290,8 +294,7 @@ int process_md(article_info article, char **out) (*out)[len + 1] = '\0'; } } - - printf("%s\n\n***\n", *out); + (*out)[strlen(*out) - 1] = '\0'; return strlen(*out); } diff --git a/src/netw_op/request.c b/src/netw_op/request.c index d3d20b5..398a83b 100644 --- a/src/netw_op/request.c +++ b/src/netw_op/request.c @@ -2,7 +2,8 @@ #include "../../include/utils_op/utils.h" #include "../../include/file_op/file.h" #include "../../include/file_op/mime.h" -#include "../../include/db_op/db_cli.h" +#include "../../include/articles_op/html.h" +#include "../../include/articles_op/article.h" /** * @brief Send 404 response @@ -34,10 +35,10 @@ void res_500(int fd) { char *msg = "Server error"; - struct header_s *header = gen_header(500, sizeof(msg), "text/plain"); + struct header_s *header = gen_header(500, strlen(msg), "text/plain"); send(fd, header->str, header->size - 1, 0); - send(fd, msg, sizeof(msg), 0); + send(fd, msg, strlen(msg), 0); close(fd); printf("500 ERROR\n"); @@ -158,17 +159,41 @@ void handle_get_request(int fd, char *request) char *path = get_path(request); printf("Client accessed path: %s\n", path); - if (strcmp(path, "/") == 0) + if (strcmp(path, "/") == 0 || strcmp(path, "/index.html") == 0) { FILE *fp = fopen("static/index.html", "r"); - char buff[FILE_SIZE], msg[FILE_SIZE * 2], articles_list_str[FILE_SIZE / 2] = {0}, blogposts_list_str[FILE_SIZE / 2] = {0}; + if (fp == NULL) + { + res_404(fd, "/index.html"); + return; + } - fread(buff, FILE_SIZE, 1, fp); + size_t file_size = get_file_size(fp) + 1; + char *template = malloc(file_size); + + fread(template, file_size, 1, fp); fclose(fp); - + article_info *articles = malloc(0); + int amount = list_articles(&articles); + if (amount < 0) + { + res_500(fd); + return; + } - sprintf(msg, buff, articles_list_str); + char *articles_list_str; + gen_html_article_list(articles, amount, &articles_list_str); + + int line_length = snprintf(NULL, 0, template, articles_list_str) + 1; + char *msg = malloc(line_length); + if (msg == NULL) + { + res_500(fd); + return; + } + + snprintf(msg, line_length, template, articles_list_str); struct header_s *header = gen_header(200, strlen(msg), "text/html"); send(fd, header->str, header->size - 1, 0); @@ -181,12 +206,49 @@ void handle_get_request(int fd, char *request) return; } - if (strncmp(path, "/blog/", strlen("/blog/")) == 0) + if (strncmp(path, "/article/", strlen("/article/")) == 0) { - char *id = (char *)malloc(strlen(path) - strlen("/blog/") + 1); - memmove(id, path + strlen("/blog/"), strlen(path) - strlen("/blog/") + 1); + char *id_str = (char *)malloc(strlen(path) - strlen("/article/") + 1); + memmove(id_str, path + strlen("/article/"), strlen(path) - strlen("/article/") + 1); - printf("Blog post id = %s\n", id); + int id = strtol(id_str, NULL, 10); + if (id < 0) + { + res_404(fd, path); + return; + } + + article_info *articles = malloc(0); + int amount = list_articles(&articles); + if (amount < 0) + { + res_500(fd); + return; + } + + if (id > amount-1) + { + res_404(fd, path); + return; + } + + get_article_contents(&(articles[id])); + + char *html; + gen_html_article(articles[id], &html); + + struct header_s *header = gen_header(200, strlen(html), "text/html"); + send(fd, header->str, header->size - 1, 0); + + send(fd, html, strlen(html), 0); + close(fd); + + printf("Sent article with id=%d\n", id); + + free(articles); + free(html); + + return; } if (send_response(fd, path) < 0) diff --git a/static/article.html b/static/articles/index.html similarity index 64% rename from static/article.html rename to static/articles/index.html index b24110d..7e0a1eb 100644 --- a/static/article.html +++ b/static/articles/index.html @@ -1,12 +1,21 @@ + %s + + - %s +
+ +
+
+ %s +
+ \ No newline at end of file diff --git a/static/articles/style.css b/static/articles/style.css new file mode 100644 index 0000000..e69de29 diff --git a/static/articles_list.db b/static/articles_list.db new file mode 100644 index 0000000..e69de29 diff --git a/static/index.html b/static/index.html index 7b74f37..d2f6abb 100644 --- a/static/index.html +++ b/static/index.html @@ -11,7 +11,7 @@
- +

Home

@@ -21,6 +21,7 @@
  • Projects
  • Philosophy
  • +

    Articles

    %s