Mercurial > paster
comparison http.c @ 25:f4e8a7920b94
pasterd: implement search, closes #2474 @30m
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 07 Feb 2020 13:23:48 +0100 |
parents | 8d274b012d28 |
children | 48834441dc86 |
comparison
equal
deleted
inserted
replaced
24:6702a87420d1 | 25:f4e8a7920b94 |
---|---|
41 static void page_index(struct kreq *); | 41 static void page_index(struct kreq *); |
42 static void page_new(struct kreq *); | 42 static void page_new(struct kreq *); |
43 static void page_fork(struct kreq *); | 43 static void page_fork(struct kreq *); |
44 static void page_paste(struct kreq *); | 44 static void page_paste(struct kreq *); |
45 static void page_download(struct kreq *); | 45 static void page_download(struct kreq *); |
46 static void page_search(struct kreq *); | |
46 static void page_static(struct kreq *); | 47 static void page_static(struct kreq *); |
47 | 48 |
48 enum page { | 49 enum page { |
49 PAGE_INDEX, | 50 PAGE_INDEX, |
50 PAGE_NEW, | 51 PAGE_NEW, |
51 PAGE_FORK, | 52 PAGE_FORK, |
52 PAGE_PASTE, | 53 PAGE_PASTE, |
53 PAGE_DOWNLOAD, | 54 PAGE_DOWNLOAD, |
55 PAGE_SEARCH, | |
54 PAGE_STATIC, | 56 PAGE_STATIC, |
55 PAGE_LAST /* Not used. */ | 57 PAGE_LAST /* Not used. */ |
56 }; | 58 }; |
57 | 59 |
58 static const char *pages[] = { | 60 static const char *pages[] = { |
59 [PAGE_INDEX] = "", | 61 [PAGE_INDEX] = "", |
60 [PAGE_NEW] = "new", | 62 [PAGE_NEW] = "new", |
61 [PAGE_FORK] = "fork", | 63 [PAGE_FORK] = "fork", |
62 [PAGE_PASTE] = "paste", | 64 [PAGE_PASTE] = "paste", |
63 [PAGE_DOWNLOAD] = "download", | 65 [PAGE_DOWNLOAD] = "download", |
66 [PAGE_SEARCH] = "search", | |
64 [PAGE_STATIC] = "static" | 67 [PAGE_STATIC] = "static" |
65 }; | 68 }; |
66 | 69 |
67 static void (*handlers[])(struct kreq *req) = { | 70 static void (*handlers[])(struct kreq *req) = { |
68 [PAGE_INDEX] = page_index, | 71 [PAGE_INDEX] = page_index, |
69 [PAGE_NEW] = page_new, | 72 [PAGE_NEW] = page_new, |
70 [PAGE_FORK] = page_fork, | 73 [PAGE_FORK] = page_fork, |
71 [PAGE_PASTE] = page_paste, | 74 [PAGE_PASTE] = page_paste, |
72 [PAGE_DOWNLOAD] = page_download, | 75 [PAGE_DOWNLOAD] = page_download, |
76 [PAGE_SEARCH] = page_search, | |
73 [PAGE_STATIC] = page_static | 77 [PAGE_STATIC] = page_static |
74 }; | 78 }; |
75 | 79 |
76 struct tmpl_index { | 80 struct tmpl_index { |
77 struct kreq *req; | 81 struct kreq *req; |
787 break; | 791 break; |
788 } | 792 } |
789 } | 793 } |
790 | 794 |
791 static void | 795 static void |
796 page_search_get(struct kreq *req) | |
797 { | |
798 /* We re-use the /new form with an empty paste. */ | |
799 struct tmpl_paste data = { | |
800 .req = req | |
801 }; | |
802 const struct ktemplate kt = { | |
803 .key = tmpl_new_keywords, | |
804 .keysz = 6, | |
805 .cb = tmpl_new, | |
806 .arg = &data | |
807 }; | |
808 | |
809 page(req, &kt, KHTTP_200, "search.html"); | |
810 } | |
811 | |
812 static void | |
813 page_search_post(struct kreq *req) | |
814 { | |
815 struct tmpl_index data = { | |
816 .req = req, | |
817 .count = 10 | |
818 }; | |
819 | |
820 const char *title = NULL; | |
821 const char *author = NULL; | |
822 const char *language = NULL; | |
823 | |
824 for (size_t i = 0; i < req->fieldsz; ++i) { | |
825 const char *key = req->fields[i].key; | |
826 const char *val = req->fields[i].val; | |
827 | |
828 if (strcmp(key, "title") == 0) | |
829 title = val; | |
830 else if (strcmp(key, "author") == 0) | |
831 author = val; | |
832 else if (strcmp(key, "language") == 0) | |
833 language = val; | |
834 } | |
835 | |
836 /* Sets to null if they are empty. */ | |
837 if (title && strlen(title) == 0) | |
838 title = NULL; | |
839 if (author && strlen(author) == 0) | |
840 author = NULL; | |
841 if (language && strlen(language) == 0) | |
842 language = NULL; | |
843 | |
844 if (!database_search(data.pastes, &data.count, title, author, language)) | |
845 page(req, NULL, KHTTP_500, "500.html"); | |
846 else { | |
847 struct ktemplate kt = { | |
848 .key = tmpl_index_keywords, | |
849 .keysz = 1, | |
850 .arg = &data, | |
851 .cb = tmpl_index | |
852 }; | |
853 | |
854 page(req, &kt, KHTTP_200, "index.html"); | |
855 } | |
856 | |
857 for (size_t i = 0; i < data.count; ++i) | |
858 paste_finish(&data.pastes[i]); | |
859 } | |
860 | |
861 static void | |
862 page_search(struct kreq *req) | |
863 { | |
864 switch (req->method) { | |
865 case KMETHOD_GET: | |
866 page_search_get(req); | |
867 break; | |
868 case KMETHOD_POST: | |
869 page_search_post(req); | |
870 break; | |
871 default: | |
872 page(req, NULL, KHTTP_400, "400.html"); | |
873 break; | |
874 } | |
875 } | |
876 | |
877 static void | |
792 page_static_get(struct kreq *req) | 878 page_static_get(struct kreq *req) |
793 { | 879 { |
794 struct stat st; | 880 struct stat st; |
795 char path[PATH_MAX]; | 881 char path[PATH_MAX]; |
796 | 882 |