diff page-download.c @ 78:9bfe5ce3cc45

pasterd: rework themes
author David Demelier <markand@malikania.fr>
date Thu, 16 Mar 2023 20:45:59 +0100
parents 67b3d13a5035
children 52029a52a385
line wrap: on
line diff
--- a/page-download.c	Thu Mar 16 15:05:26 2023 +0100
+++ b/page-download.c	Thu Mar 16 20:45:59 2023 +0100
@@ -2,11 +2,11 @@
  * page-download.c -- page /download/<id>
  *
  * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
- * 
+ *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@@ -16,49 +16,48 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/types.h>
 #include <assert.h>
-#include <stdarg.h>
-#include <stdint.h>
-
-#include <kcgi.h>
 
 #include "database.h"
 #include "page.h"
-#include "paste.h"
+#include "json-util.h"
 
 static void
-get(struct kreq *r)
+get(struct kreq *req)
 {
-	struct paste paste = {0};
+	json_t *paste;
 
-	if (!database_get(&paste, r->path))
-		page_status(r, KHTTP_404);
+	if (!(paste = database_get(req->path)))
+		page_status(req, KHTTP_404);
 	else {
-		khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_OCTET_STREAM]);
+		khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_OCTET_STREAM]);
 #if 0
 		/* TODO: this seems to generated truncated files. */
-		khttp_head(r, kresps[KRESP_CONTENT_LENGTH], "%zu", strlen(paste.code));
+		khttp_head(req, kresps[KRESP_CONTENT_LENGTH], "%zu", strlen(paste.code));
 #endif
-		khttp_head(r, kresps[KRESP_CONNECTION], "keep-alive");
-		khttp_head(r, kresps[KRESP_CONTENT_DISPOSITION],
-		    "attachment; filename=\"%s.%s\"", paste.id, paste.language);
-		khttp_body(r);
-		khttp_puts(r, paste.code);
-		khttp_free(r);
-		paste_finish(&paste);
+		khttp_head(req, kresps[KRESP_CONNECTION], "keep-alive");
+		khttp_head(req, kresps[KRESP_CONTENT_DISPOSITION], "attachment; filename=\"%s.%s\"",
+		    ju_get_string(paste, "id"),
+		    ju_get_string(paste, "language")
+		);
+		khttp_body(req);
+		khttp_puts(req, ju_get_string(paste, "code"));
+		khttp_free(req);
+		json_decref(paste);
 	}
 }
 
 void
-page_download(struct kreq *r)
+page_download(struct kreq *req)
 {
-	switch (r->method) {
+	assert(req);
+
+	switch (req->method) {
 	case KMETHOD_GET:
-		get(r);
+		get(req);
 		break;
 	default:
-		page_status(r, KHTTP_400);
+		page_status(req, KHTTP_400);
 		break;
 	}
 }