server_help.cpp 6.2 KB

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