cms.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <sys/mman.h>
  2. #include <sys/stat.h>
  3. #include <err.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "buffer.h"
  10. #include "filehelper.h"
  11. #include "handler.h"
  12. #include "template.h"
  13. #ifndef CMS_CONTENT_DIR
  14. #error "Need CMS_CONTENT_DIR defined to compile"
  15. #endif
  16. #ifndef CMS_HOSTNAME
  17. #error "Need CMS_HOSTNAME defined to compile"
  18. #endif
  19. #ifndef CMS_SESSION_DIR
  20. #error "Need CMS_SESSION_Dir defined to compile"
  21. #endif
  22. char *cms_content_dir = CMS_CONTENT_DIR;
  23. char *cms_template_dir = CMS_TEMPLATE_DIR;
  24. char *cms_session_db = CMS_SESSION_DIR "/session.db";
  25. char *cms_session_htpasswd = CMS_SESSION_DIR "/htpasswd";
  26. static __dead void usage(void);
  27. __dead void
  28. usage(void)
  29. {
  30. extern char *__progname;
  31. dprintf(STDERR_FILENO, "usage: %s URI\n", __progname);
  32. exit(1);
  33. }
  34. int
  35. main(int argc, char **argv)
  36. {
  37. char *result = "";
  38. struct buffer_list *hb, *out;
  39. struct request *r;
  40. if (argc > 2)
  41. usage();
  42. if (argc >= 2) {
  43. cms_content_dir = CMS_CHROOT CMS_CONTENT_DIR;
  44. cms_template_dir = CMS_CHROOT CMS_TEMPLATE_DIR;
  45. cms_session_db = CMS_CHROOT CMS_SESSION_DIR "/session.db";
  46. cms_session_htpasswd = CMS_CHROOT CMS_SESSION_DIR "/htpasswd";
  47. if (argv[1][0] != '/')
  48. errx(1, "Require absolute path as argument");
  49. // XXX Substitude PATH_INFO env variable
  50. setenv("PATH_INFO", argv[1], 1);
  51. }
  52. char *path_info = getenv("PATH_INFO");
  53. if (path_info == NULL || strlen(path_info) == 0
  54. || strcmp(path_info, "index.html") == 0
  55. || strcmp(path_info, "/index.html") == 0)
  56. path_info = "/home.html";
  57. r = request_new(path_info);
  58. if (r == NULL)
  59. _error("404 Not Found", NULL);
  60. struct page_info *page = request_fetch_page(r);
  61. if (page == NULL)
  62. _error("404 Not Found", NULL);
  63. request_init_tmpl_data(r);
  64. request_handle_login(r);
  65. out = request_render_page(r, CMS_DEFAULT_TEMPLATE);
  66. result = buffer_list_concat_string(out);
  67. request_add_header(r, "Content-type", "application/xhtml+xml");
  68. request_add_header(r, "Status", "200 Ok");
  69. hb = request_output_headers(r);
  70. char *header = buffer_list_concat_string(hb);
  71. dprintf(STDOUT_FILENO, "%s\r\n%s", header, result);
  72. request_free(r);
  73. buffer_list_free(hb);
  74. buffer_list_free(out);
  75. return 0;
  76. }