1
0

http_page.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "http_page.h"
  2. #include "hdir.h"
  3. #include "hurl.h"
  4. #define AUTOINDEX_FILENAME_MAXLEN 50
  5. void make_http_status_page(http_status status_code, std::string& page) {
  6. char szCode[8];
  7. snprintf(szCode, sizeof(szCode), "%d ", status_code);
  8. const char* status_message = http_status_str(status_code);
  9. page += R"(<!DOCTYPE html>
  10. <html>
  11. <head>
  12. <title>)";
  13. page += szCode; page += status_message;
  14. page += R"(</title>
  15. </head>
  16. <body>
  17. <center><h1>)";
  18. page += szCode; page += status_message;
  19. page += R"(</h1></center>
  20. <hr>
  21. </body>
  22. </html>)";
  23. }
  24. void make_index_of_page(const char* dir, std::string& page, const char* url) {
  25. char c_str[1024] = {0};
  26. snprintf(c_str, sizeof(c_str), R"(<!DOCTYPE html>
  27. <html>
  28. <head>
  29. <title>Index of %s</title>
  30. </head>
  31. <body>
  32. <h1>Index of %s</h1>
  33. <hr>
  34. )", url, url);
  35. page += c_str;
  36. page += " <table border=\"0\">\n";
  37. page += R"( <tr>
  38. <th align="left" width="30%">Name</th>
  39. <th align="left" width="20%">Date</th>
  40. <th align="left" width="20%">Size</th>
  41. </tr>
  42. )";
  43. #define _ADD_TD_(page, td) \
  44. page += " <td>"; \
  45. page += td; \
  46. page += "</td>\n"; \
  47. std::list<hdir_t> dirs;
  48. listdir(dir, dirs);
  49. std::string escaped_name;
  50. for (auto& item : dirs) {
  51. if (item.name[0] == '.' && item.name[1] == '\0') continue;
  52. page += " <tr>\n";
  53. // fix CVE-2023-26146
  54. escaped_name = hv::escapeHTML(item.name);
  55. const char* filename = escaped_name.c_str();
  56. size_t len = escaped_name.size() + (item.type == 'd');
  57. // name
  58. snprintf(c_str, sizeof(c_str), "<a href=\"%s%s\">%s%s</a>",
  59. filename,
  60. item.type == 'd' ? "/" : "",
  61. len < AUTOINDEX_FILENAME_MAXLEN ? filename : std::string(filename, filename+AUTOINDEX_FILENAME_MAXLEN-4).append("...").c_str(),
  62. item.type == 'd' ? "/" : "");
  63. _ADD_TD_(page, c_str)
  64. if (strcmp(filename, "..") != 0) {
  65. // mtime
  66. struct tm* tm = localtime(&item.mtime);
  67. snprintf(c_str, sizeof(c_str), "%04d-%02d-%02d %02d:%02d:%02d",
  68. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  69. _ADD_TD_(page, c_str)
  70. // size
  71. if (item.type == 'd') {
  72. page += '-';
  73. }
  74. else {
  75. float hsize;
  76. if (item.size < 1024) {
  77. snprintf(c_str, sizeof(c_str), "%lu", (unsigned long)item.size);
  78. }
  79. else if ((hsize = item.size/1024.0f) < 1024.0f) {
  80. snprintf(c_str, sizeof(c_str), "%.1fK", hsize);
  81. }
  82. else if ((hsize /= 1024.0f) < 1024.0f) {
  83. snprintf(c_str, sizeof(c_str), "%.1fM", hsize);
  84. }
  85. else {
  86. hsize /= 1024.0f;
  87. snprintf(c_str, sizeof(c_str), "%.1fG", hsize);
  88. }
  89. _ADD_TD_(page, c_str)
  90. }
  91. }
  92. page += " </tr>\n";
  93. }
  94. #undef _ADD_TD_
  95. page += R"( </table>
  96. <hr>
  97. </body>
  98. </html>
  99. )";
  100. }