cms.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/mman.h>
  17. #include <sys/stat.h>
  18. #include <err.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include "buffer.h"
  25. #include "filehelper.h"
  26. #include "handler.h"
  27. #include "template.h"
  28. #ifndef CMS_CONTENT_DIR
  29. #error "Need CMS_CONTENT_DIR defined to compile"
  30. #endif
  31. #ifndef CMS_HOSTNAME
  32. #error "Need CMS_HOSTNAME defined to compile"
  33. #endif
  34. #ifndef CMS_SESSION_DIR
  35. #error "Need CMS_SESSION_Dir defined to compile"
  36. #endif
  37. char *cms_content_dir = CMS_CONTENT_DIR;
  38. char *cms_template_dir = CMS_TEMPLATE_DIR;
  39. char *cms_session_db = CMS_SESSION_DIR "/session.db";
  40. char *cms_session_htpasswd = CMS_SESSION_DIR "/htpasswd";
  41. static __dead void usage(void);
  42. __dead void
  43. usage(void)
  44. {
  45. extern char *__progname;
  46. dprintf(STDERR_FILENO, "usage: %s URI\n", __progname);
  47. exit(1);
  48. }
  49. int
  50. main(int argc, char **argv)
  51. {
  52. char *result = "";
  53. struct buffer_list *hb, *out;
  54. struct request *r;
  55. if (argc > 2)
  56. usage();
  57. if (argc >= 2) {
  58. cms_content_dir = CMS_CHROOT CMS_CONTENT_DIR;
  59. cms_template_dir = CMS_CHROOT CMS_TEMPLATE_DIR;
  60. cms_session_db = CMS_CHROOT CMS_SESSION_DIR "/session.db";
  61. cms_session_htpasswd = CMS_CHROOT CMS_SESSION_DIR "/htpasswd";
  62. if (argv[1][0] != '/')
  63. errx(1, "Require absolute path as argument");
  64. // XXX Substitude PATH_INFO env variable
  65. setenv("PATH_INFO", argv[1], 1);
  66. }
  67. char *path_info = getenv("PATH_INFO");
  68. if (path_info == NULL || strlen(path_info) == 0
  69. || strcmp(path_info, "index.html") == 0
  70. || strcmp(path_info, "/index.html") == 0)
  71. path_info = "/home.html";
  72. r = request_new(path_info);
  73. if (r == NULL)
  74. _error("404 Not Found", NULL);
  75. struct page_info *page = request_fetch_page(r);
  76. if (page == NULL)
  77. _error("404 Not Found", NULL);
  78. request_init_tmpl_data(r);
  79. request_handle_login(r);
  80. out = request_render_page(r, CMS_DEFAULT_TEMPLATE);
  81. result = buffer_list_concat_string(out);
  82. request_add_header(r, "Content-type", "application/xhtml+xml");
  83. request_add_header(r, "Status", "200 Ok");
  84. hb = request_output_headers(r);
  85. char *header = buffer_list_concat_string(hb);
  86. dprintf(STDOUT_FILENO, "%s\r\n%s", header, result);
  87. request_free(r);
  88. buffer_list_free(hb);
  89. buffer_list_free(out);
  90. return 0;
  91. }