httpd_sess.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <stdlib.h>
  15. #include <esp_log.h>
  16. #include <esp_err.h>
  17. #include <esp_http_server.h>
  18. #include "esp_httpd_priv.h"
  19. static const char *TAG = "httpd_sess";
  20. bool httpd_is_sess_available(struct httpd_data *hd)
  21. {
  22. int i;
  23. for (i = 0; i < hd->config.max_open_sockets; i++) {
  24. if (hd->hd_sd[i].fd == -1) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. struct sock_db *httpd_sess_get(struct httpd_data *hd, int sockfd)
  31. {
  32. if (hd == NULL) {
  33. return NULL;
  34. }
  35. /* Check if called inside a request handler, and the
  36. * session sockfd in use is same as the parameter */
  37. if ((hd->hd_req_aux.sd) && (hd->hd_req_aux.sd->fd == sockfd)) {
  38. /* Just return the pointer to the sock_db
  39. * corresponding to the request */
  40. return hd->hd_req_aux.sd;
  41. }
  42. int i;
  43. for (i = 0; i < hd->config.max_open_sockets; i++) {
  44. if (hd->hd_sd[i].fd == sockfd) {
  45. return &hd->hd_sd[i];
  46. }
  47. }
  48. return NULL;
  49. }
  50. esp_err_t httpd_sess_new(struct httpd_data *hd, int newfd)
  51. {
  52. ESP_LOGD(TAG, LOG_FMT("fd = %d"), newfd);
  53. if (httpd_sess_get(hd, newfd)) {
  54. ESP_LOGE(TAG, LOG_FMT("session already exists with fd = %d"), newfd);
  55. return ESP_FAIL;
  56. }
  57. int i;
  58. for (i = 0; i < hd->config.max_open_sockets; i++) {
  59. if (hd->hd_sd[i].fd == -1) {
  60. memset(&hd->hd_sd[i], 0, sizeof(hd->hd_sd[i]));
  61. hd->hd_sd[i].fd = newfd;
  62. hd->hd_sd[i].handle = (httpd_handle_t) hd;
  63. hd->hd_sd[i].send_fn = httpd_default_send;
  64. hd->hd_sd[i].recv_fn = httpd_default_recv;
  65. /* Call user-defined session opening function */
  66. if (hd->config.open_fn) {
  67. esp_err_t ret = hd->config.open_fn(hd, hd->hd_sd[i].fd);
  68. if (ret != ESP_OK) {
  69. httpd_sess_delete(hd, hd->hd_sd[i].fd);
  70. ESP_LOGD(TAG, LOG_FMT("open_fn failed for fd = %d"), newfd);
  71. return ret;
  72. }
  73. }
  74. return ESP_OK;
  75. }
  76. }
  77. ESP_LOGD(TAG, LOG_FMT("unable to launch session for fd = %d"), newfd);
  78. return ESP_FAIL;
  79. }
  80. void httpd_sess_free_ctx(void *ctx, httpd_free_ctx_fn_t free_fn)
  81. {
  82. if (ctx) {
  83. if (free_fn) {
  84. free_fn(ctx);
  85. } else {
  86. free(ctx);
  87. }
  88. }
  89. }
  90. void *httpd_sess_get_ctx(httpd_handle_t handle, int sockfd)
  91. {
  92. struct sock_db *sd = httpd_sess_get(handle, sockfd);
  93. if (sd == NULL) {
  94. return NULL;
  95. }
  96. /* Check if the function has been called from inside a
  97. * request handler, in which case fetch the context from
  98. * the httpd_req_t structure */
  99. struct httpd_data *hd = (struct httpd_data *) handle;
  100. if (hd->hd_req_aux.sd == sd) {
  101. return hd->hd_req.sess_ctx;
  102. }
  103. return sd->ctx;
  104. }
  105. void httpd_sess_set_ctx(httpd_handle_t handle, int sockfd, void *ctx, httpd_free_ctx_fn_t free_fn)
  106. {
  107. struct sock_db *sd = httpd_sess_get(handle, sockfd);
  108. if (sd == NULL) {
  109. return;
  110. }
  111. /* Check if the function has been called from inside a
  112. * request handler, in which case set the context inside
  113. * the httpd_req_t structure */
  114. struct httpd_data *hd = (struct httpd_data *) handle;
  115. if (hd->hd_req_aux.sd == sd) {
  116. if (hd->hd_req.sess_ctx != ctx) {
  117. /* Don't free previous context if it is in sockdb
  118. * as it will be freed inside httpd_req_cleanup() */
  119. if (sd->ctx != hd->hd_req.sess_ctx) {
  120. /* Free previous context */
  121. httpd_sess_free_ctx(hd->hd_req.sess_ctx, hd->hd_req.free_ctx);
  122. }
  123. hd->hd_req.sess_ctx = ctx;
  124. }
  125. hd->hd_req.free_ctx = free_fn;
  126. return;
  127. }
  128. /* Else set the context inside the sock_db structure */
  129. if (sd->ctx != ctx) {
  130. /* Free previous context */
  131. httpd_sess_free_ctx(sd->ctx, sd->free_ctx);
  132. sd->ctx = ctx;
  133. }
  134. sd->free_ctx = free_fn;
  135. }
  136. void *httpd_sess_get_transport_ctx(httpd_handle_t handle, int sockfd)
  137. {
  138. struct sock_db *sd = httpd_sess_get(handle, sockfd);
  139. if (sd == NULL) {
  140. return NULL;
  141. }
  142. return sd->transport_ctx;
  143. }
  144. void httpd_sess_set_transport_ctx(httpd_handle_t handle, int sockfd, void *ctx, httpd_free_ctx_fn_t free_fn)
  145. {
  146. struct sock_db *sd = httpd_sess_get(handle, sockfd);
  147. if (sd == NULL) {
  148. return;
  149. }
  150. if (sd->transport_ctx != ctx) {
  151. /* Free previous transport context */
  152. httpd_sess_free_ctx(sd->transport_ctx, sd->free_transport_ctx);
  153. sd->transport_ctx = ctx;
  154. }
  155. sd->free_transport_ctx = free_fn;
  156. }
  157. void httpd_sess_set_descriptors(struct httpd_data *hd,
  158. fd_set *fdset, int *maxfd)
  159. {
  160. int i;
  161. *maxfd = -1;
  162. for (i = 0; i < hd->config.max_open_sockets; i++) {
  163. if (hd->hd_sd[i].fd != -1) {
  164. FD_SET(hd->hd_sd[i].fd, fdset);
  165. if (hd->hd_sd[i].fd > *maxfd) {
  166. *maxfd = hd->hd_sd[i].fd;
  167. }
  168. }
  169. }
  170. }
  171. /** Check if a FD is valid */
  172. static int fd_is_valid(int fd)
  173. {
  174. return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
  175. }
  176. static inline uint64_t httpd_sess_get_lru_counter(void)
  177. {
  178. static uint64_t lru_counter = 0;
  179. return ++lru_counter;
  180. }
  181. void httpd_sess_delete_invalid(struct httpd_data *hd)
  182. {
  183. for (int i = 0; i < hd->config.max_open_sockets; i++) {
  184. if (hd->hd_sd[i].fd != -1 && !fd_is_valid(hd->hd_sd[i].fd)) {
  185. ESP_LOGW(TAG, LOG_FMT("Closing invalid socket %d"), hd->hd_sd[i].fd);
  186. httpd_sess_delete(hd, hd->hd_sd[i].fd);
  187. }
  188. }
  189. }
  190. int httpd_sess_delete(struct httpd_data *hd, int fd)
  191. {
  192. ESP_LOGD(TAG, LOG_FMT("fd = %d"), fd);
  193. int i;
  194. int pre_sess_fd = -1;
  195. for (i = 0; i < hd->config.max_open_sockets; i++) {
  196. if (hd->hd_sd[i].fd == fd) {
  197. /* global close handler */
  198. if (hd->config.close_fn) {
  199. hd->config.close_fn(hd, fd);
  200. }
  201. /* release 'user' context */
  202. if (hd->hd_sd[i].ctx) {
  203. if (hd->hd_sd[i].free_ctx) {
  204. hd->hd_sd[i].free_ctx(hd->hd_sd[i].ctx);
  205. } else {
  206. free(hd->hd_sd[i].ctx);
  207. }
  208. hd->hd_sd[i].ctx = NULL;
  209. hd->hd_sd[i].free_ctx = NULL;
  210. }
  211. /* release 'transport' context */
  212. if (hd->hd_sd[i].transport_ctx) {
  213. if (hd->hd_sd[i].free_transport_ctx) {
  214. hd->hd_sd[i].free_transport_ctx(hd->hd_sd[i].transport_ctx);
  215. } else {
  216. free(hd->hd_sd[i].transport_ctx);
  217. }
  218. hd->hd_sd[i].transport_ctx = NULL;
  219. hd->hd_sd[i].free_transport_ctx = NULL;
  220. }
  221. /* mark session slot as available */
  222. hd->hd_sd[i].fd = -1;
  223. break;
  224. } else if (hd->hd_sd[i].fd != -1) {
  225. /* Return the fd just preceding the one being
  226. * deleted so that iterator can continue from
  227. * the correct fd */
  228. pre_sess_fd = hd->hd_sd[i].fd;
  229. }
  230. }
  231. return pre_sess_fd;
  232. }
  233. void httpd_sess_init(struct httpd_data *hd)
  234. {
  235. int i;
  236. for (i = 0; i < hd->config.max_open_sockets; i++) {
  237. hd->hd_sd[i].fd = -1;
  238. hd->hd_sd[i].ctx = NULL;
  239. }
  240. }
  241. bool httpd_sess_pending(struct httpd_data *hd, int fd)
  242. {
  243. struct sock_db *sd = httpd_sess_get(hd, fd);
  244. if (! sd) {
  245. return ESP_FAIL;
  246. }
  247. if (sd->pending_fn) {
  248. // test if there's any data to be read (besides read() function, which is handled by select() in the main httpd loop)
  249. // this should check e.g. for the SSL data buffer
  250. if (sd->pending_fn(hd, fd) > 0) {
  251. return true;
  252. }
  253. }
  254. return (sd->pending_len != 0);
  255. }
  256. /* This MUST return ESP_OK on successful execution. If any other
  257. * value is returned, everything related to this socket will be
  258. * cleaned up and the socket will be closed.
  259. */
  260. esp_err_t httpd_sess_process(struct httpd_data *hd, int newfd)
  261. {
  262. struct sock_db *sd = httpd_sess_get(hd, newfd);
  263. if (! sd) {
  264. return ESP_FAIL;
  265. }
  266. ESP_LOGD(TAG, LOG_FMT("httpd_req_new"));
  267. if (httpd_req_new(hd, sd) != ESP_OK) {
  268. return ESP_FAIL;
  269. }
  270. ESP_LOGD(TAG, LOG_FMT("httpd_req_delete"));
  271. if (httpd_req_delete(hd) != ESP_OK) {
  272. return ESP_FAIL;
  273. }
  274. ESP_LOGD(TAG, LOG_FMT("success"));
  275. sd->lru_counter = httpd_sess_get_lru_counter();
  276. return ESP_OK;
  277. }
  278. esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd)
  279. {
  280. if (handle == NULL) {
  281. return ESP_ERR_INVALID_ARG;
  282. }
  283. /* Search for the socket database entry */
  284. struct httpd_data *hd = (struct httpd_data *) handle;
  285. int i;
  286. for (i = 0; i < hd->config.max_open_sockets; i++) {
  287. if (hd->hd_sd[i].fd == sockfd) {
  288. hd->hd_sd[i].lru_counter = httpd_sess_get_lru_counter();
  289. return ESP_OK;
  290. }
  291. }
  292. return ESP_ERR_NOT_FOUND;
  293. }
  294. esp_err_t httpd_sess_close_lru(struct httpd_data *hd)
  295. {
  296. uint64_t lru_counter = UINT64_MAX;
  297. int lru_fd = -1;
  298. int i;
  299. for (i = 0; i < hd->config.max_open_sockets; i++) {
  300. /* If a descriptor is -1, there is no need to close any session.
  301. * So, we can return from here, without finding the Least Recently Used
  302. * session
  303. */
  304. if (hd->hd_sd[i].fd == -1) {
  305. return ESP_OK;
  306. }
  307. if (hd->hd_sd[i].lru_counter < lru_counter) {
  308. lru_counter = hd->hd_sd[i].lru_counter;
  309. lru_fd = hd->hd_sd[i].fd;
  310. }
  311. }
  312. ESP_LOGD(TAG, LOG_FMT("fd = %d"), lru_fd);
  313. struct sock_db *sd = httpd_sess_get(hd, lru_fd);
  314. sd->lru_socket = true;
  315. return httpd_sess_trigger_close(hd, lru_fd);
  316. }
  317. int httpd_sess_iterate(struct httpd_data *hd, int start_fd)
  318. {
  319. int start_index = 0;
  320. int i;
  321. if (start_fd != -1) {
  322. /* Take our index to where this fd is stored */
  323. for (i = 0; i < hd->config.max_open_sockets; i++) {
  324. if (hd->hd_sd[i].fd == start_fd) {
  325. start_index = i + 1;
  326. break;
  327. }
  328. }
  329. }
  330. for (i = start_index; i < hd->config.max_open_sockets; i++) {
  331. if (hd->hd_sd[i].fd != -1) {
  332. return hd->hd_sd[i].fd;
  333. }
  334. }
  335. return -1;
  336. }
  337. static void httpd_sess_close(void *arg)
  338. {
  339. struct sock_db *sock_db = (struct sock_db *)arg;
  340. if (sock_db) {
  341. if (sock_db->lru_counter == 0 && !sock_db->lru_socket) {
  342. ESP_LOGD(TAG, "Skipping session close for %d as it seems to be a race condition", sock_db->fd);
  343. return;
  344. }
  345. int fd = sock_db->fd;
  346. sock_db->lru_socket = false;
  347. struct httpd_data *hd = (struct httpd_data *) sock_db->handle;
  348. httpd_sess_delete(hd, fd);
  349. close(fd);
  350. }
  351. }
  352. esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd)
  353. {
  354. struct sock_db *sock_db = httpd_sess_get(handle, sockfd);
  355. if (sock_db) {
  356. return httpd_queue_work(handle, httpd_sess_close, sock_db);
  357. }
  358. return ESP_ERR_NOT_FOUND;
  359. }