comparison page-download.c @ 0:f41e1b48510d

misc: initial import
author David Demelier <markand@malikania.fr>
date Wed, 25 Nov 2020 21:13:03 +0100
parents
children f19f5f9fee56
comparison
equal deleted inserted replaced
-1:000000000000 0:f41e1b48510d
1 /*
2 * page-download.c -- page /download/<id>
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23
24 #include <kcgi.h>
25
26 #include "database.h"
27 #include "image.h"
28 #include "page.h"
29
30 static void
31 get(struct kreq *r)
32 {
33 struct image image = {0};
34
35 if (!database_get(&image, r->path))
36 page(r, NULL, KHTTP_404, "pages/404.html");
37 else {
38 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_OCTET_STREAM]);
39 khttp_head(r, kresps[KRESP_CONTENT_LENGTH], "%llu", (unsigned long long)image.datasz);
40 khttp_head(r, kresps[KRESP_CONNECTION], "keep-alive");
41 khttp_head(r, kresps[KRESP_CONTENT_DISPOSITION],
42 "attachment; filename=\"%s\"", image.filename);
43 khttp_body(r);
44 khttp_write(r, image.data, image.datasz);
45 khttp_free(r);
46 image_finish(&image);
47 }
48 }
49
50 void
51 page_download(struct kreq *r)
52 {
53 assert(r);
54
55 switch (r->method) {
56 case KMETHOD_GET:
57 get(r);
58 break;
59 default:
60 page(r, NULL, KHTTP_400, "pages/400.html");
61 break;
62 }
63 }