comparison http.c @ 5:f455893bf0b0

pasterd: show dates in /
author David Demelier <markand@malikania.fr>
date Wed, 05 Feb 2020 13:15:55 +0100
parents ca5b8abc2d44
children 39defd15564e
comparison
equal deleted inserted replaced
4:ca5b8abc2d44 5:f455893bf0b0
37 37
38 static void page_index(struct kreq *); 38 static void page_index(struct kreq *);
39 static void page_new(struct kreq *); 39 static void page_new(struct kreq *);
40 static void page_fork(struct kreq *); 40 static void page_fork(struct kreq *);
41 static void page_paste(struct kreq *); 41 static void page_paste(struct kreq *);
42 static void page_about(struct kreq *);
43 static void page_download(struct kreq *); 42 static void page_download(struct kreq *);
44 43
45 enum page { 44 enum page {
46 PAGE_INDEX, 45 PAGE_INDEX,
47 PAGE_NEW, 46 PAGE_NEW,
48 PAGE_FORK, 47 PAGE_FORK,
49 PAGE_PASTE, 48 PAGE_PASTE,
50 PAGE_ABOUT,
51 PAGE_DOWNLOAD, 49 PAGE_DOWNLOAD,
52 PAGE_LAST /* Not used. */ 50 PAGE_LAST /* Not used. */
53 }; 51 };
54 52
55 static const char *pages[] = { 53 static const char *pages[] = {
56 [PAGE_INDEX] = "", 54 [PAGE_INDEX] = "",
57 [PAGE_NEW] = "new", 55 [PAGE_NEW] = "new",
58 [PAGE_FORK] = "fork", 56 [PAGE_FORK] = "fork",
59 [PAGE_PASTE] = "paste", 57 [PAGE_PASTE] = "paste",
60 [PAGE_ABOUT] = "about",
61 [PAGE_DOWNLOAD] = "download", 58 [PAGE_DOWNLOAD] = "download",
62 }; 59 };
63 60
64 static void (*handlers[])(struct kreq *req) = { 61 static void (*handlers[])(struct kreq *req) = {
65 [PAGE_INDEX] = page_index, 62 [PAGE_INDEX] = page_index,
66 [PAGE_NEW] = page_new, 63 [PAGE_NEW] = page_new,
67 [PAGE_FORK] = page_fork, 64 [PAGE_FORK] = page_fork,
68 [PAGE_PASTE] = page_paste, 65 [PAGE_PASTE] = page_paste,
69 [PAGE_ABOUT] = page_about,
70 [PAGE_DOWNLOAD] = page_download 66 [PAGE_DOWNLOAD] = page_download
71 }; 67 };
72 68
73 struct tmpl_index { 69 struct tmpl_index {
74 struct kreq *req; 70 struct kreq *req;
89 static const char *tmpl_index_pastes_keywords[] = { 85 static const char *tmpl_index_pastes_keywords[] = {
90 "uuid", 86 "uuid",
91 "name", 87 "name",
92 "author", 88 "author",
93 "language", 89 "language",
94 "expiration" 90 "expiration",
91 "date"
95 }; 92 };
96 93
97 static const char *tmpl_paste_keywords[] = { 94 static const char *tmpl_paste_keywords[] = {
98 "uuid", 95 "uuid",
99 "title", 96 "title",
111 "code", /* /fork only */ 108 "code", /* /fork only */
112 "languages" 109 "languages"
113 }; 110 };
114 111
115 static const char *languages[] = { 112 static const char *languages[] = {
113 "nohighlight"
116 "1c", 114 "1c",
117 "abnf", 115 "abnf",
118 "accesslog", 116 "accesslog",
119 "actionscript", 117 "actionscript",
120 "ada", 118 "ada",
222 "moonscript", 220 "moonscript",
223 "n1ql", 221 "n1ql",
224 "nginx", 222 "nginx",
225 "nimrod", 223 "nimrod",
226 "nix", 224 "nix",
227 "nohighlight",
228 "nsis", 225 "nsis",
229 "objectivec", 226 "objectivec",
230 "ocaml", 227 "ocaml",
231 "openscad", 228 "openscad",
232 "oxygene", 229 "oxygene",
341 break; 338 break;
342 case 4: 339 case 4:
343 khttp_puts(data->req, paste->code); 340 khttp_puts(data->req, paste->code);
344 break; 341 break;
345 case 5: 342 case 5:
346 /* TODO: timestamp here. */ 343 khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp)));
347 khttp_puts(data->req, "TODO");
348 break; 344 break;
349 case 6: 345 case 6:
350 khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No")); 346 khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No"));
351 break; 347 break;
352 case 7: 348 case 7:
379 case 3: 375 case 3:
380 khttp_puts(data->req, paste->language); 376 khttp_puts(data->req, paste->language);
381 break; 377 break;
382 case 4: 378 case 4:
383 khttp_puts(data->req, bprintf("%d", paste->duration)); 379 khttp_puts(data->req, bprintf("%d", paste->duration));
380 break;
381 case 5:
382 khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp)));
384 break; 383 break;
385 default: 384 default:
386 break; 385 break;
387 } 386 }
388 387
394 { 393 {
395 /* No check, only one index. */ 394 /* No check, only one index. */
396 struct tmpl_index *data = arg; 395 struct tmpl_index *data = arg;
397 const struct ktemplate kt = { 396 const struct ktemplate kt = {
398 .key = tmpl_index_pastes_keywords, 397 .key = tmpl_index_pastes_keywords,
399 .keysz = 5, 398 .keysz = 6,
400 .arg = data, 399 .arg = data,
401 .cb = tmpl_index_pastes 400 .cb = tmpl_index_pastes
402 }; 401 };
403 402
404 for (size_t i = 0; i < data->count; ++i) { 403 for (size_t i = 0; i < data->count; ++i) {
464 page_footer(req); 463 page_footer(req);
465 khttp_free(req); 464 khttp_free(req);
466 } 465 }
467 466
468 static void 467 static void
469 page_index(struct kreq *req) 468 page_index_get(struct kreq *req)
470 { 469 {
471 struct tmpl_index data = { 470 struct tmpl_index data = {
472 .req = req, 471 .req = req,
473 .count = 10 472 .count = 10
474 }; 473 };
486 page(req, &kt, KHTTP_200, "index.html"); 485 page(req, &kt, KHTTP_200, "index.html");
487 } 486 }
488 487
489 for (size_t i = 0; i < data.count; ++i) 488 for (size_t i = 0; i < data.count; ++i)
490 paste_finish(&data.pastes[i]); 489 paste_finish(&data.pastes[i]);
490 }
491
492 static void
493 page_index(struct kreq *req)
494 {
495 switch (req->method) {
496 case KMETHOD_GET:
497 page_index_get(req);
498 break;
499 default:
500 page(req, NULL, KHTTP_400, "400.html");
501 break;
502 }
491 } 503 }
492 504
493 static void 505 static void
494 page_new_get(struct kreq *req) 506 page_new_get(struct kreq *req)
495 { 507 {
562 break; 574 break;
563 } 575 }
564 } 576 }
565 577
566 static void 578 static void
579 page_fork_get(struct kreq *req)
580 {
581 struct tmpl_paste data = {
582 .req = req
583 };
584
585 if (!database_get(&data.paste, req->path))
586 page(req, NULL, KHTTP_404, "404.html");
587 else {
588 const struct ktemplate kt = {
589 .key = tmpl_new_keywords,
590 .keysz = 4,
591 .cb = tmpl_new,
592 .arg = &data
593 };
594
595 page(req, &kt, KHTTP_200, "new.html");
596 paste_finish(&data.paste);
597 }
598 }
599
600 static void
567 page_fork(struct kreq *req) 601 page_fork(struct kreq *req)
568 { 602 {
569 (void)req; 603 switch (req->method) {
570 } 604 case KMETHOD_GET:
571 605 page_fork_get(req);
572 static void 606 break;
573 page_paste(struct kreq *req) 607 default:
608 page(req, NULL, KHTTP_400, "400.html");
609 break;
610 }
611 }
612
613 static void
614 page_paste_get(struct kreq *req)
574 { 615 {
575 struct tmpl_paste data = { 616 struct tmpl_paste data = {
576 .req = req 617 .req = req
577 }; 618 };
578 619
590 paste_finish(&data.paste); 631 paste_finish(&data.paste);
591 } 632 }
592 } 633 }
593 634
594 static void 635 static void
595 page_about(struct kreq *req) 636 page_paste(struct kreq *req)
596 { 637 {
597 (void)req; 638 switch (req->method) {
598 } 639 case KMETHOD_GET:
599 640 page_paste_get(req);
600 static void 641 break;
601 page_download(struct kreq *req) 642 default:
602 { 643 page(req, NULL, KHTTP_400, "400.html");
603 if (req->method != KMETHOD_GET) 644 break;
604 return; 645 }
605 646 }
647
648 static void
649 page_download_get(struct kreq *req)
650 {
606 struct paste paste; 651 struct paste paste;
607 652
608 if (!database_get(&paste, req->path)) 653 if (!database_get(&paste, req->path))
609 page(req, NULL, KHTTP_404, "404.html"); 654 page(req, NULL, KHTTP_404, "404.html");
610 else { 655 else {
622 paste_finish(&paste); 667 paste_finish(&paste);
623 } 668 }
624 } 669 }
625 670
626 static void 671 static void
672 page_download(struct kreq *req)
673 {
674 switch (req->method) {
675 case KMETHOD_GET:
676 page_download_get(req);
677 break;
678 default:
679 page(req, NULL, KHTTP_400, "400.html");
680 break;
681 }
682 }
683
684 static void
627 process(struct kreq *req) 685 process(struct kreq *req)
628 { 686 {
629 assert(req); 687 assert(req);
630 688
631 handlers[req->page](req); 689 handlers[req->page](req);