http_page.cpp 3.0 KB

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