esp_http_server.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. #ifndef _ESP_HTTP_SERVER_H_
  15. #define _ESP_HTTP_SERVER_H_
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <freertos/FreeRTOS.h>
  19. #include <freertos/task.h>
  20. #include <http_parser.h>
  21. #include <sdkconfig.h>
  22. #include <esp_err.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /*
  27. note: esp_https_server.h includes a customized copy of this
  28. initializer that should be kept in sync
  29. */
  30. #define HTTPD_DEFAULT_CONFIG() { \
  31. .task_priority = tskIDLE_PRIORITY+5, \
  32. .stack_size = 4096, \
  33. .server_port = 80, \
  34. .ctrl_port = 32768, \
  35. .max_open_sockets = 7, \
  36. .max_uri_handlers = 8, \
  37. .max_resp_headers = 8, \
  38. .backlog_conn = 5, \
  39. .lru_purge_enable = false, \
  40. .recv_wait_timeout = 5, \
  41. .send_wait_timeout = 5, \
  42. .global_user_ctx = NULL, \
  43. .global_user_ctx_free_fn = NULL, \
  44. .global_transport_ctx = NULL, \
  45. .global_transport_ctx_free_fn = NULL, \
  46. .open_fn = NULL, \
  47. .close_fn = NULL, \
  48. }
  49. #define ESP_ERR_HTTPD_BASE (0x8000) /*!< Starting number of HTTPD error codes */
  50. #define ESP_ERR_HTTPD_HANDLERS_FULL (ESP_ERR_HTTPD_BASE + 1) /*!< All slots for registering URI handlers have been consumed */
  51. #define ESP_ERR_HTTPD_HANDLER_EXISTS (ESP_ERR_HTTPD_BASE + 2) /*!< URI handler with same method and target URI already registered */
  52. #define ESP_ERR_HTTPD_INVALID_REQ (ESP_ERR_HTTPD_BASE + 3) /*!< Invalid request pointer */
  53. #define ESP_ERR_HTTPD_RESULT_TRUNC (ESP_ERR_HTTPD_BASE + 4) /*!< Result string truncated */
  54. #define ESP_ERR_HTTPD_RESP_HDR (ESP_ERR_HTTPD_BASE + 5) /*!< Response header field larger than supported */
  55. #define ESP_ERR_HTTPD_RESP_SEND (ESP_ERR_HTTPD_BASE + 6) /*!< Error occured while sending response packet */
  56. #define ESP_ERR_HTTPD_ALLOC_MEM (ESP_ERR_HTTPD_BASE + 7) /*!< Failed to dynamically allocate memory for resource */
  57. #define ESP_ERR_HTTPD_TASK (ESP_ERR_HTTPD_BASE + 8) /*!< Failed to launch server task/thread */
  58. /* ************** Group: Initialization ************** */
  59. /** @name Initialization
  60. * APIs related to the Initialization of the web server
  61. * @{
  62. */
  63. /**
  64. * @brief HTTP Server Instance Handle
  65. *
  66. * Every instance of the server will have a unique handle.
  67. */
  68. typedef void* httpd_handle_t;
  69. /**
  70. * @brief HTTP Method Type wrapper over "enum http_method"
  71. * available in "http_parser" library
  72. */
  73. typedef enum http_method httpd_method_t;
  74. /**
  75. * @brief Prototype for freeing context data (if any)
  76. * @param[in] ctx : object to free
  77. */
  78. typedef void (*httpd_free_ctx_fn_t)(void *ctx);
  79. /**
  80. * @brief Function prototype for opening a session.
  81. *
  82. * Called immediately after the socket was opened to set up the send/recv functions and
  83. * other parameters of the socket.
  84. *
  85. * @param[in] hd : server instance
  86. * @param[in] sockfd : session socket file descriptor
  87. * @return status
  88. */
  89. typedef esp_err_t (*httpd_open_func_t)(httpd_handle_t hd, int sockfd);
  90. /**
  91. * @brief Function prototype for closing a session.
  92. *
  93. * @note It's possible that the socket descriptor is invalid at this point, the function
  94. * is called for all terminated sessions. Ensure proper handling of return codes.
  95. *
  96. * @param[in] hd : server instance
  97. * @param[in] sockfd : session socket file descriptor
  98. */
  99. typedef void (*httpd_close_func_t)(httpd_handle_t hd, int sockfd);
  100. /**
  101. * @brief HTTP Server Configuration Structure
  102. *
  103. * @note Use HTTPD_DEFAULT_CONFIG() to initialize the configuration
  104. * to a default value and then modify only those fields that are
  105. * specifically determined by the use case.
  106. */
  107. typedef struct httpd_config {
  108. unsigned task_priority; /*!< Priority of FreeRTOS task which runs the server */
  109. size_t stack_size; /*!< The maximum stack size allowed for the server task */
  110. /**
  111. * TCP Port number for receiving and transmitting HTTP traffic
  112. */
  113. uint16_t server_port;
  114. /**
  115. * UDP Port number for asynchronously exchanging control signals
  116. * between various components of the server
  117. */
  118. uint16_t ctrl_port;
  119. uint16_t max_open_sockets; /*!< Max number of sockets/clients connected at any time*/
  120. uint16_t max_uri_handlers; /*!< Maximum allowed uri handlers */
  121. uint16_t max_resp_headers; /*!< Maximum allowed additional headers in HTTP response */
  122. uint16_t backlog_conn; /*!< Number of backlog connections */
  123. bool lru_purge_enable; /*!< Purge "Least Recently Used" connection */
  124. uint16_t recv_wait_timeout; /*!< Timeout for recv function (in seconds)*/
  125. uint16_t send_wait_timeout; /*!< Timeout for send function (in seconds)*/
  126. /**
  127. * Global user context.
  128. *
  129. * This field can be used to store arbitrary user data within the server context.
  130. * The value can be retrieved using the server handle, available e.g. in the httpd_req_t struct.
  131. *
  132. * When shutting down, the server frees up the user context by
  133. * calling free() on the global_user_ctx field. If you wish to use a custom
  134. * function for freeing the global user context, please specify that here.
  135. */
  136. void * global_user_ctx;
  137. /**
  138. * Free function for global user context
  139. */
  140. httpd_free_ctx_fn_t global_user_ctx_free_fn;
  141. /**
  142. * Global transport context.
  143. *
  144. * Similar to global_user_ctx, but used for session encoding or encryption (e.g. to hold the SSL context).
  145. * It will be freed using free(), unless global_transport_ctx_free_fn is specified.
  146. */
  147. void * global_transport_ctx;
  148. /**
  149. * Free function for global transport context
  150. */
  151. httpd_free_ctx_fn_t global_transport_ctx_free_fn;
  152. /**
  153. * Custom session opening callback.
  154. *
  155. * Called on a new session socket just after accept(), but before reading any data.
  156. *
  157. * This is an opportunity to set up e.g. SSL encryption using global_transport_ctx
  158. * and the send/recv/pending session overrides.
  159. *
  160. * If a context needs to be maintained between these functions, store it in the session using
  161. * httpd_sess_set_transport_ctx() and retrieve it later with httpd_sess_get_transport_ctx()
  162. */
  163. httpd_open_func_t open_fn;
  164. /**
  165. * Custom session closing callback.
  166. *
  167. * Called when a session is deleted, before freeing user and transport contexts and before
  168. * closing the socket. This is a place for custom de-init code common to all sockets.
  169. *
  170. * Set the user or transport context to NULL if it was freed here, so the server does not
  171. * try to free it again.
  172. *
  173. * This function is run for all terminated sessions, including sessions where the socket
  174. * was closed by the network stack - that is, the file descriptor may not be valid anymore.
  175. */
  176. httpd_close_func_t close_fn;
  177. } httpd_config_t;
  178. /**
  179. * @brief Starts the web server
  180. *
  181. * Create an instance of HTTP server and allocate memory/resources for it
  182. * depending upon the specified configuration.
  183. *
  184. * Example usage:
  185. * @code{c}
  186. *
  187. * //Function for starting the webserver
  188. * httpd_handle_t start_webserver(void)
  189. * {
  190. * // Generate default configuration
  191. * httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  192. *
  193. * // Empty handle to http_server
  194. * httpd_handle_t server = NULL;
  195. *
  196. * // Start the httpd server
  197. * if (httpd_start(&server, &config) == ESP_OK) {
  198. * // Register URI handlers
  199. * httpd_register_uri_handler(server, &uri_get);
  200. * httpd_register_uri_handler(server, &uri_post);
  201. * }
  202. * // If server failed to start, handle will be NULL
  203. * return server;
  204. * }
  205. *
  206. * @endcode
  207. *
  208. * @param[in] config : Configuration for new instance of the server
  209. * @param[out] handle : Handle to newly created instance of the server. NULL on error
  210. * @return
  211. * - ESP_OK : Instance created successfully
  212. * - ESP_ERR_INVALID_ARG : Null argument(s)
  213. * - ESP_ERR_HTTPD_ALLOC_MEM : Failed to allocate memory for instance
  214. * - ESP_ERR_HTTPD_TASK : Failed to launch server task
  215. */
  216. esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config);
  217. /**
  218. * @brief Stops the web server
  219. *
  220. * Deallocates memory/resources used by an HTTP server instance and
  221. * deletes it. Once deleted the handle can no longer be used for accessing
  222. * the instance.
  223. *
  224. * Example usage:
  225. * @code{c}
  226. *
  227. * // Function for stopping the webserver
  228. * void stop_webserver(httpd_handle_t server)
  229. * {
  230. * // Ensure handle is non NULL
  231. * if (server != NULL) {
  232. * // Stop the httpd server
  233. * httpd_stop(server);
  234. * }
  235. * }
  236. *
  237. * @endcode
  238. *
  239. * @param[in] handle Handle to server returned by httpd_start
  240. * @return
  241. * - ESP_OK : Server stopped successfully
  242. * - ESP_ERR_INVALID_ARG : Handle argument is Null
  243. */
  244. esp_err_t httpd_stop(httpd_handle_t handle);
  245. /** End of Group Initialization
  246. * @}
  247. */
  248. /* ************** Group: URI Handlers ************** */
  249. /** @name URI Handlers
  250. * APIs related to the URI handlers
  251. * @{
  252. */
  253. /* Max supported HTTP request header length */
  254. #define HTTPD_MAX_REQ_HDR_LEN CONFIG_HTTPD_MAX_REQ_HDR_LEN
  255. /* Max supported HTTP request URI length */
  256. #define HTTPD_MAX_URI_LEN CONFIG_HTTPD_MAX_URI_LEN
  257. /**
  258. * @brief HTTP Request Data Structure
  259. */
  260. typedef struct httpd_req {
  261. httpd_handle_t handle; /*!< Handle to server instance */
  262. int method; /*!< The type of HTTP request, -1 if unsupported method */
  263. const char uri[HTTPD_MAX_URI_LEN + 1]; /*!< The URI of this request (1 byte extra for null termination) */
  264. size_t content_len; /*!< Length of the request body */
  265. void *aux; /*!< Internally used members */
  266. /**
  267. * User context pointer passed during URI registration.
  268. */
  269. void *user_ctx;
  270. /**
  271. * Session Context Pointer
  272. *
  273. * A session context. Contexts are maintained across 'sessions' for a
  274. * given open TCP connection. One session could have multiple request
  275. * responses. The web server will ensure that the context persists
  276. * across all these request and responses.
  277. *
  278. * By default, this is NULL. URI Handlers can set this to any meaningful
  279. * value.
  280. *
  281. * If the underlying socket gets closed, and this pointer is non-NULL,
  282. * the web server will free up the context by calling free(), unless
  283. * free_ctx function is set.
  284. */
  285. void *sess_ctx;
  286. /**
  287. * Pointer to free context hook
  288. *
  289. * Function to free session context
  290. *
  291. * If the web server's socket closes, it frees up the session context by
  292. * calling free() on the sess_ctx member. If you wish to use a custom
  293. * function for freeing the session context, please specify that here.
  294. */
  295. httpd_free_ctx_fn_t free_ctx;
  296. } httpd_req_t;
  297. /**
  298. * @brief Structure for URI handler
  299. */
  300. typedef struct httpd_uri {
  301. const char *uri; /*!< The URI to handle */
  302. httpd_method_t method; /*!< Method supported by the URI */
  303. /**
  304. * Handler to call for supported request method. This must
  305. * return ESP_OK, or else the underlying socket will be closed.
  306. */
  307. esp_err_t (*handler)(httpd_req_t *r);
  308. /**
  309. * Pointer to user context data which will be available to handler
  310. */
  311. void *user_ctx;
  312. } httpd_uri_t;
  313. /**
  314. * @brief Registers a URI handler
  315. *
  316. * @note URI handlers can be registered in real time as long as the
  317. * server handle is valid.
  318. *
  319. * Example usage:
  320. * @code{c}
  321. *
  322. * esp_err_t my_uri_handler(httpd_req_t* req)
  323. * {
  324. * // Recv , Process and Send
  325. * ....
  326. * ....
  327. * ....
  328. *
  329. * // Fail condition
  330. * if (....) {
  331. * // Return fail to close session //
  332. * return ESP_FAIL;
  333. * }
  334. *
  335. * // On success
  336. * return ESP_OK;
  337. * }
  338. *
  339. * // URI handler structure
  340. * httpd_uri_t my_uri {
  341. * .uri = "/my_uri/path/xyz",
  342. * .method = HTTPD_GET,
  343. * .handler = my_uri_handler,
  344. * .user_ctx = NULL
  345. * };
  346. *
  347. * // Register handler
  348. * if (httpd_register_uri_handler(server_handle, &my_uri) != ESP_OK) {
  349. * // If failed to register handler
  350. * ....
  351. * }
  352. *
  353. * @endcode
  354. *
  355. * @param[in] handle handle to HTTPD server instance
  356. * @param[in] uri_handler pointer to handler that needs to be registered
  357. *
  358. * @return
  359. * - ESP_OK : On successfully registering the handler
  360. * - ESP_ERR_INVALID_ARG : Null arguments
  361. * - ESP_ERR_HTTPD_HANDLERS_FULL : If no slots left for new handler
  362. * - ESP_ERR_HTTPD_HANDLER_EXISTS : If handler with same URI and
  363. * method is already registered
  364. */
  365. esp_err_t httpd_register_uri_handler(httpd_handle_t handle,
  366. const httpd_uri_t *uri_handler);
  367. /**
  368. * @brief Unregister a URI handler
  369. *
  370. * @param[in] handle handle to HTTPD server instance
  371. * @param[in] uri URI string
  372. * @param[in] method HTTP method
  373. *
  374. * @return
  375. * - ESP_OK : On successfully deregistering the handler
  376. * - ESP_ERR_INVALID_ARG : Null arguments
  377. * - ESP_ERR_NOT_FOUND : Handler with specified URI and method not found
  378. */
  379. esp_err_t httpd_unregister_uri_handler(httpd_handle_t handle,
  380. const char *uri, httpd_method_t method);
  381. /**
  382. * @brief Unregister all URI handlers with the specified uri string
  383. *
  384. * @param[in] handle handle to HTTPD server instance
  385. * @param[in] uri uri string specifying all handlers that need
  386. * to be deregisterd
  387. *
  388. * @return
  389. * - ESP_OK : On successfully deregistering all such handlers
  390. * - ESP_ERR_INVALID_ARG : Null arguments
  391. * - ESP_ERR_NOT_FOUND : No handler registered with specified uri string
  392. */
  393. esp_err_t httpd_unregister_uri(httpd_handle_t handle, const char* uri);
  394. /** End of URI Handlers
  395. * @}
  396. */
  397. /* ************** Group: TX/RX ************** */
  398. /** @name TX / RX
  399. * Prototype for HTTPDs low-level send/recv functions
  400. * @{
  401. */
  402. #define HTTPD_SOCK_ERR_FAIL -1
  403. #define HTTPD_SOCK_ERR_INVALID -2
  404. #define HTTPD_SOCK_ERR_TIMEOUT -3
  405. /**
  406. * @brief Prototype for HTTPDs low-level send function
  407. *
  408. * @note User specified send function must handle errors internally,
  409. * depending upon the set value of errno, and return specific
  410. * HTTPD_SOCK_ERR_ codes, which will eventually be conveyed as
  411. * return value of httpd_send() function
  412. *
  413. * @param[in] hd : server instance
  414. * @param[in] sockfd : session socket file descriptor
  415. * @param[in] buf : buffer with bytes to send
  416. * @param[in] buf_len : data size
  417. * @param[in] flags : flags for the send() function
  418. * @return
  419. * - Bytes : The number of bytes sent successfully
  420. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  421. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  422. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()
  423. */
  424. typedef int (*httpd_send_func_t)(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags);
  425. /**
  426. * @brief Prototype for HTTPDs low-level recv function
  427. *
  428. * @note User specified recv function must handle errors internally,
  429. * depending upon the set value of errno, and return specific
  430. * HTTPD_SOCK_ERR_ codes, which will eventually be conveyed as
  431. * return value of httpd_req_recv() function
  432. *
  433. * @param[in] hd : server instance
  434. * @param[in] sockfd : session socket file descriptor
  435. * @param[in] buf : buffer with bytes to send
  436. * @param[in] buf_len : data size
  437. * @param[in] flags : flags for the send() function
  438. * @return
  439. * - Bytes : The number of bytes received successfully
  440. * - 0 : Buffer length parameter is zero / connection closed by peer
  441. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  442. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  443. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()
  444. */
  445. typedef int (*httpd_recv_func_t)(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags);
  446. /**
  447. * @brief Prototype for HTTPDs low-level "get pending bytes" function
  448. *
  449. * @note User specified pending function must handle errors internally,
  450. * depending upon the set value of errno, and return specific
  451. * HTTPD_SOCK_ERR_ codes, which will be handled accordingly in
  452. * the server task.
  453. *
  454. * @param[in] hd : server instance
  455. * @param[in] sockfd : session socket file descriptor
  456. * @return
  457. * - Bytes : The number of bytes waiting to be received
  458. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  459. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket pending()
  460. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket pending()
  461. */
  462. typedef int (*httpd_pending_func_t)(httpd_handle_t hd, int sockfd);
  463. /** End of TX / RX
  464. * @}
  465. */
  466. /* ************** Group: Request/Response ************** */
  467. /** @name Request / Response
  468. * APIs related to the data send/receive by URI handlers.
  469. * These APIs are supposed to be called only from the context of
  470. * a URI handler where httpd_req_t* request pointer is valid.
  471. * @{
  472. */
  473. /**
  474. * @brief Override web server's receive function (by session FD)
  475. *
  476. * This function overrides the web server's receive function. This same function is
  477. * used to read HTTP request packets.
  478. *
  479. * @note This API is supposed to be called either from the context of
  480. * - an http session APIs where sockfd is a valid parameter
  481. * - a URI handler where sockfd is obtained using httpd_req_to_sockfd()
  482. *
  483. * @param[in] hd HTTPD instance handle
  484. * @param[in] sockfd Session socket FD
  485. * @param[in] recv_func The receive function to be set for this session
  486. *
  487. * @return
  488. * - ESP_OK : On successfully registering override
  489. * - ESP_ERR_INVALID_ARG : Null arguments
  490. */
  491. esp_err_t httpd_sess_set_recv_override(httpd_handle_t hd, int sockfd, httpd_recv_func_t recv_func);
  492. /**
  493. * @brief Override web server's send function (by session FD)
  494. *
  495. * This function overrides the web server's send function. This same function is
  496. * used to send out any response to any HTTP request.
  497. *
  498. * @note This API is supposed to be called either from the context of
  499. * - an http session APIs where sockfd is a valid parameter
  500. * - a URI handler where sockfd is obtained using httpd_req_to_sockfd()
  501. *
  502. * @param[in] hd HTTPD instance handle
  503. * @param[in] sockfd Session socket FD
  504. * @param[in] send_func The send function to be set for this session
  505. *
  506. * @return
  507. * - ESP_OK : On successfully registering override
  508. * - ESP_ERR_INVALID_ARG : Null arguments
  509. */
  510. esp_err_t httpd_sess_set_send_override(httpd_handle_t hd, int sockfd, httpd_send_func_t send_func);
  511. /**
  512. * @brief Override web server's pending function (by session FD)
  513. *
  514. * This function overrides the web server's pending function. This function is
  515. * used to test for pending bytes in a socket.
  516. *
  517. * @note This API is supposed to be called either from the context of
  518. * - an http session APIs where sockfd is a valid parameter
  519. * - a URI handler where sockfd is obtained using httpd_req_to_sockfd()
  520. *
  521. * @param[in] hd HTTPD instance handle
  522. * @param[in] sockfd Session socket FD
  523. * @param[in] pending_func The receive function to be set for this session
  524. *
  525. * @return
  526. * - ESP_OK : On successfully registering override
  527. * - ESP_ERR_INVALID_ARG : Null arguments
  528. */
  529. esp_err_t httpd_sess_set_pending_override(httpd_handle_t hd, int sockfd, httpd_pending_func_t pending_func);
  530. /**
  531. * @brief Get the Socket Descriptor from the HTTP request
  532. *
  533. * This API will return the socket descriptor of the session for
  534. * which URI handler was executed on reception of HTTP request.
  535. * This is useful when user wants to call functions that require
  536. * session socket fd, from within a URI handler, ie. :
  537. * httpd_sess_get_ctx(),
  538. * httpd_sess_trigger_close(),
  539. * httpd_sess_update_timestamp().
  540. *
  541. * @note This API is supposed to be called only from the context of
  542. * a URI handler where httpd_req_t* request pointer is valid.
  543. *
  544. * @param[in] r The request whose socket descriptor should be found
  545. *
  546. * @return
  547. * - Socket descriptor : The socket descriptor for this request
  548. * - -1 : Invalid/NULL request pointer
  549. */
  550. int httpd_req_to_sockfd(httpd_req_t *r);
  551. /**
  552. * @brief API to read content data from the HTTP request
  553. *
  554. * This API will read HTTP content data from the HTTP request into
  555. * provided buffer. Use content_len provided in httpd_req_t structure
  556. * to know the length of data to be fetched. If content_len is too
  557. * large for the buffer then user may have to make multiple calls to
  558. * this function, each time fetching 'buf_len' number of bytes,
  559. * while the pointer to content data is incremented internally by
  560. * the same number.
  561. *
  562. * @note
  563. * - This API is supposed to be called only from the context of
  564. * a URI handler where httpd_req_t* request pointer is valid.
  565. * - If an error is returned, the URI handler must further return an error.
  566. * This will ensure that the erroneous socket is closed and cleaned up by
  567. * the web server.
  568. * - Presently Chunked Encoding is not supported
  569. *
  570. * @param[in] r The request being responded to
  571. * @param[in] buf Pointer to a buffer that the data will be read into
  572. * @param[in] buf_len Length of the buffer
  573. *
  574. * @return
  575. * - Bytes : Number of bytes read into the buffer successfully
  576. * - 0 : Buffer length parameter is zero / connection closed by peer
  577. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  578. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  579. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()
  580. */
  581. int httpd_req_recv(httpd_req_t *r, char *buf, size_t buf_len);
  582. /**
  583. * @brief Search for a field in request headers and
  584. * return the string length of it's value
  585. *
  586. * @note
  587. * - This API is supposed to be called only from the context of
  588. * a URI handler where httpd_req_t* request pointer is valid.
  589. * - Once httpd_resp_send() API is called all request headers
  590. * are purged, so request headers need be copied into separate
  591. * buffers if they are required later.
  592. *
  593. * @param[in] r The request being responded to
  594. * @param[in] field The header field to be searched in the request
  595. *
  596. * @return
  597. * - Length : If field is found in the request URL
  598. * - Zero : Field not found / Invalid request / Null arguments
  599. */
  600. size_t httpd_req_get_hdr_value_len(httpd_req_t *r, const char *field);
  601. /**
  602. * @brief Get the value string of a field from the request headers
  603. *
  604. * @note
  605. * - This API is supposed to be called only from the context of
  606. * a URI handler where httpd_req_t* request pointer is valid.
  607. * - Once httpd_resp_send() API is called all request headers
  608. * are purged, so request headers need be copied into separate
  609. * buffers if they are required later.
  610. * - If output size is greater than input, then the value is truncated,
  611. * accompanied by truncation error as return value.
  612. * - Use httpd_req_get_hdr_value_len() to know the right buffer length
  613. *
  614. * @param[in] r The request being responded to
  615. * @param[in] field The field to be searched in the header
  616. * @param[out] val Pointer to the buffer into which the value will be copied if the field is found
  617. * @param[in] val_size Size of the user buffer "val"
  618. *
  619. * @return
  620. * - ESP_OK : Field found in the request header and value string copied
  621. * - ESP_ERR_NOT_FOUND : Key not found
  622. * - ESP_ERR_INVALID_ARG : Null arguments
  623. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid HTTP request pointer
  624. * - ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
  625. */
  626. esp_err_t httpd_req_get_hdr_value_str(httpd_req_t *r, const char *field, char *val, size_t val_size);
  627. /**
  628. * @brief Get Query string length from the request URL
  629. *
  630. * @note This API is supposed to be called only from the context of
  631. * a URI handler where httpd_req_t* request pointer is valid
  632. *
  633. * @param[in] r The request being responded to
  634. *
  635. * @return
  636. * - Length : Query is found in the request URL
  637. * - Zero : Query not found / Null arguments / Invalid request
  638. */
  639. size_t httpd_req_get_url_query_len(httpd_req_t *r);
  640. /**
  641. * @brief Get Query string from the request URL
  642. *
  643. * @note
  644. * - Presently, the user can fetch the full URL query string, but decoding
  645. * will have to be performed by the user. Request headers can be read using
  646. * httpd_req_get_hdr_value_str() to know the 'Content-Type' (eg. Content-Type:
  647. * application/x-www-form-urlencoded) and then the appropriate decoding
  648. * algorithm needs to be applied.
  649. * - This API is supposed to be called only from the context of
  650. * a URI handler where httpd_req_t* request pointer is valid
  651. * - If output size is greater than input, then the value is truncated,
  652. * accompanied by truncation error as return value
  653. * - Use httpd_req_get_url_query_len() to know the right buffer length
  654. *
  655. * @param[in] r The request being responded to
  656. * @param[out] buf Pointer to the buffer into which the query string will be copied (if found)
  657. * @param[in] buf_len Length of output buffer
  658. *
  659. * @return
  660. * - ESP_OK : Query is found in the request URL and copied to buffer
  661. * - ESP_ERR_NOT_FOUND : Query not found
  662. * - ESP_ERR_INVALID_ARG : Null arguments
  663. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid HTTP request pointer
  664. * - ESP_ERR_HTTPD_RESULT_TRUNC : Query string truncated
  665. */
  666. esp_err_t httpd_req_get_url_query_str(httpd_req_t *r, char *buf, size_t buf_len);
  667. /**
  668. * @brief Helper function to get a URL query tag from a query
  669. * string of the type param1=val1&param2=val2
  670. *
  671. * @note
  672. * - The components of URL query string (keys and values) are not URLdecoded.
  673. * The user must check for 'Content-Type' field in the request headers and
  674. * then depending upon the specified encoding (URLencoded or otherwise) apply
  675. * the appropriate decoding algorithm.
  676. * - If actual value size is greater than val_size, then the value is truncated,
  677. * accompanied by truncation error as return value.
  678. *
  679. * @param[in] qry Pointer to query string
  680. * @param[in] key The key to be searched in the query string
  681. * @param[out] val Pointer to the buffer into which the value will be copied if the key is found
  682. * @param[in] val_size Size of the user buffer "val"
  683. *
  684. * @return
  685. * - ESP_OK : Key is found in the URL query string and copied to buffer
  686. * - ESP_ERR_NOT_FOUND : Key not found
  687. * - ESP_ERR_INVALID_ARG : Null arguments
  688. * - ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
  689. */
  690. esp_err_t httpd_query_key_value(const char *qry, const char *key, char *val, size_t val_size);
  691. /**
  692. * @brief API to send a complete HTTP response.
  693. *
  694. * This API will send the data as an HTTP response to the request.
  695. * This assumes that you have the entire response ready in a single
  696. * buffer. If you wish to send response in incremental chunks use
  697. * httpd_resp_send_chunk() instead.
  698. *
  699. * If no status code and content-type were set, by default this
  700. * will send 200 OK status code and content type as text/html.
  701. * You may call the following functions before this API to configure
  702. * the response headers :
  703. * httpd_resp_set_status() - for setting the HTTP status string,
  704. * httpd_resp_set_type() - for setting the Content Type,
  705. * httpd_resp_set_hdr() - for appending any additional field
  706. * value entries in the response header
  707. *
  708. * @note
  709. * - This API is supposed to be called only from the context of
  710. * a URI handler where httpd_req_t* request pointer is valid.
  711. * - Once this API is called, the request has been responded to.
  712. * - No additional data can then be sent for the request.
  713. * - Once this API is called, all request headers are purged, so
  714. * request headers need be copied into separate buffers if
  715. * they are required later.
  716. *
  717. * @param[in] r The request being responded to
  718. * @param[in] buf Buffer from where the content is to be fetched
  719. * @param[in] buf_len Length of the buffer, -1 to use strlen()
  720. *
  721. * @return
  722. * - ESP_OK : On successfully sending the response packet
  723. * - ESP_ERR_INVALID_ARG : Null request pointer
  724. * - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  725. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  726. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request
  727. */
  728. esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len);
  729. /**
  730. * @brief API to send one HTTP chunk
  731. *
  732. * This API will send the data as an HTTP response to the
  733. * request. This API will use chunked-encoding and send the response
  734. * in the form of chunks. If you have the entire response contained in
  735. * a single buffer, please use httpd_resp_send() instead.
  736. *
  737. * If no status code and content-type were set, by default this will
  738. * send 200 OK status code and content type as text/html. You may
  739. * call the following functions before this API to configure the
  740. * response headers
  741. * httpd_resp_set_status() - for setting the HTTP status string,
  742. * httpd_resp_set_type() - for setting the Content Type,
  743. * httpd_resp_set_hdr() - for appending any additional field
  744. * value entries in the response header
  745. *
  746. * @note
  747. * - This API is supposed to be called only from the context of
  748. * a URI handler where httpd_req_t* request pointer is valid.
  749. * - When you are finished sending all your chunks, you must call
  750. * this function with buf_len as 0.
  751. * - Once this API is called, all request headers are purged, so
  752. * request headers need be copied into separate buffers if they
  753. * are required later.
  754. *
  755. * @param[in] r The request being responded to
  756. * @param[in] buf Pointer to a buffer that stores the data
  757. * @param[in] buf_len Length of the data from the buffer that should be sent out, -1 to use strlen()
  758. *
  759. * @return
  760. * - ESP_OK : On successfully sending the response packet chunk
  761. * - ESP_ERR_INVALID_ARG : Null request pointer
  762. * - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  763. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  764. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  765. */
  766. esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len);
  767. /* Some commonly used status codes */
  768. #define HTTPD_200 "200 OK" /*!< HTTP Response 200 */
  769. #define HTTPD_204 "204 No Content" /*!< HTTP Response 204 */
  770. #define HTTPD_207 "207 Multi-Status" /*!< HTTP Response 207 */
  771. #define HTTPD_400 "400 Bad Request" /*!< HTTP Response 400 */
  772. #define HTTPD_404 "404 Not Found" /*!< HTTP Response 404 */
  773. #define HTTPD_408 "408 Request Timeout" /*!< HTTP Response 408 */
  774. #define HTTPD_500 "500 Internal Server Error" /*!< HTTP Response 500 */
  775. /**
  776. * @brief API to set the HTTP status code
  777. *
  778. * This API sets the status of the HTTP response to the value specified.
  779. * By default, the '200 OK' response is sent as the response.
  780. *
  781. * @note
  782. * - This API is supposed to be called only from the context of
  783. * a URI handler where httpd_req_t* request pointer is valid.
  784. * - This API only sets the status to this value. The status isn't
  785. * sent out until any of the send APIs is executed.
  786. * - Make sure that the lifetime of the status string is valid till
  787. * send function is called.
  788. *
  789. * @param[in] r The request being responded to
  790. * @param[in] status The HTTP status code of this response
  791. *
  792. * @return
  793. * - ESP_OK : On success
  794. * - ESP_ERR_INVALID_ARG : Null arguments
  795. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  796. */
  797. esp_err_t httpd_resp_set_status(httpd_req_t *r, const char *status);
  798. /* Some commonly used content types */
  799. #define HTTPD_TYPE_JSON "application/json" /*!< HTTP Content type JSON */
  800. #define HTTPD_TYPE_TEXT "text/html" /*!< HTTP Content type text/HTML */
  801. #define HTTPD_TYPE_OCTET "application/octet-stream" /*!< HTTP Content type octext-stream */
  802. /**
  803. * @brief API to set the HTTP content type
  804. *
  805. * This API sets the 'Content Type' field of the response.
  806. * The default content type is 'text/html'.
  807. *
  808. * @note
  809. * - This API is supposed to be called only from the context of
  810. * a URI handler where httpd_req_t* request pointer is valid.
  811. * - This API only sets the content type to this value. The type
  812. * isn't sent out until any of the send APIs is executed.
  813. * - Make sure that the lifetime of the type string is valid till
  814. * send function is called.
  815. *
  816. * @param[in] r The request being responded to
  817. * @param[in] type The Content Type of the response
  818. *
  819. * @return
  820. * - ESP_OK : On success
  821. * - ESP_ERR_INVALID_ARG : Null arguments
  822. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  823. */
  824. esp_err_t httpd_resp_set_type(httpd_req_t *r, const char *type);
  825. /**
  826. * @brief API to append any additional headers
  827. *
  828. * This API sets any additional header fields that need to be sent in the response.
  829. *
  830. * @note
  831. * - This API is supposed to be called only from the context of
  832. * a URI handler where httpd_req_t* request pointer is valid.
  833. * - The header isn't sent out until any of the send APIs is executed.
  834. * - The maximum allowed number of additional headers is limited to
  835. * value of max_resp_headers in config structure.
  836. * - Make sure that the lifetime of the field value strings are valid till
  837. * send function is called.
  838. *
  839. * @param[in] r The request being responded to
  840. * @param[in] field The field name of the HTTP header
  841. * @param[in] value The value of this HTTP header
  842. *
  843. * @return
  844. * - ESP_OK : On successfully appending new header
  845. * - ESP_ERR_INVALID_ARG : Null arguments
  846. * - ESP_ERR_HTTPD_RESP_HDR : Total additional headers exceed max allowed
  847. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  848. */
  849. esp_err_t httpd_resp_set_hdr(httpd_req_t *r, const char *field, const char *value);
  850. /**
  851. * @brief Helper function for HTTP 404
  852. *
  853. * Send HTTP 404 message. If you wish to send additional data in the body of the
  854. * response, please use the lower-level functions directly.
  855. *
  856. * @note
  857. * - This API is supposed to be called only from the context of
  858. * a URI handler where httpd_req_t* request pointer is valid.
  859. * - Once this API is called, all request headers are purged, so
  860. * request headers need be copied into separate buffers if
  861. * they are required later.
  862. *
  863. * @param[in] r The request being responded to
  864. *
  865. * @return
  866. * - ESP_OK : On successfully sending the response packet
  867. * - ESP_ERR_INVALID_ARG : Null arguments
  868. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  869. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  870. */
  871. esp_err_t httpd_resp_send_404(httpd_req_t *r);
  872. /**
  873. * @brief Helper function for HTTP 408
  874. *
  875. * Send HTTP 408 message. If you wish to send additional data in the body of the
  876. * response, please use the lower-level functions directly.
  877. *
  878. * @note
  879. * - This API is supposed to be called only from the context of
  880. * a URI handler where httpd_req_t* request pointer is valid.
  881. * - Once this API is called, all request headers are purged, so
  882. * request headers need be copied into separate buffers if
  883. * they are required later.
  884. *
  885. * @param[in] r The request being responded to
  886. *
  887. * @return
  888. * - ESP_OK : On successfully sending the response packet
  889. * - ESP_ERR_INVALID_ARG : Null arguments
  890. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  891. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  892. */
  893. esp_err_t httpd_resp_send_408(httpd_req_t *r);
  894. /**
  895. * @brief Helper function for HTTP 500
  896. *
  897. * Send HTTP 500 message. If you wish to send additional data in the body of the
  898. * response, please use the lower-level functions directly.
  899. *
  900. * @note
  901. * - This API is supposed to be called only from the context of
  902. * a URI handler where httpd_req_t* request pointer is valid.
  903. * - Once this API is called, all request headers are purged, so
  904. * request headers need be copied into separate buffers if
  905. * they are required later.
  906. *
  907. * @param[in] r The request being responded to
  908. *
  909. * @return
  910. * - ESP_OK : On successfully sending the response packet
  911. * - ESP_ERR_INVALID_ARG : Null arguments
  912. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  913. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  914. */
  915. esp_err_t httpd_resp_send_500(httpd_req_t *r);
  916. /**
  917. * @brief Raw HTTP send
  918. *
  919. * Call this API if you wish to construct your custom response packet.
  920. * When using this, all essential header, eg. HTTP version, Status Code,
  921. * Content Type and Length, Encoding, etc. will have to be constructed
  922. * manually, and HTTP delimeters (CRLF) will need to be placed correctly
  923. * for separating sub-sections of the HTTP response packet.
  924. *
  925. * If the send override function is set, this API will end up
  926. * calling that function eventually to send data out.
  927. *
  928. * @note
  929. * - This API is supposed to be called only from the context of
  930. * a URI handler where httpd_req_t* request pointer is valid.
  931. * - Unless the response has the correct HTTP structure (which the
  932. * user must now ensure) it is not guaranteed that it will be
  933. * recognized by the client. For most cases, you wouldn't have
  934. * to call this API, but you would rather use either of :
  935. * httpd_resp_send(),
  936. * httpd_resp_send_chunk()
  937. *
  938. * @param[in] r The request being responded to
  939. * @param[in] buf Buffer from where the fully constructed packet is to be read
  940. * @param[in] buf_len Length of the buffer
  941. *
  942. * @return
  943. * - Bytes : Number of bytes that were sent successfully
  944. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  945. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  946. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()
  947. */
  948. int httpd_send(httpd_req_t *r, const char *buf, size_t buf_len);
  949. /** End of Request / Response
  950. * @}
  951. */
  952. /* ************** Group: Session ************** */
  953. /** @name Session
  954. * Functions for controlling sessions and accessing context data
  955. * @{
  956. */
  957. /**
  958. * @brief Get session context from socket descriptor
  959. *
  960. * Typically if a session context is created, it is available to URI handlers
  961. * through the httpd_req_t structure. But, there are cases where the web
  962. * server's send/receive functions may require the context (for example, for
  963. * accessing keying information etc). Since the send/receive function only have
  964. * the socket descriptor at their disposal, this API provides them with a way to
  965. * retrieve the session context.
  966. *
  967. * @param[in] handle Handle to server returned by httpd_start
  968. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  969. *
  970. * @return
  971. * - void* : Pointer to the context associated with this session
  972. * - NULL : Empty context / Invalid handle / Invalid socket fd
  973. */
  974. void *httpd_sess_get_ctx(httpd_handle_t handle, int sockfd);
  975. /**
  976. * @brief Set session context by socket descriptor
  977. *
  978. * @param[in] handle Handle to server returned by httpd_start
  979. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  980. * @param[in] ctx Context object to assign to the session
  981. * @param[in] free_fn Function that should be called to free the context
  982. */
  983. void httpd_sess_set_ctx(httpd_handle_t handle, int sockfd, void *ctx, httpd_free_ctx_fn_t free_fn);
  984. /**
  985. * @brief Get session 'transport' context by socket descriptor
  986. * @see httpd_sess_get_ctx()
  987. *
  988. * This context is used by the send/receive functions, for example to manage SSL context.
  989. *
  990. * @param[in] handle Handle to server returned by httpd_start
  991. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  992. * @return
  993. * - void* : Pointer to the transport context associated with this session
  994. * - NULL : Empty context / Invalid handle / Invalid socket fd
  995. */
  996. void *httpd_sess_get_transport_ctx(httpd_handle_t handle, int sockfd);
  997. /**
  998. * @brief Set session 'transport' context by socket descriptor
  999. * @see httpd_sess_set_ctx()
  1000. *
  1001. * @param[in] handle Handle to server returned by httpd_start
  1002. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  1003. * @param[in] ctx Transport context object to assign to the session
  1004. * @param[in] free_fn Function that should be called to free the transport context
  1005. */
  1006. void httpd_sess_set_transport_ctx(httpd_handle_t handle, int sockfd, void *ctx, httpd_free_ctx_fn_t free_fn);
  1007. /**
  1008. * @brief Get HTTPD global user context (it was set in the server config struct)
  1009. *
  1010. * @param[in] handle Handle to server returned by httpd_start
  1011. * @return global user context
  1012. */
  1013. void *httpd_get_global_user_ctx(httpd_handle_t handle);
  1014. /**
  1015. * @brief Get HTTPD global transport context (it was set in the server config struct)
  1016. *
  1017. * @param[in] handle Handle to server returned by httpd_start
  1018. * @return global transport context
  1019. */
  1020. void *httpd_get_global_transport_ctx(httpd_handle_t handle);
  1021. /**
  1022. * @brief Trigger an httpd session close externally
  1023. *
  1024. * @note Calling this API is only required in special circumstances wherein
  1025. * some application requires to close an httpd client session asynchronously.
  1026. *
  1027. * @param[in] handle Handle to server returned by httpd_start
  1028. * @param[in] sockfd The socket descriptor of the session to be closed
  1029. *
  1030. * @return
  1031. * - ESP_OK : On successfully initiating closure
  1032. * - ESP_FAIL : Failure to queue work
  1033. * - ESP_ERR_NOT_FOUND : Socket fd not found
  1034. * - ESP_ERR_INVALID_ARG : Null arguments
  1035. */
  1036. esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
  1037. /**
  1038. * @brief Update timestamp for a given socket
  1039. *
  1040. * Timestamps are internally associated with each session to monitor
  1041. * how recently a session exchanged traffic. When LRU purge is enabled,
  1042. * if a client is requesting for connection but maximum number of
  1043. * sockets/sessions is reached, then the session having the earliest
  1044. * timestamp is closed automatically.
  1045. *
  1046. * Updating the timestamp manually prevents the socket from being purged
  1047. * due to the Least Recently Used (LRU) logic, even though it might not
  1048. * have received traffic for some time. This is useful when all open
  1049. * sockets/session are frequently exchanging traffic but the user specifically
  1050. * wants one of the sessions to be kept open, irrespective of when it last
  1051. * exchanged a packet.
  1052. *
  1053. * @note Calling this API is only necessary if the LRU Purge Enable option
  1054. * is enabled.
  1055. *
  1056. * @param[in] handle Handle to server returned by httpd_start
  1057. * @param[in] sockfd The socket descriptor of the session for which timestamp
  1058. * is to be updated
  1059. *
  1060. * @return
  1061. * - ESP_OK : Socket found and timestamp updated
  1062. * - ESP_ERR_NOT_FOUND : Socket not found
  1063. * - ESP_ERR_INVALID_ARG : Null arguments
  1064. */
  1065. esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd);
  1066. /** End of Session
  1067. * @}
  1068. */
  1069. /* ************** Group: Work Queue ************** */
  1070. /** @name Work Queue
  1071. * APIs related to the HTTPD Work Queue
  1072. * @{
  1073. */
  1074. /**
  1075. * @brief Prototype of the HTTPD work function
  1076. * Please refer to httpd_queue_work() for more details.
  1077. * @param[in] arg The arguments for this work function
  1078. */
  1079. typedef void (*httpd_work_fn_t)(void *arg);
  1080. /**
  1081. * @brief Queue execution of a function in HTTPD's context
  1082. *
  1083. * This API queues a work function for asynchronous execution
  1084. *
  1085. * @note Some protocols require that the web server generate some asynchronous data
  1086. * and send it to the persistently opened connection. This facility is for use
  1087. * by such protocols.
  1088. *
  1089. * @param[in] handle Handle to server returned by httpd_start
  1090. * @param[in] work Pointer to the function to be executed in the HTTPD's context
  1091. * @param[in] arg Pointer to the arguments that should be passed to this function
  1092. *
  1093. * @return
  1094. * - ESP_OK : On successfully queueing the work
  1095. * - ESP_FAIL : Failure in ctrl socket
  1096. * - ESP_ERR_INVALID_ARG : Null arguments
  1097. */
  1098. esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *arg);
  1099. /** End of Group Work Queue
  1100. * @}
  1101. */
  1102. #ifdef __cplusplus
  1103. }
  1104. #endif
  1105. #endif /* ! _ESP_HTTP_SERVER_H_ */