server_help.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "server_help.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/param.h>
  5. #include <sys/unistd.h>
  6. #include <sys/stat.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <dirent.h>
  11. #ifdef __cplusplus
  12. }
  13. #endif
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #include "Helper.h"
  17. #include "esp_http_server.h"
  18. #include "../../include/defines.h"
  19. static const char *TAG = "SERVER HELP";
  20. char scratch[SERVER_HELPER_SCRATCH_BUFSIZE];
  21. bool endsWith(std::string const &str, std::string const &suffix) {
  22. if (str.length() < suffix.length()) {
  23. return false;
  24. }
  25. return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
  26. }
  27. esp_err_t send_file(httpd_req_t *req, std::string filename)
  28. {
  29. FILE *fd = fopen(filename.c_str(), "r");
  30. if (!fd) {
  31. ESP_LOGE(TAG, "Failed to read file: %s", filename.c_str());
  32. /* Respond with 404 Error */
  33. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
  34. return ESP_FAIL;
  35. }
  36. ESP_LOGD(TAG, "Sending file: %s ...", filename.c_str());
  37. // httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  38. /* For all files with the following file extention tell
  39. the webbrowser to cache them for 24h */
  40. if (endsWith(filename, ".html") ||
  41. endsWith(filename, ".htm") ||
  42. endsWith(filename, ".css") ||
  43. endsWith(filename, ".js") ||
  44. endsWith(filename, ".map") ||
  45. endsWith(filename, ".jpg") ||
  46. endsWith(filename, ".jpeg") ||
  47. endsWith(filename, ".ico") ||
  48. endsWith(filename, ".png")) {
  49. if (filename == "/sdcard/html/setup.html") {
  50. httpd_resp_set_hdr(req, "Clear-Site-Data", "\"*\"");
  51. }
  52. else {
  53. httpd_resp_set_hdr(req, "Cache-Control", "max-age=86400");
  54. }
  55. }
  56. set_content_type_from_file(req, filename.c_str());
  57. /* Retrieve the pointer to scratch buffer for temporary storage */
  58. char *chunk = scratch;
  59. size_t chunksize;
  60. do {
  61. /* Read file in chunks into the scratch buffer */
  62. chunksize = fread(chunk, 1, SERVER_HELPER_SCRATCH_BUFSIZE, fd);
  63. /* Send the buffer contents as HTTP response chunk */
  64. if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
  65. fclose(fd);
  66. ESP_LOGE(TAG, "File sending failed!");
  67. /* Abort sending file */
  68. httpd_resp_sendstr_chunk(req, NULL);
  69. /* Respond with 500 Internal Server Error */
  70. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
  71. return ESP_FAIL;
  72. }
  73. /* Keep looping till the whole file is sent */
  74. } while (chunksize != 0);
  75. /* Close file after sending complete */
  76. fclose(fd);
  77. ESP_LOGD(TAG, "File sending complete");
  78. return ESP_OK;
  79. }
  80. /* Copies the full path into destination buffer and returns
  81. * pointer to path (skipping the preceding base path) */
  82. const char* get_path_from_uri(char *dest, const char *base_path, const char *uri, size_t destsize)
  83. {
  84. const size_t base_pathlen = strlen(base_path);
  85. size_t pathlen = strlen(uri);
  86. const char *quest = strchr(uri, '?');
  87. if (quest) {
  88. pathlen = MIN(pathlen, quest - uri);
  89. }
  90. const char *hash = strchr(uri, '#');
  91. if (hash) {
  92. pathlen = MIN(pathlen, hash - uri);
  93. }
  94. if (base_pathlen + pathlen + 1 > destsize) {
  95. /* Full path string won't fit into destination buffer */
  96. return NULL;
  97. }
  98. /* Construct full path (base + path) */
  99. strcpy(dest, base_path);
  100. strlcpy(dest + base_pathlen, uri, pathlen + 1);
  101. /* Return pointer to path, skipping the base */
  102. return dest + base_pathlen;
  103. }
  104. /* Set HTTP response content type according to file extension */
  105. esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filename)
  106. {
  107. if (IS_FILE_EXT(filename, ".pdf")) {
  108. return httpd_resp_set_type(req, "application/pdf");
  109. } else if (IS_FILE_EXT(filename, ".html")) {
  110. return httpd_resp_set_type(req, "text/html");
  111. } else if (IS_FILE_EXT(filename, ".jpeg")) {
  112. return httpd_resp_set_type(req, "image/jpeg");
  113. } else if (IS_FILE_EXT(filename, ".jpg")) {
  114. return httpd_resp_set_type(req, "image/jpeg");
  115. } else if (IS_FILE_EXT(filename, ".ico")) {
  116. return httpd_resp_set_type(req, "image/x-icon");
  117. } else if (IS_FILE_EXT(filename, ".js")) {
  118. return httpd_resp_set_type(req, "text/javascript");
  119. } else if (IS_FILE_EXT(filename, ".css")) {
  120. return httpd_resp_set_type(req, "text/css");
  121. }
  122. /* This is a limited set only */
  123. /* For any other type always set as plain text */
  124. return httpd_resp_set_type(req, "text/plain");
  125. }