server_help.cpp 3.5 KB

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