server_main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "server_main.h"
  2. #include <string>
  3. #include "server_help.h"
  4. #include "ClassLogFile.h"
  5. #include "time_sntp.h"
  6. #include "version.h"
  7. httpd_handle_t server = NULL;
  8. std::string starttime = "";
  9. /* An HTTP GET handler */
  10. esp_err_t info_get_handler(httpd_req_t *req)
  11. {
  12. LogFile.WriteToFile("info_get_handler");
  13. char _query[200];
  14. char _valuechar[30];
  15. std::string _task;
  16. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  17. {
  18. printf("Query: "); printf(_query); printf("\n");
  19. if (httpd_query_key_value(_query, "type", _valuechar, 30) == ESP_OK)
  20. {
  21. printf("type is found"); printf(_valuechar); printf("\n");
  22. _task = std::string(_valuechar);
  23. }
  24. };
  25. if (_task.compare("GitBranch") == 0)
  26. {
  27. std::string zw;
  28. zw = std::string(libfive_git_branch());
  29. httpd_resp_sendstr_chunk(req, zw.c_str());
  30. httpd_resp_sendstr_chunk(req, NULL);
  31. return ESP_OK;
  32. }
  33. if (_task.compare("GitTag") == 0)
  34. {
  35. std::string zw;
  36. zw = std::string(libfive_git_version());
  37. httpd_resp_sendstr_chunk(req, zw.c_str());
  38. httpd_resp_sendstr_chunk(req, NULL);
  39. return ESP_OK;
  40. }
  41. if (_task.compare("GitRevision") == 0)
  42. {
  43. std::string zw;
  44. zw = std::string(libfive_git_revision());
  45. httpd_resp_sendstr_chunk(req, zw.c_str());
  46. httpd_resp_sendstr_chunk(req, NULL);
  47. return ESP_OK;
  48. }
  49. if (_task.compare("BuildTime") == 0)
  50. {
  51. std::string zw;
  52. zw = std::string(build_time());
  53. httpd_resp_sendstr_chunk(req, zw.c_str());
  54. httpd_resp_sendstr_chunk(req, NULL);
  55. return ESP_OK;
  56. }
  57. if (_task.compare("GitBaseBranch") == 0)
  58. {
  59. std::string zw;
  60. zw = std::string(git_base_branch());
  61. httpd_resp_sendstr_chunk(req, zw.c_str());
  62. httpd_resp_sendstr_chunk(req, NULL);
  63. return ESP_OK;
  64. }
  65. return ESP_OK;
  66. }
  67. esp_err_t starttime_get_handler(httpd_req_t *req)
  68. {
  69. httpd_resp_send(req, starttime.c_str(), strlen(starttime.c_str()));
  70. /* Respond with an empty chunk to signal HTTP response completion */
  71. httpd_resp_send_chunk(req, NULL, 0);
  72. return ESP_OK;
  73. }
  74. esp_err_t hello_main_handler(httpd_req_t *req)
  75. {
  76. char filepath[50];
  77. struct stat file_stat;
  78. printf("uri: %s\n", req->uri);
  79. int _pos;
  80. char *base_path = (char*) req->user_ctx;
  81. std::string filetosend(base_path);
  82. const char *filename = get_path_from_uri(filepath, base_path,
  83. req->uri - 1, sizeof(filepath));
  84. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  85. if ((strcmp(req->uri, "/") == 0))
  86. {
  87. filetosend = filetosend + "/html/index.html";
  88. }
  89. else
  90. {
  91. filetosend = filetosend + "/html" + std::string(req->uri);
  92. _pos = filetosend.find("?");
  93. if (_pos > -1){
  94. filetosend = filetosend.substr(0, _pos);
  95. }
  96. }
  97. printf("Filename: %s\n", filename);
  98. printf("File requested: %s\n", filetosend.c_str());
  99. if (!filename) {
  100. ESP_LOGE(TAG, "Filename is too long");
  101. /* Respond with 500 Internal Server Error */
  102. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  103. return ESP_FAIL;
  104. }
  105. if (stat(filetosend.c_str(), &file_stat) == -1) {
  106. /* If file not present on SPIFFS check if URI
  107. * corresponds to one of the hardcoded paths */
  108. ESP_LOGE(TAG, "Failed to stat file : %s", filetosend.c_str());
  109. /* Respond with 404 Not Found */
  110. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
  111. return ESP_FAIL;
  112. }
  113. esp_err_t res;
  114. res = httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  115. if (res != ESP_OK)
  116. return res;
  117. res = send_file(req, filetosend, &file_stat);
  118. if (res != ESP_OK)
  119. return res;
  120. /* Respond with an empty chunk to signal HTTP response completion */
  121. httpd_resp_send_chunk(req, NULL, 0);
  122. return ESP_OK;
  123. }
  124. esp_err_t img_tmp_handler(httpd_req_t *req)
  125. {
  126. char filepath[50];
  127. struct stat file_stat;
  128. printf("uri: %s\n", req->uri);
  129. char *base_path = (char*) req->user_ctx;
  130. std::string filetosend(base_path);
  131. const char *filename = get_path_from_uri(filepath, base_path,
  132. req->uri + sizeof("/img_tmp") - 1, sizeof(filepath));
  133. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  134. filetosend = filetosend + "/img_tmp/" + std::string(filename);
  135. printf("File to upload: %s\n", filetosend.c_str());
  136. if (!filename) {
  137. ESP_LOGE(TAG, "Filename is too long");
  138. /* Respond with 500 Internal Server Error */
  139. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  140. return ESP_FAIL;
  141. }
  142. if (stat(filetosend.c_str(), &file_stat) == -1) {
  143. /* If file not present on SPIFFS check if URI
  144. * corresponds to one of the hardcoded paths */
  145. ESP_LOGE(TAG, "Failed to stat file : %s", filetosend.c_str());
  146. /* Respond with 404 Not Found */
  147. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
  148. return ESP_FAIL;
  149. }
  150. esp_err_t res = send_file(req, filetosend, &file_stat);
  151. if (res != ESP_OK)
  152. return res;
  153. /* Respond with an empty chunk to signal HTTP response completion */
  154. httpd_resp_send_chunk(req, NULL, 0);
  155. return ESP_OK;
  156. }
  157. void register_server_main_uri(httpd_handle_t server, const char *base_path)
  158. {
  159. httpd_uri_t info_get_handle = {
  160. .uri = "/version", // Match all URIs of type /path/to/file
  161. .method = HTTP_GET,
  162. .handler = info_get_handler,
  163. .user_ctx = (void*) base_path // Pass server data as context
  164. };
  165. httpd_register_uri_handler(server, &info_get_handle);
  166. httpd_uri_t starttime_tmp_handle = {
  167. .uri = "/starttime", // Match all URIs of type /path/to/file
  168. .method = HTTP_GET,
  169. .handler = starttime_get_handler,
  170. .user_ctx = NULL // Pass server data as context
  171. };
  172. httpd_register_uri_handler(server, &starttime_tmp_handle);
  173. httpd_uri_t img_tmp_handle = {
  174. .uri = "/img_tmp/*", // Match all URIs of type /path/to/file
  175. .method = HTTP_GET,
  176. .handler = img_tmp_handler,
  177. .user_ctx = (void*) base_path // Pass server data as context
  178. };
  179. httpd_register_uri_handler(server, &img_tmp_handle);
  180. httpd_uri_t main_rest_handle = {
  181. .uri = "/*", // Match all URIs of type /path/to/file
  182. .method = HTTP_GET,
  183. .handler = hello_main_handler,
  184. .user_ctx = (void*) base_path // Pass server data as context
  185. };
  186. httpd_register_uri_handler(server, &main_rest_handle);
  187. }
  188. httpd_handle_t start_webserver(void)
  189. {
  190. httpd_handle_t server = NULL;
  191. httpd_config_t config = { };
  192. config.task_priority = tskIDLE_PRIORITY+5;
  193. config.stack_size = 16384; // bei 32k stürzt das Programm beim Bilderaufnehmen ab
  194. config.core_id = tskNO_AFFINITY;
  195. config.server_port = 80;
  196. config.ctrl_port = 32768;
  197. config.max_open_sockets = 7;
  198. config.max_uri_handlers = 24;
  199. config.max_resp_headers = 8;
  200. config.backlog_conn = 5;
  201. config.lru_purge_enable = false;
  202. config.recv_wait_timeout = 30; // default: 5
  203. config.send_wait_timeout = 30; // default: 5
  204. config.global_user_ctx = NULL;
  205. config.global_user_ctx_free_fn = NULL;
  206. config.global_transport_ctx = NULL;
  207. config.global_transport_ctx_free_fn = NULL;
  208. config.open_fn = NULL;
  209. config.close_fn = NULL;
  210. // config.uri_match_fn = NULL;
  211. config.uri_match_fn = httpd_uri_match_wildcard;
  212. starttime = gettimestring("%Y%m%d-%H%M%S");
  213. // Start the httpd server
  214. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  215. if (httpd_start(&server, &config) == ESP_OK) {
  216. // Set URI handlers
  217. ESP_LOGI(TAG, "Registering URI handlers");
  218. return server;
  219. }
  220. ESP_LOGI(TAG, "Error starting server!");
  221. return NULL;
  222. }
  223. void stop_webserver(httpd_handle_t server)
  224. {
  225. httpd_stop(server);
  226. }
  227. void disconnect_handler(void* arg, esp_event_base_t event_base,
  228. int32_t event_id, void* event_data)
  229. {
  230. httpd_handle_t* server = (httpd_handle_t*) arg;
  231. if (*server) {
  232. ESP_LOGI(TAG, "Stopping webserver");
  233. stop_webserver(*server);
  234. *server = NULL;
  235. }
  236. }
  237. void connect_handler(void* arg, esp_event_base_t event_base,
  238. int32_t event_id, void* event_data)
  239. {
  240. httpd_handle_t* server = (httpd_handle_t*) arg;
  241. if (*server == NULL) {
  242. ESP_LOGI(TAG, "Starting webserver");
  243. *server = start_webserver();
  244. }
  245. }