_esp_httpd_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <sys/socket.h>
  16. #include <sys/param.h>
  17. #include <errno.h>
  18. #include <esp_log.h>
  19. #include <esp_err.h>
  20. #include <assert.h>
  21. #include <esp_http_server.h>
  22. #include <_esp_http_server.h>
  23. #include "esp_httpd_priv.h"
  24. #include "ctrl_sock.h"
  25. static const char *TAG = "_httpd";
  26. struct httpd_ctrl_data {
  27. enum httpd_ctrl_msg {
  28. HTTPD_CTRL_SHUTDOWN,
  29. HTTPD_CTRL_WORK,
  30. } hc_msg;
  31. httpd_work_fn_t hc_work;
  32. void *hc_work_arg;
  33. };
  34. static esp_err_t _httpd_server_init(struct httpd_data *hd)
  35. {
  36. int fd = socket(PF_INET6, SOCK_STREAM, 0);
  37. if (fd < 0) {
  38. ESP_LOGE(TAG, LOG_FMT("error in socket (%d)"), errno);
  39. return ESP_FAIL;
  40. }
  41. struct in6_addr inaddr_any = IN6ADDR_ANY_INIT;
  42. struct sockaddr_in6 serv_addr = {
  43. .sin6_family = PF_INET6,
  44. .sin6_addr = inaddr_any,
  45. .sin6_port = htons(hd->config.server_port)
  46. };
  47. /* Enable SO_REUSEADDR to allow binding to the same
  48. * address and port when restarting the server */
  49. int enable = 1;
  50. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
  51. /* This will fail if CONFIG_LWIP_SO_REUSE is not enabled. But
  52. * it does not affect the normal working of the HTTP Server */
  53. ESP_LOGW(TAG, LOG_FMT("error enabling SO_REUSEADDR (%d)"), errno);
  54. }
  55. int ret = bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  56. if (ret < 0) {
  57. ESP_LOGE(TAG, LOG_FMT("error in bind (%d)"), errno);
  58. close(fd);
  59. return ESP_FAIL;
  60. }
  61. ret = listen(fd, hd->config.backlog_conn);
  62. if (ret < 0) {
  63. ESP_LOGE(TAG, LOG_FMT("error in listen (%d)"), errno);
  64. close(fd);
  65. return ESP_FAIL;
  66. }
  67. hd->listen_fd = fd;
  68. return ESP_OK;
  69. }
  70. static void _httpd_process_ctrl_msg(struct httpd_data *hd)
  71. {
  72. struct httpd_ctrl_data msg;
  73. int ret = recv(hd->ctrl_fd, &msg, sizeof(msg), 0);
  74. if (ret <= 0) {
  75. ESP_LOGW(TAG, LOG_FMT("error in recv (%d)"), errno);
  76. return;
  77. }
  78. if (ret != sizeof(msg)) {
  79. ESP_LOGW(TAG, LOG_FMT("incomplete msg"));
  80. return;
  81. }
  82. switch (msg.hc_msg) {
  83. case HTTPD_CTRL_WORK:
  84. if (msg.hc_work) {
  85. ESP_LOGD(TAG, LOG_FMT("work"));
  86. (*msg.hc_work)(msg.hc_work_arg);
  87. }
  88. break;
  89. case HTTPD_CTRL_SHUTDOWN:
  90. ESP_LOGD(TAG, LOG_FMT("shutdown"));
  91. hd->hd_td.status = THREAD_STOPPING;
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. static esp_err_t _httpd_accept_conn(struct httpd_data *hd, int listen_fd)
  98. {
  99. /* If no space is available for new session, close the least recently used one */
  100. if (hd->config.lru_purge_enable == true) {
  101. if (!httpd_is_sess_available(hd)) {
  102. /* Queue asynchronous closure of the least recently used session */
  103. return httpd_sess_close_lru(hd);
  104. /* Returning from this allowes the main server thread to process
  105. * the queued asynchronous control message for closing LRU session.
  106. * Since connection request hasn't been addressed yet using accept()
  107. * therefore _httpd_accept_conn() will be called again, but this time
  108. * with space available for one session
  109. */
  110. }
  111. }
  112. struct sockaddr_in addr_from;
  113. socklen_t addr_from_len = sizeof(addr_from);
  114. int new_fd = accept(listen_fd, (struct sockaddr *)&addr_from, &addr_from_len);
  115. if (new_fd < 0) {
  116. ESP_LOGW(TAG, LOG_FMT("error in accept (%d)"), errno);
  117. return ESP_FAIL;
  118. }
  119. ESP_LOGD(TAG, LOG_FMT("newfd = %d"), new_fd);
  120. struct timeval tv;
  121. /* Set recv timeout of this fd as per config */
  122. tv.tv_sec = hd->config.recv_wait_timeout;
  123. tv.tv_usec = 0;
  124. setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
  125. /* Set send timeout of this fd as per config */
  126. tv.tv_sec = hd->config.send_wait_timeout;
  127. tv.tv_usec = 0;
  128. setsockopt(new_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv));
  129. if (ESP_OK != httpd_sess_new(hd, new_fd)) {
  130. ESP_LOGW(TAG, LOG_FMT("session creation failed"));
  131. close(new_fd);
  132. return ESP_FAIL;
  133. }
  134. ESP_LOGD(TAG, LOG_FMT("complete"));
  135. return ESP_OK;
  136. }
  137. /* Manage in-coming connection or data requests */
  138. static esp_err_t _httpd_server(struct httpd_data *hd)
  139. {
  140. fd_set read_set;
  141. FD_ZERO(&read_set);
  142. if (hd->config.lru_purge_enable || httpd_is_sess_available(hd)) {
  143. /* Only listen for new connections if server has capacity to
  144. * handle more (or when LRU purge is enabled, in which case
  145. * older connections will be closed) */
  146. FD_SET(hd->listen_fd, &read_set);
  147. }
  148. FD_SET(hd->ctrl_fd, &read_set);
  149. int tmp_max_fd;
  150. httpd_sess_set_descriptors(hd, &read_set, &tmp_max_fd);
  151. int maxfd = MAX(hd->listen_fd, tmp_max_fd);
  152. tmp_max_fd = maxfd;
  153. maxfd = MAX(hd->ctrl_fd, tmp_max_fd);
  154. ESP_LOGD(TAG, LOG_FMT("doing select maxfd+1 = %d"), maxfd + 1);
  155. int active_cnt = select(maxfd + 1, &read_set, NULL, NULL, NULL);
  156. if (active_cnt < 0) {
  157. ESP_LOGE(TAG, LOG_FMT("error in select (%d)"), errno);
  158. httpd_sess_delete_invalid(hd);
  159. return ESP_OK;
  160. }
  161. /* Case0: Do we have a control message? */
  162. /* Case1: Do we have any activity on the current data
  163. * sessions? */
  164. int fd = -1;
  165. while ((fd = httpd_sess_iterate(hd, fd)) != -1) {
  166. if (FD_ISSET(fd, &read_set) || (httpd_sess_pending(hd, fd))) {
  167. ESP_LOGD(TAG, LOG_FMT("processing socket %d"), fd);
  168. if (httpd_sess_process(hd, fd) != ESP_OK) {
  169. ESP_LOGD(TAG, LOG_FMT("closing socket %d"), fd);
  170. close(fd);
  171. /* Delete session and update fd to that
  172. * preceding the one being deleted */
  173. fd = httpd_sess_delete(hd, fd);
  174. }
  175. }
  176. }
  177. /* Case2: Do we have any incoming connection requests to
  178. * process? */
  179. if (FD_ISSET(hd->listen_fd, &read_set)) {
  180. ESP_LOGD(TAG, LOG_FMT("processing listen socket %d"), hd->listen_fd);
  181. if (_httpd_accept_conn(hd, hd->listen_fd) != ESP_OK) {
  182. ESP_LOGW(TAG, LOG_FMT("error accepting new connection"));
  183. }
  184. }
  185. return ESP_OK;
  186. }
  187. static void _httpd_close_all_sessions(struct httpd_data *hd)
  188. {
  189. int fd = -1;
  190. while ((fd = httpd_sess_iterate(hd, fd)) != -1) {
  191. ESP_LOGD(TAG, LOG_FMT("cleaning up socket %d"), fd);
  192. httpd_sess_delete(hd, fd);
  193. close(fd);
  194. }
  195. }
  196. /* The main HTTPD thread */
  197. static void _httpd_thread(void *arg)
  198. {
  199. int ret;
  200. struct httpd_data *hd = (struct httpd_data *) arg;
  201. hd->hd_td.status = THREAD_RUNNING;
  202. ESP_LOGD(TAG, LOG_FMT("web server started"));
  203. while (1) {
  204. ret = _httpd_server(hd);
  205. if (ret != ESP_OK) {
  206. break;
  207. }
  208. }
  209. ESP_LOGD(TAG, LOG_FMT("web server exiting"));
  210. _httpd_close_all_sessions(hd);
  211. close(hd->listen_fd);
  212. hd->hd_td.status = THREAD_STOPPED;
  213. httpd_os_thread_delete();
  214. }
  215. static struct httpd_data *__httpd_create(const httpd_config_t *config)
  216. {
  217. /* Allocate memory for httpd instance data */
  218. struct httpd_data *hd = calloc(1, sizeof(struct httpd_data));
  219. if (!hd) {
  220. ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP server instance"));
  221. return NULL;
  222. }
  223. hd->hd_calls = calloc(config->max_uri_handlers, sizeof(httpd_uri_t *));
  224. if (!hd->hd_calls) {
  225. ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP URI handlers"));
  226. free(hd);
  227. return NULL;
  228. }
  229. hd->hd_sd = calloc(config->max_open_sockets, sizeof(struct sock_db));
  230. if (!hd->hd_sd) {
  231. ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP session data"));
  232. free(hd->hd_calls);
  233. free(hd);
  234. return NULL;
  235. }
  236. struct httpd_req_aux *ra = &hd->hd_req_aux;
  237. ra->resp_hdrs = calloc(config->max_resp_headers, sizeof(struct resp_hdr));
  238. if (!ra->resp_hdrs) {
  239. ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP response headers"));
  240. free(hd->hd_sd);
  241. free(hd->hd_calls);
  242. free(hd);
  243. return NULL;
  244. }
  245. hd->err_handler_fns = calloc(HTTPD_ERR_CODE_MAX, sizeof(httpd_err_handler_func_t));
  246. if (!hd->err_handler_fns) {
  247. ESP_LOGE(TAG, LOG_FMT("Failed to allocate memory for HTTP error handlers"));
  248. free(ra->resp_hdrs);
  249. free(hd->hd_sd);
  250. free(hd->hd_calls);
  251. free(hd);
  252. return NULL;
  253. }
  254. /* Save the configuration for this instance */
  255. hd->config = *config;
  256. return hd;
  257. }
  258. static void _httpd_delete(struct httpd_data *hd)
  259. {
  260. struct httpd_req_aux *ra = &hd->hd_req_aux;
  261. /* Free memory of httpd instance data */
  262. free(hd->err_handler_fns);
  263. free(ra->resp_hdrs);
  264. free(hd->hd_sd);
  265. /* Free registered URI handlers */
  266. httpd_unregister_all_uri_handlers(hd);
  267. free(hd->hd_calls);
  268. free(hd);
  269. }
  270. esp_err_t __httpd_start(httpd_handle_t *handle, const httpd_config_t *config)
  271. {
  272. if (handle == NULL || config == NULL) {
  273. return ESP_ERR_INVALID_ARG;
  274. }
  275. /* Sanity check about whether LWIP is configured for providing the
  276. * maximum number of open sockets sufficient for the server. Though,
  277. * this check doesn't guarantee that many sockets will actually be
  278. * available at runtime as other processes may use up some sockets.
  279. * Note that server also uses 3 sockets for its internal use :
  280. * 1) listening for new TCP connections
  281. * 2) for sending control messages over UDP
  282. * 3) for receiving control messages over UDP
  283. * So the total number of required sockets is max_open_sockets + 3
  284. */
  285. if (CONFIG_LWIP_MAX_SOCKETS < config->max_open_sockets + 3) {
  286. ESP_LOGE(TAG, "Configuration option max_open_sockets is too large (max allowed %d)\n\t"
  287. "Either decrease this or configure LWIP_MAX_SOCKETS to a larger value",
  288. CONFIG_LWIP_MAX_SOCKETS - 3);
  289. return ESP_ERR_INVALID_ARG;
  290. }
  291. struct httpd_data *hd = __httpd_create(config);
  292. if (hd == NULL) {
  293. /* Failed to allocate memory */
  294. return ESP_ERR_HTTPD_ALLOC_MEM;
  295. }
  296. if (_httpd_server_init(hd) != ESP_OK) {
  297. _httpd_delete(hd);
  298. return ESP_FAIL;
  299. }
  300. httpd_sess_init(hd);
  301. if (__httpd_os_thread_create_static(&hd->hd_td.handle, "httpd",
  302. hd->config.stack_size,
  303. hd->config.task_priority,
  304. _httpd_thread, hd,
  305. hd->config.core_id) != ESP_OK) {
  306. /* Failed to launch task */
  307. _httpd_delete(hd);
  308. return ESP_ERR_HTTPD_TASK;
  309. }
  310. *handle = (httpd_handle_t *)hd;
  311. return ESP_OK;
  312. }