server_help.cpp 3.5 KB

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