comparison http.c @ 3:80d67d8dbfb2

pasterd: implement /download
author David Demelier <markand@malikania.fr>
date Wed, 05 Feb 2020 09:19:12 +0100
parents 65607ae124b1
children ca5b8abc2d44
comparison
equal deleted inserted replaced
2:65607ae124b1 3:80d67d8dbfb2
299 299
300 return path; 300 return path;
301 } 301 }
302 302
303 static void 303 static void
304 header(struct kreq *req) 304 page_header(struct kreq *req)
305 { 305 {
306 khttp_template(req, NULL, template("header.html")); 306 khttp_template(req, NULL, template("header.html"));
307 } 307 }
308 308
309 static void 309 static void
310 footer(struct kreq *req) 310 page_footer(struct kreq *req)
311 { 311 {
312 khttp_template(req, NULL, template("footer.html")); 312 khttp_template(req, NULL, template("footer.html"));
313 }
314
315 static void
316 page_error(struct kreq *req, int httpcode, const char *filename)
317 {
318 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[httpcode]);
319 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]);
320 khttp_body(req);
321 page_header(req);
322 khttp_template(req, NULL, template(filename));
323 page_footer(req);
313 } 324 }
314 325
315 static long long int 326 static long long int
316 duration(const char *val) 327 duration(const char *val)
317 { 328 {
472 .cb = tmpl_index 483 .cb = tmpl_index
473 }; 484 };
474 485
475 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); 486 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
476 khttp_body(req); 487 khttp_body(req);
477 header(req); 488 page_header(req);
478 khttp_template(req, &kt, template("index.html")); 489 khttp_template(req, &kt, template("index.html"));
479 footer(req); 490 page_footer(req);
480 } 491 }
481 492
482 for (size_t i = 0; i < data.count; ++i) 493 for (size_t i = 0; i < data.count; ++i)
483 paste_finish(&data.pastes[i]); 494 paste_finish(&data.pastes[i]);
484 495
499 }; 510 };
500 511
501 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); 512 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]);
502 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); 513 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
503 khttp_body(req); 514 khttp_body(req);
504 header(req); 515 page_header(req);
505 khttp_template(req, &kt, template("new.html")); 516 khttp_template(req, &kt, template("new.html"));
506 footer(req); 517 page_footer(req);
507 khttp_free(req); 518 khttp_free(req);
508 } 519 }
509 520
510 static void 521 static void
511 page_new_post(struct kreq *req) 522 page_new_post(struct kreq *req)
535 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]); 546 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]);
536 547
537 if (!paste.title || !paste.author || !paste.language || !paste.code) { 548 if (!paste.title || !paste.author || !paste.language || !paste.code) {
538 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_400]); 549 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_400]);
539 khttp_body(req); 550 khttp_body(req);
540 header(req); 551 page_header(req);
541 khttp_template(req, NULL, template("400.html")); 552 khttp_template(req, NULL, template("400.html"));
542 footer(req); 553 page_footer(req);
543 } else { 554 } else {
544 if (!database_insert(&paste)) { 555 if (!database_insert(&paste)) {
545 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_500]); 556 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_500]);
546 khttp_body(req); 557 khttp_body(req);
547 header(req); 558 page_header(req);
548 khttp_template(req, NULL, template("500.html")); 559 khttp_template(req, NULL, template("500.html"));
549 footer(req); 560 page_footer(req);
550 } else { 561 } else {
551 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]); 562 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]);
552 khttp_head(req, kresps[KRESP_LOCATION], "/paste/%s", paste.uuid); 563 khttp_head(req, kresps[KRESP_LOCATION], "/paste/%s", paste.uuid);
553 } 564 }
554 } 565 }
599 .arg = &data 610 .arg = &data
600 }; 611 };
601 612
602 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); 613 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
603 khttp_body(req); 614 khttp_body(req);
604 header(req); 615 page_header(req);
605 khttp_template(req, &kt, template("paste.html")); 616 khttp_template(req, &kt, template("paste.html"));
606 footer(req); 617 page_footer(req);
607 } 618 }
608 619
609 khttp_free(req); 620 khttp_free(req);
610 paste_finish(&data.paste); 621 paste_finish(&data.paste);
611 } 622 }
617 } 628 }
618 629
619 static void 630 static void
620 page_download(struct kreq *req) 631 page_download(struct kreq *req)
621 { 632 {
622 (void)req; 633 if (req->method != KMETHOD_GET)
634 return;
635
636 struct paste paste;
637
638 if (!database_get(&paste, req->path))
639 page_error(req, KHTTP_404, "404.html");
640 else {
641 khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_OCTET_STREAM]);
642 #if 0
643 /* TODO: this seems to generated truncated files. */
644 khttp_head(req, kresps[KRESP_CONTENT_LENGTH], "%zu", strlen(paste.code));
645 #endif
646 khttp_head(req, kresps[KRESP_CONNECTION], "keep-alive");
647 khttp_head(req, kresps[KRESP_CONTENT_DISPOSITION],
648 "attachment; filename=\"%s.%s\"", paste.uuid, paste.language);
649 khttp_body(req);
650 khttp_puts(req, paste.code);
651 paste_finish(&paste);
652 }
653
654 khttp_free(req);
623 } 655 }
624 656
625 static void 657 static void
626 process(struct kreq *req) 658 process(struct kreq *req)
627 { 659 {