Mercurial > paster
comparison page-search.c @ 51:07b6887d3557
pasterd: split into individual pages
While here add a similar mini.css theme as in imgup.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 21 Dec 2020 18:22:44 +0100 |
parents | |
children | fba88439ec0a |
comparison
equal
deleted
inserted
replaced
50:520f57836ff3 | 51:07b6887d3557 |
---|---|
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 "fragment-language.h" | |
30 #include "page-index.h" | |
31 #include "page-search.h" | |
32 #include "page.h" | |
33 #include "paste.h" | |
34 #include "util.h" | |
35 | |
36 static const char *keywords[] = { | |
37 "languages" | |
38 }; | |
39 | |
40 static int | |
41 template(size_t keyword, void *arg) | |
42 { | |
43 struct kreq *r = arg; | |
44 | |
45 switch (keyword) { | |
46 case 0: | |
47 for (size_t i = 0; i < languagesz; ++i) | |
48 fragment_language(r, languages[i], false); | |
49 break; | |
50 default: | |
51 break; | |
52 } | |
53 | |
54 return 1; | |
55 } | |
56 | |
57 static void | |
58 get(struct kreq *r) | |
59 { | |
60 struct ktemplate kt = { | |
61 .key = keywords, | |
62 .keysz = NELEM(keywords), | |
63 .cb = template, | |
64 .arg = r | |
65 }; | |
66 | |
67 page(r, &kt, KHTTP_200, "pages/search.html"); | |
68 } | |
69 | |
70 static void | |
71 post(struct kreq *r) | |
72 { | |
73 struct paste pastes[10] = {0}; | |
74 size_t pastesz = NELEM(pastes); | |
75 const char *title = NULL; | |
76 const char *author = NULL; | |
77 const char *language = NULL; | |
78 | |
79 for (size_t i = 0; i < r->fieldsz; ++i) { | |
80 const char *key = r->fields[i].key; | |
81 const char *val = r->fields[i].val; | |
82 | |
83 if (strcmp(key, "title") == 0) | |
84 title = val; | |
85 else if (strcmp(key, "author") == 0) | |
86 author = val; | |
87 else if (strcmp(key, "language") == 0) | |
88 language = val; | |
89 } | |
90 | |
91 /* Sets to null if they are empty. */ | |
92 if (title && strlen(title) == 0) | |
93 title = NULL; | |
94 if (author && strlen(author) == 0) | |
95 author = NULL; | |
96 | |
97 if (!database_search(pastes, &pastesz, title, author, language)) | |
98 page(r, NULL, KHTTP_500, "pages/500.html"); | |
99 else | |
100 page_index_render(r, pastes, pastesz); | |
101 | |
102 for (size_t i = 0; i < pastesz; ++i) | |
103 paste_finish(&pastes[i]); | |
104 } | |
105 | |
106 void | |
107 page_search(struct kreq *r) | |
108 { | |
109 assert(r); | |
110 | |
111 switch (r->method) { | |
112 case KMETHOD_GET: | |
113 get(r); | |
114 break; | |
115 case KMETHOD_POST: | |
116 post(r); | |
117 break; | |
118 default: | |
119 page(r, NULL, KHTTP_400, "pages/400.html"); | |
120 break; | |
121 } | |
122 } |