server_help.cpp 3.7 KB

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