server_help.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. static const char *TAG = "serverhelp";
  19. #define SCRATCH_BUFSIZE 8192
  20. char scratch[SCRATCH_BUFSIZE];
  21. #define IS_FILE_EXT(filename, ext) \
  22. (strcasecmp(&filename[strlen(filename) - sizeof(ext) + 1], ext) == 0)
  23. esp_err_t send_file(httpd_req_t *req, std::string filename)
  24. {
  25. FILE *fd = OpenFileAndWait(filename.c_str(), "r");
  26. if (!fd) {
  27. ESP_LOGE(TAG, "Failed to read existing file : %s", filename.c_str());
  28. /* Respond with 500 Internal Server Error */
  29. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file");
  30. return ESP_FAIL;
  31. }
  32. ESP_LOGI(TAG, "Sending file : %s ...", filename.c_str());
  33. // httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  34. set_content_type_from_file(req, filename.c_str());
  35. /* Retrieve the pointer to scratch buffer for temporary storage */
  36. char *chunk = scratch;
  37. size_t chunksize;
  38. do {
  39. /* Read file in chunks into the scratch buffer */
  40. chunksize = fread(chunk, 1, SCRATCH_BUFSIZE, fd);
  41. /* Send the buffer contents as HTTP response chunk */
  42. if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
  43. fclose(fd);
  44. ESP_LOGE(TAG, "File sending failed!");
  45. /* Abort sending file */
  46. httpd_resp_sendstr_chunk(req, NULL);
  47. /* Respond with 500 Internal Server Error */
  48. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
  49. return ESP_FAIL;
  50. }
  51. /* Keep looping till the whole file is sent */
  52. } while (chunksize != 0);
  53. /* Close file after sending complete */
  54. fclose(fd);
  55. ESP_LOGI(TAG, "File sending complete");
  56. return ESP_OK;
  57. }
  58. /* Copies the full path into destination buffer and returns
  59. * pointer to path (skipping the preceding base path) */
  60. const char* get_path_from_uri(char *dest, const char *base_path, const char *uri, size_t destsize)
  61. {
  62. const size_t base_pathlen = strlen(base_path);
  63. size_t pathlen = strlen(uri);
  64. const char *quest = strchr(uri, '?');
  65. if (quest) {
  66. pathlen = MIN(pathlen, quest - uri);
  67. }
  68. const char *hash = strchr(uri, '#');
  69. if (hash) {
  70. pathlen = MIN(pathlen, hash - uri);
  71. }
  72. if (base_pathlen + pathlen + 1 > destsize) {
  73. /* Full path string won't fit into destination buffer */
  74. return NULL;
  75. }
  76. /* Construct full path (base + path) */
  77. strcpy(dest, base_path);
  78. strlcpy(dest + base_pathlen, uri, pathlen + 1);
  79. /* Return pointer to path, skipping the base */
  80. return dest + base_pathlen;
  81. }
  82. /* Set HTTP response content type according to file extension */
  83. esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filename)
  84. {
  85. if (IS_FILE_EXT(filename, ".pdf")) {
  86. return httpd_resp_set_type(req, "application/pdf");
  87. } else if (IS_FILE_EXT(filename, ".html")) {
  88. return httpd_resp_set_type(req, "text/html");
  89. } else if (IS_FILE_EXT(filename, ".jpeg")) {
  90. return httpd_resp_set_type(req, "image/jpeg");
  91. } else if (IS_FILE_EXT(filename, ".jpg")) {
  92. return httpd_resp_set_type(req, "image/jpeg");
  93. } else if (IS_FILE_EXT(filename, ".ico")) {
  94. return httpd_resp_set_type(req, "image/x-icon");
  95. } else if (IS_FILE_EXT(filename, ".js")) {
  96. return httpd_resp_set_type(req, "text/javascript");
  97. } else if (IS_FILE_EXT(filename, ".css")) {
  98. return httpd_resp_set_type(req, "text/css");
  99. }
  100. /* This is a limited set only */
  101. /* For any other type always set as plain text */
  102. return httpd_resp_set_type(req, "text/plain");
  103. }