diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-07-17 22:35:09 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-07-17 22:35:09 -0400 |
commit | e30980265e897e020fc137afd9465889d993d63c (patch) | |
tree | de5b84f7da886684625fed18bae8d7b89c76f570 | |
parent | 2febdaa840037d5f6d892172be29c733440cdcba (diff) |
mostly working new post, except for that crashing on write() bit
-rw-r--r-- | blog.c | 77 |
1 files changed, 50 insertions, 27 deletions
@@ -1,37 +1,72 @@ #define _XOPEN_SOURCE 700 +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> +#include <time.h> +#include <unistd.h> #include "blog.h" -extern char **environ; +#include <errno.h> int handle_post(void) { - - //printf("Status: 301 Found\r\n"); - //printf("Location: http%s://%s%s\r\n\r\n", getenv("HTTPS") ? "s" : "", getenv("HTTP_HOST"), getenv("DOCUMENT_URI")); - printf("Status: 200 OK\r\n"); printf("Content-Type: text/plain\r\n\r\n"); read_post_data(); if (!authenticate(find_post_data("username"), find_post_data("password"))) { - printf("Bad login!"); + printf("Status 403 Forbidden\r\n"); + printf("Content-Type: text/plain\r\n\r\n"); + puts("Incorrect username or password. Go back and try again."); return 0; } - for (char **e = environ; e && *e; e++) { - puts(*e); + int blogdir = open(DATA_DIRECTORY, O_DIRECTORY); + if (blogdir == -1) { + printf("open(%s): %s\n", DATA_DIRECTORY, strerror(errno)); + return 1; } - return 0; -} -int show_entry(const char *path) -{ - printf("Status: 200 OK\r\n"); - printf("Content-Type: text/plain\r\n\r\n"); - puts(path); + time_t now = time(NULL); + struct tm *tm = localtime(&now); + + char ymd[16] = { 0 }; + strftime(ymd, sizeof(ymd), "%Y", tm); + mkdirat(blogdir, ymd, 0755); + + strftime(ymd, sizeof(ymd), "%Y/%m", tm); + mkdirat(blogdir, ymd, 0755); + + strftime(ymd, sizeof(ymd), "%Y/%m/%d", tm); + mkdirat(blogdir, ymd, 0755); + + char *title = find_post_data("title"); + char uri[FILENAME_MAX] = { 0 }; + snprintf(uri, sizeof(uri), "%s/%s", ymd, title); + + int newpost = openat(blogdir, uri, O_WRONLY | O_CREAT, 0644); + if (newpost == -1) { + printf("open(%s): %s\n", uri, strerror(errno)); + return 0; + } + + char *body = find_post_data("body"); + if (body == NULL) { + puts("body is null?!"); + } + + if (write(newpost, body, strlen(body)) != strlen(body)) { + printf("write: %s\n", strerror(errno)); + return 0; + } + + close(newpost); + close(blogdir); + + printf("Status: 302 Found\r\n"); + printf("Location: http%s://%s/%s\r\n\r\n", getenv("HTTPS") ? "s" : "", getenv("HTTP_HOST"), uri); return 0; } @@ -42,11 +77,6 @@ int main(void) return handle_post(); } - char *path_info = getenv("PATH_INFO"); - if (path_info && *path_info && strcmp(path_info, "/")) { - return show_entry(path_info); - } - printf("Status: 200 OK\r\n"); printf("Content-Type: text/html\r\n\r\n"); @@ -65,13 +95,6 @@ int main(void) puts("<textarea name=\"body\" cols=\"80\" rows=\"24\"></textarea><br>"); puts("<input type=\"submit\">"); puts("</form>"); - - puts("<!--"); - for (char **e = environ; e && *e; e++) { - puts(*e); - } - puts("-->"); - puts("</body>"); puts("</html>"); |