sitemap_cgi.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/types.h>
  17. #include <err.h>
  18. #include <stdarg.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <kcgi.h>
  24. #include "filehelper.h"
  25. #include "sitemap.h"
  26. #ifndef CMS_CONTENT_DIR
  27. #error "Need CMS_CONTENT_DIR defined to compile"
  28. #endif
  29. #ifndef CMS_HOSTNAME
  30. #error "Need CMS_HOSTNAME defined to compile"
  31. #endif
  32. enum page {
  33. PAGE_SITEMAP,
  34. PAGE_SITEMAP_GZ,
  35. PAGE__MAX
  36. };
  37. const char *const pages[PAGE__MAX] = {
  38. "sitemap", /* PAGE_SITEMAP */
  39. "sitemap.xml.gz", /* PAGE_SITEMAP_GZ */
  40. };
  41. int
  42. main(void)
  43. {
  44. size_t size;
  45. struct kreq r;
  46. char *out;
  47. if (KCGI_OK != khttp_parse(&r, NULL, 0, pages, PAGE__MAX, PAGE_SITEMAP))
  48. return EXIT_FAILURE;
  49. struct sitemap *sitemap = sitemap_new(CMS_CONTENT_DIR, CMS_HOSTNAME);
  50. uint32_t mtime = sitemap_newest(sitemap, NULL);
  51. switch (r.page) {
  52. case PAGE_SITEMAP:
  53. out = sitemap_toxml(sitemap);
  54. size = strlen(out);
  55. break;
  56. case PAGE_SITEMAP_GZ:
  57. default:
  58. out = sitemap_toxmlgz(sitemap, &size, "sitemap.xml", mtime);
  59. break;
  60. }
  61. khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
  62. khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s", "application/xml");
  63. if (r.page == PAGE_SITEMAP_GZ)
  64. khttp_head(&r, kresps[KRESP_CONTENT_ENCODING], "%s", "x-gzip");
  65. khttp_body(&r);
  66. khttp_write(&r, out, size);
  67. khttp_free(&r);
  68. free(out);
  69. return EXIT_SUCCESS;
  70. }