comparison page-search.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-search.c -- page /search
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 <stddef.h>
23 #include <stdint.h>
24 #include <string.h>
25
26 #include <kcgi.h>
27
28 #include "database.h"
29 #include "image.h"
30 #include "page-index.h"
31 #include "page-search.h"
32 #include "page.h"
33 #include "util.h"
34
35 static void
36 get(struct kreq *r)
37 {
38 page(r, NULL, KHTTP_200, "pages/search.html");
39 }
40
41 static void
42 post(struct kreq *r)
43 {
44 struct image images[10] = {0};
45 size_t imagesz = NELEM(images);
46 const char *title = NULL;
47 const char *author = NULL;
48
49 for (size_t i = 0; i < r->fieldsz; ++i) {
50 const char *key = r->fields[i].key;
51 const char *val = r->fields[i].val;
52
53 if (strcmp(key, "title") == 0)
54 title = val;
55 else if (strcmp(key, "author") == 0)
56 author = val;
57 }
58
59 /* Sets to null if they are empty. */
60 if (title && strlen(title) == 0)
61 title = NULL;
62 if (author && strlen(author) == 0)
63 author = NULL;
64
65 if (!database_search(images, &imagesz, title, author))
66 page(r, NULL, KHTTP_500, "pages/500.html");
67 else
68 page_index_render(r, images, imagesz);
69
70 for (size_t i = 0; i < imagesz; ++i)
71 image_finish(&images[i]);
72 }
73
74 void
75 page_search(struct kreq *r)
76 {
77 assert(r);
78
79 switch (r->method) {
80 case KMETHOD_GET:
81 get(r);
82 break;
83 case KMETHOD_POST:
84 post(r);
85 break;
86 default:
87 page(r, NULL, KHTTP_400, "pages/400.html");
88 break;
89 }
90 }