diff http.c @ 5:f455893bf0b0

pasterd: show dates in /
author David Demelier <markand@malikania.fr>
date Wed, 05 Feb 2020 13:15:55 +0100
parents ca5b8abc2d44
children 39defd15564e
line wrap: on
line diff
--- a/http.c	Wed Feb 05 20:10:00 2020 +0100
+++ b/http.c	Wed Feb 05 13:15:55 2020 +0100
@@ -39,7 +39,6 @@
 static void page_new(struct kreq *);
 static void page_fork(struct kreq *);
 static void page_paste(struct kreq *);
-static void page_about(struct kreq *);
 static void page_download(struct kreq *);
 
 enum page {
@@ -47,7 +46,6 @@
 	PAGE_NEW,
 	PAGE_FORK,
 	PAGE_PASTE,
-	PAGE_ABOUT,
 	PAGE_DOWNLOAD,
 	PAGE_LAST       /* Not used. */
 };
@@ -57,7 +55,6 @@
 	[PAGE_NEW]      = "new",
 	[PAGE_FORK]     = "fork",
 	[PAGE_PASTE]    = "paste",
-	[PAGE_ABOUT]    = "about",
 	[PAGE_DOWNLOAD] = "download",
 };
 
@@ -66,7 +63,6 @@
 	[PAGE_NEW]      = page_new,
 	[PAGE_FORK]     = page_fork,
 	[PAGE_PASTE]    = page_paste,
-	[PAGE_ABOUT]    = page_about,
 	[PAGE_DOWNLOAD] = page_download
 };
 
@@ -91,7 +87,8 @@
 	"name",
 	"author",
 	"language",
-	"expiration"
+	"expiration",
+	"date"
 };
 
 static const char *tmpl_paste_keywords[] = {
@@ -113,6 +110,7 @@
 };
 
 static const char *languages[] = {
+	"nohighlight"
 	"1c",
 	"abnf",
 	"accesslog",
@@ -224,7 +222,6 @@
 	"nginx",
 	"nimrod",
 	"nix",
-	"nohighlight",
 	"nsis",
 	"objectivec",
 	"ocaml",
@@ -343,8 +340,7 @@
 		khttp_puts(data->req, paste->code);
 		break;
 	case 5:
-		/* TODO: timestamp here. */
-		khttp_puts(data->req, "TODO");
+		khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp)));
 		break;
 	case 6:
 		khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No"));
@@ -382,6 +378,9 @@
 	case 4:
 		khttp_puts(data->req, bprintf("%d", paste->duration));
 		break;
+	case 5:
+		khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp)));
+		break;
 	default:
 		break;
 	}
@@ -396,7 +395,7 @@
 	struct tmpl_index *data = arg;
 	const struct ktemplate kt = {
 		.key    = tmpl_index_pastes_keywords,
-		.keysz  = 5,
+		.keysz  = 6,
 		.arg    = data,
 		.cb     = tmpl_index_pastes
 	};
@@ -466,7 +465,7 @@
 }
 
 static void
-page_index(struct kreq *req)
+page_index_get(struct kreq *req)
 {
 	struct tmpl_index data = {
 		.req    = req,
@@ -491,6 +490,19 @@
 }
 
 static void
+page_index(struct kreq *req)
+{
+	switch (req->method) {
+	case KMETHOD_GET:
+		page_index_get(req);
+		break;
+	default:
+		page(req, NULL, KHTTP_400, "400.html");
+		break;
+	}
+}
+
+static void
 page_new_get(struct kreq *req)
 {
 	struct tmpl_paste data = {
@@ -564,13 +576,42 @@
 }
 
 static void
-page_fork(struct kreq *req)
+page_fork_get(struct kreq *req)
 {
-	(void)req;
+	struct tmpl_paste data = {
+		.req = req
+	};
+
+	if (!database_get(&data.paste, req->path))
+		page(req, NULL, KHTTP_404, "404.html");
+	else {
+		const struct ktemplate kt = {
+			.key    = tmpl_new_keywords,
+			.keysz  = 4,
+			.cb     = tmpl_new,
+			.arg    = &data
+		};
+
+		page(req, &kt, KHTTP_200, "new.html");
+		paste_finish(&data.paste);
+	}
 }
 
 static void
-page_paste(struct kreq *req)
+page_fork(struct kreq *req)
+{
+	switch (req->method) {
+	case KMETHOD_GET:
+		page_fork_get(req);
+		break;
+	default:
+		page(req, NULL, KHTTP_400, "400.html");
+		break;
+	}
+}
+
+static void
+page_paste_get(struct kreq *req)
 {
 	struct tmpl_paste data = {
 		.req = req
@@ -592,17 +633,21 @@
 }
 
 static void
-page_about(struct kreq *req)
+page_paste(struct kreq *req)
 {
-	(void)req;
+	switch (req->method) {
+	case KMETHOD_GET:
+		page_paste_get(req);
+		break;
+	default:
+		page(req, NULL, KHTTP_400, "400.html");
+		break;
+	}
 }
 
 static void
-page_download(struct kreq *req)
+page_download_get(struct kreq *req)
 {
-	if (req->method != KMETHOD_GET)
-		return;
-
 	struct paste paste;
 
 	if (!database_get(&paste, req->path))
@@ -624,6 +669,19 @@
 }
 
 static void
+page_download(struct kreq *req)
+{
+	switch (req->method) {
+	case KMETHOD_GET:
+		page_download_get(req);
+		break;
+	default:
+		page(req, NULL, KHTTP_400, "400.html");
+		break;
+	}
+}
+
+static void
 process(struct kreq *req)
 {
 	assert(req);