content_type.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "content_type.h"
  3. static struct {
  4. const char *format;
  5. HTTP_CONTENT_TYPE content_type;
  6. bool needs_charset;
  7. const char *options;
  8. } content_types[] = {
  9. // primary - preferred during id-to-string conversions
  10. { .format = "text/html", CT_TEXT_HTML, true },
  11. { .format = "text/plain", CT_TEXT_PLAIN, true },
  12. { .format = "text/css", CT_TEXT_CSS, true },
  13. { .format = "text/yaml", CT_TEXT_YAML, true },
  14. { .format = "text/xml", CT_TEXT_XML, true },
  15. { .format = "text/xsl", CT_TEXT_XSL, true },
  16. { .format = "application/json", CT_APPLICATION_JSON, true },
  17. { .format = "application/xml", CT_APPLICATION_XML, true },
  18. { .format = "application/javascript", CT_APPLICATION_X_JAVASCRIPT, true },
  19. { .format = "application/octet-stream", CT_APPLICATION_OCTET_STREAM, false },
  20. { .format = "image/svg+xml", CT_IMAGE_SVG_XML, false },
  21. { .format = "application/x-font-truetype", CT_APPLICATION_X_FONT_TRUETYPE, false },
  22. { .format = "application/x-font-opentype", CT_APPLICATION_X_FONT_OPENTYPE, false },
  23. { .format = "application/font-woff", CT_APPLICATION_FONT_WOFF, false },
  24. { .format = "application/font-woff2", CT_APPLICATION_FONT_WOFF2, false },
  25. { .format = "application/vnd.ms-fontobject",CT_APPLICATION_VND_MS_FONTOBJ, false },
  26. { .format = "image/png", CT_IMAGE_PNG, false },
  27. { .format = "image/jpeg", CT_IMAGE_JPG, false },
  28. { .format = "image/gif", CT_IMAGE_GIF, false },
  29. { .format = "image/x-icon", CT_IMAGE_XICON, false },
  30. { .format = "image/bmp", CT_IMAGE_BMP, false },
  31. { .format = "image/icns", CT_IMAGE_ICNS, false },
  32. { .format = "audio/mpeg", CT_AUDIO_MPEG, false },
  33. { .format = "audio/ogg", CT_AUDIO_OGG, false },
  34. { .format = "video/mp4", CT_VIDEO_MP4, false },
  35. { .format = "application/pdf", CT_APPLICATION_PDF, false },
  36. { .format = "application/zip", CT_APPLICATION_ZIP, false },
  37. { .format = "image/png", CT_IMAGE_PNG, false },
  38. // secondary - overlapping with primary
  39. { .format = "text/plain", CT_PROMETHEUS, false, "version=0.0.4" },
  40. { .format = "prometheus", CT_PROMETHEUS },
  41. { .format = "text", CT_TEXT_PLAIN },
  42. { .format = "txt", CT_TEXT_PLAIN },
  43. { .format = "json", CT_APPLICATION_JSON },
  44. { .format = "html", CT_TEXT_HTML },
  45. { .format = "xml", CT_APPLICATION_XML },
  46. // terminator
  47. { .format = NULL, CT_TEXT_PLAIN },
  48. };
  49. HTTP_CONTENT_TYPE content_type_string2id(const char *format) {
  50. if(format && *format) {
  51. for (int i = 0; content_types[i].format; i++)
  52. if (strcmp(content_types[i].format, format) == 0)
  53. return content_types[i].content_type;
  54. }
  55. return CT_TEXT_PLAIN;
  56. }
  57. const char *content_type_id2string(HTTP_CONTENT_TYPE content_type) {
  58. for (int i = 0; content_types[i].format; i++)
  59. if (content_types[i].content_type == content_type)
  60. return content_types[i].format;
  61. return "text/plain";
  62. }
  63. void http_header_content_type(BUFFER *wb, HTTP_CONTENT_TYPE content_type) {
  64. buffer_strcat(wb, "Content-Type: ");
  65. for (int i = 0; content_types[i].format; i++) {
  66. if (content_types[i].content_type == content_type) {
  67. buffer_strcat(wb, content_types[i].format);
  68. if(content_types[i].needs_charset) {
  69. buffer_strcat(wb, "; charset=utf-8");
  70. }
  71. if(content_types[i].options) {
  72. buffer_strcat(wb, "; ");
  73. buffer_strcat(wb, content_types[i].options);
  74. }
  75. buffer_strcat(wb, "\r\n");
  76. return;
  77. }
  78. }
  79. buffer_strcat(wb, "text/plain; charset=utf-8\r\n");
  80. }