First fully featured prototype

This commit is contained in:
thematdev 2023-07-03 20:25:23 +03:00
parent 2b1da096a6
commit fc3d99fa82
Signed by: thematdev
GPG Key ID: D12878639B090D90
3 changed files with 104 additions and 3 deletions

View File

@ -7,14 +7,22 @@
#include "drivers/unix_fs/unix_fs_driver.h"
#include <stdlib.h>
#include <time.h>
#define COMMENTS_PER_PAGE 20
#define MAX_NAME 1024
#define MAX_COMMENT_SIZE 4096
#define QMENTS_PATH "qments-storage"
#define DRIVER_DATA { QMENTS_PATH }
#define DRIVER unix_fs_driver
int
page_by_id(int id)
{
return id / COMMENTS_PER_PAGE + 1;
}
void
fct_callback(char character, void *arg)
{
@ -26,15 +34,22 @@ render_comment(const Comment *comment)
{
StringBuffer _buffer, *buffer;
char *retval;
int rid;
buffer = &_buffer;
sb_init_empty(buffer);
fctprintf(fct_callback, buffer, "<div class=\"comment\" id=comment_%d>\n", comment->id);
fctprintf(fct_callback, buffer, "<div class=\"comment\" id=\"comment_%d\">\n", comment->id);
/* begin header */
fctprintf(fct_callback, buffer, "<div class=\"comment-header\">\n");
fctprintf(fct_callback, buffer, "Posted by: %s\n", comment->header->user_displayname);
rid = comment->header->reply_id;
if (rid > 0) {
fctprintf(fct_callback, buffer, "<a href=\"?page=%d#comment_%d\"> in reply to </a>", page_by_id(rid), rid);
}
fctprintf(fct_callback, buffer, "</div>\n");
/* end header */
@ -98,9 +113,22 @@ defer:
return retval;
}
void
print_submit_form()
{
fputs("<form action=\"\">\n"
"<label>Your name:</label>\n"
"<input type=\"text\" name=\"displayname\"><br>\n"
"<label>Leave comment: </label><br>\n"
"<input type=\"text\" name=\"text\"><br>\n"
"<input type=\"submit\" name=\"submit\" value=\"Submit\">\n"
"</form>\n", cgiOut);
}
/* each page stores comments with ids in [P * (page - 1), P * page)
* if page is not specified, then we'll just print last P comments (for now)
* TODO: print errors in div
* FIXME: NOW PAGING IS FAULTY, FIX IT
*/
void
print_page()
@ -147,6 +175,65 @@ print_page()
}
}
/* TODO: restore fields for user no to lose data */
int
handle_submitted_comment()
{
char displayname[MAX_NAME], text[MAX_COMMENT_SIZE], *sanitized_text;
int rid, retval;
cgiFormResultType err;
Driver driver = DRIVER;
UnixFsDriverData driver_data = DRIVER_DATA;
err = cgiFormString("text", text, MAX_COMMENT_SIZE);
if (err == cgiFormTruncated) {
fprintf(cgiOut, "Comment too long (max %d bytes)\n", MAX_COMMENT_SIZE);
goto defer;
}
if (err == cgiFormNotFound) {
fprintf(cgiOut, "Comment text not provided\n");
goto defer;
}
err = cgiFormString("displayname", displayname, MAX_NAME);
if (err == cgiFormTruncated) {
fprintf(cgiOut, "Name too long (max %d bytes)\n", MAX_NAME);
goto defer;
}
if (err == cgiFormNotFound) {
fprintf(cgiOut, "Name not provided\n");
goto defer;
}
if (contain_special(displayname)) {
err = cgiFormTruncated;
fprintf(cgiOut, "Name must not contain HTML special characters\n");
goto defer;
}
cgiFormInteger("reply-to", &rid, 0);
sanitized_text = mk_specialchars(text);
CommentHeader header;
time(&header.creation_time);
header.reply_id = rid;
header.text_length = strlen(sanitized_text);
header.user_sid = "anonymous";
header.user_displayname = displayname;
if (driver.leave_comment(&driver_data, &header, sanitized_text) < 0) {
fprintf(cgiOut, "Failed to leave your comment\n");
} else {
fprintf(cgiOut, "Comment successfully left\n");
}
free(sanitized_text);
defer:
return (err == cgiFormSuccess ? 0 : -1);
}
int
cgiMain()
{
@ -155,12 +242,16 @@ cgiMain()
fprintf(cgiOut, "<html>\n");
fprintf(cgiOut, "<head>\n");
fprintf(cgiOut, "<title> Simple discuss powered by qments\n");
fprintf(cgiOut, "<title> Simple discuss powered by qments </title>\n");
fprintf(cgiOut, "</head>\n");
fprintf(cgiOut, "<body>\n");
if (cgiFormSubmitClicked("submit") == cgiFormSuccess) {
handle_submitted_comment();
}
/* print_submit_form(); */
print_submit_form();
fprintf(cgiOut, "<div class=\"comment-section\">\n");
print_page();

View File

@ -1,7 +1,15 @@
#include <stdlib.h>
#include <string.h>
#include "string_buffer.h"
#include "utils.h"
int
contain_special(const char *s)
{
return strchr(s, '&') || strchr(s, '"') || strchr(s, '\'') || strchr(s, '<') || strchr(s, '>');
}
char *
mk_specialchars(const char *input)
{

View File

@ -1,6 +1,8 @@
#ifndef UTILS_H
#define UTILS_H
int contain_special(const char *s);
char *mk_specialchars(const char *input);
#endif /* UTILS_H */