server_help.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. {
  23. if (str.length() < suffix.length()) {
  24. return false;
  25. }
  26. return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
  27. }
  28. esp_err_t send_file(httpd_req_t *req, std::string filename)
  29. {
  30. std::string _filename_old = filename;
  31. struct stat file_stat;
  32. bool _gz_file_exists = false;
  33. ESP_LOGD(TAG, "old filename: %s", filename.c_str());
  34. std::string _filename_temp = std::string(filename) + ".gz";
  35. // Checks whether the file is available as .gz
  36. if (stat(_filename_temp.c_str(), &file_stat) == 0) {
  37. filename = _filename_temp;
  38. ESP_LOGD(TAG, "new filename: %s", filename.c_str());
  39. _gz_file_exists = true;
  40. }
  41. FILE *fd = fopen(filename.c_str(), "r");
  42. if (!fd) {
  43. ESP_LOGE(TAG, "Failed to read file: %s", filename.c_str());
  44. /* Respond with 404 Error */
  45. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
  46. return ESP_FAIL;
  47. }
  48. ESP_LOGD(TAG, "Sending file: %s ...", filename.c_str());
  49. /* For all files with the following file extention tell the webbrowser to cache them for 12h */
  50. if (endsWith(filename, ".html") ||
  51. endsWith(filename, ".htm") ||
  52. endsWith(filename, ".xml") ||
  53. endsWith(filename, ".css") ||
  54. endsWith(filename, ".js") ||
  55. endsWith(filename, ".map") ||
  56. endsWith(filename, ".jpg") ||
  57. endsWith(filename, ".jpeg") ||
  58. endsWith(filename, ".ico") ||
  59. endsWith(filename, ".png") ||
  60. endsWith(filename, ".gif") ||
  61. // endsWith(filename, ".zip") ||
  62. endsWith(filename, ".gz")) {
  63. if (filename == "/sdcard/html/setup.html") {
  64. httpd_resp_set_hdr(req, "Clear-Site-Data", "\"*\"");
  65. set_content_type_from_file(req, filename.c_str());
  66. }
  67. else if (_gz_file_exists) {
  68. httpd_resp_set_hdr(req, "Cache-Control", "max-age=43200");
  69. httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
  70. set_content_type_from_file(req, _filename_old.c_str());
  71. }
  72. else {
  73. httpd_resp_set_hdr(req, "Cache-Control", "max-age=43200");
  74. set_content_type_from_file(req, filename.c_str());
  75. }
  76. }
  77. else {
  78. set_content_type_from_file(req, filename.c_str());
  79. }
  80. /* Retrieve the pointer to scratch buffer for temporary storage */
  81. char *chunk = scratch;
  82. size_t chunksize;
  83. do {
  84. /* Read file in chunks into the scratch buffer */
  85. chunksize = fread(chunk, 1, SERVER_HELPER_SCRATCH_BUFSIZE, fd);
  86. /* Send the buffer contents as HTTP response chunk */
  87. if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
  88. fclose(fd);
  89. ESP_LOGE(TAG, "File sending failed!");
  90. /* Abort sending file */
  91. httpd_resp_sendstr_chunk(req, NULL);
  92. /* Respond with 500 Internal Server Error */
  93. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
  94. return ESP_FAIL;
  95. }
  96. /* Keep looping till the whole file is sent */
  97. } while (chunksize != 0);
  98. /* Close file after sending complete */
  99. fclose(fd);
  100. ESP_LOGD(TAG, "File sending complete");
  101. return ESP_OK;
  102. }
  103. /* Copies the full path into destination buffer and returns
  104. * pointer to path (skipping the preceding base path) */
  105. const char* get_path_from_uri(char *dest, const char *base_path, const char *uri, size_t destsize)
  106. {
  107. const size_t base_pathlen = strlen(base_path);
  108. size_t pathlen = strlen(uri);
  109. const char *quest = strchr(uri, '?');
  110. if (quest) {
  111. pathlen = MIN(pathlen, quest - uri);
  112. }
  113. const char *hash = strchr(uri, '#');
  114. if (hash) {
  115. pathlen = MIN(pathlen, hash - uri);
  116. }
  117. if (base_pathlen + pathlen + 1 > destsize) {
  118. /* Full path string won't fit into destination buffer */
  119. return NULL;
  120. }
  121. /* Construct full path (base + path) */
  122. strcpy(dest, base_path);
  123. strlcpy(dest + base_pathlen, uri, pathlen + 1);
  124. /* Return pointer to path, skipping the base */
  125. return dest + base_pathlen;
  126. }
  127. /* Set HTTP response content type according to file extension */
  128. esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filename)
  129. {
  130. if (IS_FILE_EXT(filename, ".pdf")) {
  131. return httpd_resp_set_type(req, "application/x-pdf");
  132. }
  133. else if (IS_FILE_EXT(filename, ".htm")) {
  134. return httpd_resp_set_type(req, "text/html");
  135. }
  136. else if (IS_FILE_EXT(filename, ".html")) {
  137. return httpd_resp_set_type(req, "text/html");
  138. }
  139. else if (IS_FILE_EXT(filename, ".jpeg")) {
  140. return httpd_resp_set_type(req, "image/jpeg");
  141. }
  142. else if (IS_FILE_EXT(filename, ".jpg")) {
  143. return httpd_resp_set_type(req, "image/jpeg");
  144. }
  145. else if (IS_FILE_EXT(filename, ".gif")) {
  146. return httpd_resp_set_type(req, "image/gif");
  147. }
  148. else if (IS_FILE_EXT(filename, ".png")) {
  149. return httpd_resp_set_type(req, "image/png");
  150. }
  151. else if (IS_FILE_EXT(filename, ".ico")) {
  152. return httpd_resp_set_type(req, "image/x-icon");
  153. }
  154. else if (IS_FILE_EXT(filename, ".js")) {
  155. return httpd_resp_set_type(req, "application/javascript");
  156. }
  157. else if (IS_FILE_EXT(filename, ".css")) {
  158. return httpd_resp_set_type(req, "text/css");
  159. }
  160. else if (IS_FILE_EXT(filename, ".xml")) {
  161. return httpd_resp_set_type(req, "text/xml");
  162. }
  163. else if (IS_FILE_EXT(filename, ".zip")) {
  164. return httpd_resp_set_type(req, "application/x-zip");
  165. }
  166. else if (IS_FILE_EXT(filename, ".gz")) {
  167. return httpd_resp_set_type(req, "application/x-gzip");
  168. }
  169. /* This is a limited set only */
  170. /* For any other type always set as plain text */
  171. return httpd_resp_set_type(req, "text/plain");
  172. }