esp_http_server.h 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_HTTP_SERVER_H_
  7. #define _ESP_HTTP_SERVER_H_
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <freertos/FreeRTOS.h>
  11. #include <freertos/task.h>
  12. #include <http_parser.h>
  13. #include <sdkconfig.h>
  14. #include <esp_err.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /*
  19. note: esp_https_server.h includes a customized copy of this
  20. initializer that should be kept in sync
  21. */
  22. #define HTTPD_DEFAULT_CONFIG() { \
  23. .task_priority = tskIDLE_PRIORITY+5, \
  24. .stack_size = 4096, \
  25. .core_id = tskNO_AFFINITY, \
  26. .server_port = 80, \
  27. .ctrl_port = 32768, \
  28. .max_open_sockets = 7, \
  29. .max_uri_handlers = 8, \
  30. .max_resp_headers = 8, \
  31. .backlog_conn = 5, \
  32. .lru_purge_enable = false, \
  33. .recv_wait_timeout = 5, \
  34. .send_wait_timeout = 5, \
  35. .global_user_ctx = NULL, \
  36. .global_user_ctx_free_fn = NULL, \
  37. .global_transport_ctx = NULL, \
  38. .global_transport_ctx_free_fn = NULL, \
  39. .open_fn = NULL, \
  40. .close_fn = NULL, \
  41. .uri_match_fn = NULL \
  42. }
  43. #define ESP_ERR_HTTPD_BASE (0xb000) /*!< Starting number of HTTPD error codes */
  44. #define ESP_ERR_HTTPD_HANDLERS_FULL (ESP_ERR_HTTPD_BASE + 1) /*!< All slots for registering URI handlers have been consumed */
  45. #define ESP_ERR_HTTPD_HANDLER_EXISTS (ESP_ERR_HTTPD_BASE + 2) /*!< URI handler with same method and target URI already registered */
  46. #define ESP_ERR_HTTPD_INVALID_REQ (ESP_ERR_HTTPD_BASE + 3) /*!< Invalid request pointer */
  47. #define ESP_ERR_HTTPD_RESULT_TRUNC (ESP_ERR_HTTPD_BASE + 4) /*!< Result string truncated */
  48. #define ESP_ERR_HTTPD_RESP_HDR (ESP_ERR_HTTPD_BASE + 5) /*!< Response header field larger than supported */
  49. #define ESP_ERR_HTTPD_RESP_SEND (ESP_ERR_HTTPD_BASE + 6) /*!< Error occured while sending response packet */
  50. #define ESP_ERR_HTTPD_ALLOC_MEM (ESP_ERR_HTTPD_BASE + 7) /*!< Failed to dynamically allocate memory for resource */
  51. #define ESP_ERR_HTTPD_TASK (ESP_ERR_HTTPD_BASE + 8) /*!< Failed to launch server task/thread */
  52. /* Symbol to be used as length parameter in httpd_resp_send APIs
  53. * for setting buffer length to string length */
  54. #define HTTPD_RESP_USE_STRLEN -1
  55. /* ************** Group: Initialization ************** */
  56. /** @name Initialization
  57. * APIs related to the Initialization of the web server
  58. * @{
  59. */
  60. /**
  61. * @brief HTTP Server Instance Handle
  62. *
  63. * Every instance of the server will have a unique handle.
  64. */
  65. typedef void* httpd_handle_t;
  66. /**
  67. * @brief HTTP Method Type wrapper over "enum http_method"
  68. * available in "http_parser" library
  69. */
  70. typedef enum http_method httpd_method_t;
  71. /**
  72. * @brief Prototype for freeing context data (if any)
  73. * @param[in] ctx object to free
  74. */
  75. typedef void (*httpd_free_ctx_fn_t)(void *ctx);
  76. /**
  77. * @brief Function prototype for opening a session.
  78. *
  79. * Called immediately after the socket was opened to set up the send/recv functions and
  80. * other parameters of the socket.
  81. *
  82. * @param[in] hd server instance
  83. * @param[in] sockfd session socket file descriptor
  84. * @return
  85. * - ESP_OK : On success
  86. * - Any value other than ESP_OK will signal the server to close the socket immediately
  87. */
  88. typedef esp_err_t (*httpd_open_func_t)(httpd_handle_t hd, int sockfd);
  89. /**
  90. * @brief Function prototype for closing a session.
  91. *
  92. * @note It's possible that the socket descriptor is invalid at this point, the function
  93. * is called for all terminated sessions. Ensure proper handling of return codes.
  94. *
  95. * @param[in] hd server instance
  96. * @param[in] sockfd session socket file descriptor
  97. */
  98. typedef void (*httpd_close_func_t)(httpd_handle_t hd, int sockfd);
  99. /**
  100. * @brief Function prototype for URI matching.
  101. *
  102. * @param[in] reference_uri URI/template with respect to which the other URI is matched
  103. * @param[in] uri_to_match URI/template being matched to the reference URI/template
  104. * @param[in] match_upto For specifying the actual length of `uri_to_match` up to
  105. * which the matching algorithm is to be applied (The maximum
  106. * value is `strlen(uri_to_match)`, independent of the length
  107. * of `reference_uri`)
  108. * @return true on match
  109. */
  110. typedef bool (*httpd_uri_match_func_t)(const char *reference_uri,
  111. const char *uri_to_match,
  112. size_t match_upto);
  113. /**
  114. * @brief HTTP Server Configuration Structure
  115. *
  116. * @note Use HTTPD_DEFAULT_CONFIG() to initialize the configuration
  117. * to a default value and then modify only those fields that are
  118. * specifically determined by the use case.
  119. */
  120. typedef struct httpd_config {
  121. unsigned task_priority; /*!< Priority of FreeRTOS task which runs the server */
  122. size_t stack_size; /*!< The maximum stack size allowed for the server task */
  123. BaseType_t core_id; /*!< The core the HTTP server task will run on */
  124. /**
  125. * TCP Port number for receiving and transmitting HTTP traffic
  126. */
  127. uint16_t server_port;
  128. /**
  129. * UDP Port number for asynchronously exchanging control signals
  130. * between various components of the server
  131. */
  132. uint16_t ctrl_port;
  133. uint16_t max_open_sockets; /*!< Max number of sockets/clients connected at any time*/
  134. uint16_t max_uri_handlers; /*!< Maximum allowed uri handlers */
  135. uint16_t max_resp_headers; /*!< Maximum allowed additional headers in HTTP response */
  136. uint16_t backlog_conn; /*!< Number of backlog connections */
  137. bool lru_purge_enable; /*!< Purge "Least Recently Used" connection */
  138. uint16_t recv_wait_timeout; /*!< Timeout for recv function (in seconds)*/
  139. uint16_t send_wait_timeout; /*!< Timeout for send function (in seconds)*/
  140. /**
  141. * Global user context.
  142. *
  143. * This field can be used to store arbitrary user data within the server context.
  144. * The value can be retrieved using the server handle, available e.g. in the httpd_req_t struct.
  145. *
  146. * When shutting down, the server frees up the user context by
  147. * calling free() on the global_user_ctx field. If you wish to use a custom
  148. * function for freeing the global user context, please specify that here.
  149. */
  150. void * global_user_ctx;
  151. /**
  152. * Free function for global user context
  153. */
  154. httpd_free_ctx_fn_t global_user_ctx_free_fn;
  155. /**
  156. * Global transport context.
  157. *
  158. * Similar to global_user_ctx, but used for session encoding or encryption (e.g. to hold the SSL context).
  159. * It will be freed using free(), unless global_transport_ctx_free_fn is specified.
  160. */
  161. void * global_transport_ctx;
  162. /**
  163. * Free function for global transport context
  164. */
  165. httpd_free_ctx_fn_t global_transport_ctx_free_fn;
  166. /**
  167. * Custom session opening callback.
  168. *
  169. * Called on a new session socket just after accept(), but before reading any data.
  170. *
  171. * This is an opportunity to set up e.g. SSL encryption using global_transport_ctx
  172. * and the send/recv/pending session overrides.
  173. *
  174. * If a context needs to be maintained between these functions, store it in the session using
  175. * httpd_sess_set_transport_ctx() and retrieve it later with httpd_sess_get_transport_ctx()
  176. *
  177. * Returning a value other than ESP_OK will immediately close the new socket.
  178. */
  179. httpd_open_func_t open_fn;
  180. /**
  181. * Custom session closing callback.
  182. *
  183. * Called when a session is deleted, before freeing user and transport contexts and before
  184. * closing the socket. This is a place for custom de-init code common to all sockets.
  185. *
  186. * Set the user or transport context to NULL if it was freed here, so the server does not
  187. * try to free it again.
  188. *
  189. * This function is run for all terminated sessions, including sessions where the socket
  190. * was closed by the network stack - that is, the file descriptor may not be valid anymore.
  191. */
  192. httpd_close_func_t close_fn;
  193. /**
  194. * URI matcher function.
  195. *
  196. * Called when searching for a matching URI:
  197. * 1) whose request handler is to be executed right
  198. * after an HTTP request is successfully parsed
  199. * 2) in order to prevent duplication while registering
  200. * a new URI handler using `httpd_register_uri_handler()`
  201. *
  202. * Available options are:
  203. * 1) NULL : Internally do basic matching using `strncmp()`
  204. * 2) `httpd_uri_match_wildcard()` : URI wildcard matcher
  205. *
  206. * Users can implement their own matching functions (See description
  207. * of the `httpd_uri_match_func_t` function prototype)
  208. */
  209. httpd_uri_match_func_t uri_match_fn;
  210. } httpd_config_t;
  211. /**
  212. * @brief Starts the web server
  213. *
  214. * Create an instance of HTTP server and allocate memory/resources for it
  215. * depending upon the specified configuration.
  216. *
  217. * Example usage:
  218. * @code{c}
  219. *
  220. * //Function for starting the webserver
  221. * httpd_handle_t start_webserver(void)
  222. * {
  223. * // Generate default configuration
  224. * httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  225. *
  226. * // Empty handle to http_server
  227. * httpd_handle_t server = NULL;
  228. *
  229. * // Start the httpd server
  230. * if (httpd_start(&server, &config) == ESP_OK) {
  231. * // Register URI handlers
  232. * httpd_register_uri_handler(server, &uri_get);
  233. * httpd_register_uri_handler(server, &uri_post);
  234. * }
  235. * // If server failed to start, handle will be NULL
  236. * return server;
  237. * }
  238. *
  239. * @endcode
  240. *
  241. * @param[in] config Configuration for new instance of the server
  242. * @param[out] handle Handle to newly created instance of the server. NULL on error
  243. * @return
  244. * - ESP_OK : Instance created successfully
  245. * - ESP_ERR_INVALID_ARG : Null argument(s)
  246. * - ESP_ERR_HTTPD_ALLOC_MEM : Failed to allocate memory for instance
  247. * - ESP_ERR_HTTPD_TASK : Failed to launch server task
  248. */
  249. esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config);
  250. /**
  251. * @brief Stops the web server
  252. *
  253. * Deallocates memory/resources used by an HTTP server instance and
  254. * deletes it. Once deleted the handle can no longer be used for accessing
  255. * the instance.
  256. *
  257. * Example usage:
  258. * @code{c}
  259. *
  260. * // Function for stopping the webserver
  261. * void stop_webserver(httpd_handle_t server)
  262. * {
  263. * // Ensure handle is non NULL
  264. * if (server != NULL) {
  265. * // Stop the httpd server
  266. * httpd_stop(server);
  267. * }
  268. * }
  269. *
  270. * @endcode
  271. *
  272. * @param[in] handle Handle to server returned by httpd_start
  273. * @return
  274. * - ESP_OK : Server stopped successfully
  275. * - ESP_ERR_INVALID_ARG : Handle argument is Null
  276. */
  277. esp_err_t httpd_stop(httpd_handle_t handle);
  278. /** End of Group Initialization
  279. * @}
  280. */
  281. /* ************** Group: URI Handlers ************** */
  282. /** @name URI Handlers
  283. * APIs related to the URI handlers
  284. * @{
  285. */
  286. /* Max supported HTTP request header length */
  287. #define HTTPD_MAX_REQ_HDR_LEN CONFIG_HTTPD_MAX_REQ_HDR_LEN
  288. /* Max supported HTTP request URI length */
  289. #define HTTPD_MAX_URI_LEN CONFIG_HTTPD_MAX_URI_LEN
  290. /**
  291. * @brief HTTP Request Data Structure
  292. */
  293. typedef struct httpd_req {
  294. httpd_handle_t handle; /*!< Handle to server instance */
  295. int method; /*!< The type of HTTP request, -1 if unsupported method */
  296. const char uri[HTTPD_MAX_URI_LEN + 1]; /*!< The URI of this request (1 byte extra for null termination) */
  297. size_t content_len; /*!< Length of the request body */
  298. void *aux; /*!< Internally used members */
  299. /**
  300. * User context pointer passed during URI registration.
  301. */
  302. void *user_ctx;
  303. /**
  304. * Session Context Pointer
  305. *
  306. * A session context. Contexts are maintained across 'sessions' for a
  307. * given open TCP connection. One session could have multiple request
  308. * responses. The web server will ensure that the context persists
  309. * across all these request and responses.
  310. *
  311. * By default, this is NULL. URI Handlers can set this to any meaningful
  312. * value.
  313. *
  314. * If the underlying socket gets closed, and this pointer is non-NULL,
  315. * the web server will free up the context by calling free(), unless
  316. * free_ctx function is set.
  317. */
  318. void *sess_ctx;
  319. /**
  320. * Pointer to free context hook
  321. *
  322. * Function to free session context
  323. *
  324. * If the web server's socket closes, it frees up the session context by
  325. * calling free() on the sess_ctx member. If you wish to use a custom
  326. * function for freeing the session context, please specify that here.
  327. */
  328. httpd_free_ctx_fn_t free_ctx;
  329. /**
  330. * Flag indicating if Session Context changes should be ignored
  331. *
  332. * By default, if you change the sess_ctx in some URI handler, the http server
  333. * will internally free the earlier context (if non NULL), after the URI handler
  334. * returns. If you want to manage the allocation/reallocation/freeing of
  335. * sess_ctx yourself, set this flag to true, so that the server will not
  336. * perform any checks on it. The context will be cleared by the server
  337. * (by calling free_ctx or free()) only if the socket gets closed.
  338. */
  339. bool ignore_sess_ctx_changes;
  340. } httpd_req_t;
  341. /**
  342. * @brief Structure for URI handler
  343. */
  344. typedef struct httpd_uri {
  345. const char *uri; /*!< The URI to handle */
  346. httpd_method_t method; /*!< Method supported by the URI */
  347. /**
  348. * Handler to call for supported request method. This must
  349. * return ESP_OK, or else the underlying socket will be closed.
  350. */
  351. esp_err_t (*handler)(httpd_req_t *r);
  352. /**
  353. * Pointer to user context data which will be available to handler
  354. */
  355. void *user_ctx;
  356. #ifdef CONFIG_HTTPD_WS_SUPPORT
  357. /**
  358. * Flag for indicating a WebSocket endpoint.
  359. * If this flag is true, then method must be HTTP_GET. Otherwise the handshake will not be handled.
  360. */
  361. bool is_websocket;
  362. /**
  363. * Flag indicating that control frames (PING, PONG, CLOSE) are also passed to the handler
  364. * This is used if a custom processing of the control frames is needed
  365. */
  366. bool handle_ws_control_frames;
  367. /**
  368. * Pointer to subprotocol supported by URI
  369. */
  370. const char *supported_subprotocol;
  371. #endif
  372. } httpd_uri_t;
  373. /**
  374. * @brief Registers a URI handler
  375. *
  376. * @note URI handlers can be registered in real time as long as the
  377. * server handle is valid.
  378. *
  379. * Example usage:
  380. * @code{c}
  381. *
  382. * esp_err_t my_uri_handler(httpd_req_t* req)
  383. * {
  384. * // Recv , Process and Send
  385. * ....
  386. * ....
  387. * ....
  388. *
  389. * // Fail condition
  390. * if (....) {
  391. * // Return fail to close session //
  392. * return ESP_FAIL;
  393. * }
  394. *
  395. * // On success
  396. * return ESP_OK;
  397. * }
  398. *
  399. * // URI handler structure
  400. * httpd_uri_t my_uri {
  401. * .uri = "/my_uri/path/xyz",
  402. * .method = HTTPD_GET,
  403. * .handler = my_uri_handler,
  404. * .user_ctx = NULL
  405. * };
  406. *
  407. * // Register handler
  408. * if (httpd_register_uri_handler(server_handle, &my_uri) != ESP_OK) {
  409. * // If failed to register handler
  410. * ....
  411. * }
  412. *
  413. * @endcode
  414. *
  415. * @param[in] handle handle to HTTPD server instance
  416. * @param[in] uri_handler pointer to handler that needs to be registered
  417. *
  418. * @return
  419. * - ESP_OK : On successfully registering the handler
  420. * - ESP_ERR_INVALID_ARG : Null arguments
  421. * - ESP_ERR_HTTPD_HANDLERS_FULL : If no slots left for new handler
  422. * - ESP_ERR_HTTPD_HANDLER_EXISTS : If handler with same URI and
  423. * method is already registered
  424. */
  425. esp_err_t httpd_register_uri_handler(httpd_handle_t handle,
  426. const httpd_uri_t *uri_handler);
  427. /**
  428. * @brief Unregister a URI handler
  429. *
  430. * @param[in] handle handle to HTTPD server instance
  431. * @param[in] uri URI string
  432. * @param[in] method HTTP method
  433. *
  434. * @return
  435. * - ESP_OK : On successfully deregistering the handler
  436. * - ESP_ERR_INVALID_ARG : Null arguments
  437. * - ESP_ERR_NOT_FOUND : Handler with specified URI and method not found
  438. */
  439. esp_err_t httpd_unregister_uri_handler(httpd_handle_t handle,
  440. const char *uri, httpd_method_t method);
  441. /**
  442. * @brief Unregister all URI handlers with the specified uri string
  443. *
  444. * @param[in] handle handle to HTTPD server instance
  445. * @param[in] uri uri string specifying all handlers that need
  446. * to be deregisterd
  447. *
  448. * @return
  449. * - ESP_OK : On successfully deregistering all such handlers
  450. * - ESP_ERR_INVALID_ARG : Null arguments
  451. * - ESP_ERR_NOT_FOUND : No handler registered with specified uri string
  452. */
  453. esp_err_t httpd_unregister_uri(httpd_handle_t handle, const char* uri);
  454. /** End of URI Handlers
  455. * @}
  456. */
  457. /* ************** Group: HTTP Error ************** */
  458. /** @name HTTP Error
  459. * Prototype for HTTP errors and error handling functions
  460. * @{
  461. */
  462. /**
  463. * @brief Error codes sent as HTTP response in case of errors
  464. * encountered during processing of an HTTP request
  465. */
  466. typedef enum {
  467. /* For any unexpected errors during parsing, like unexpected
  468. * state transitions, or unhandled errors.
  469. */
  470. HTTPD_500_INTERNAL_SERVER_ERROR = 0,
  471. /* For methods not supported by http_parser. Presently
  472. * http_parser halts parsing when such methods are
  473. * encountered and so the server responds with 400 Bad
  474. * Request error instead.
  475. */
  476. HTTPD_501_METHOD_NOT_IMPLEMENTED,
  477. /* When HTTP version is not 1.1 */
  478. HTTPD_505_VERSION_NOT_SUPPORTED,
  479. /* Returned when http_parser halts parsing due to incorrect
  480. * syntax of request, unsupported method in request URI or
  481. * due to chunked encoding / upgrade field present in headers
  482. */
  483. HTTPD_400_BAD_REQUEST,
  484. /* This response means the client must authenticate itself
  485. * to get the requested response.
  486. */
  487. HTTPD_401_UNAUTHORIZED,
  488. /* The client does not have access rights to the content,
  489. * so the server is refusing to give the requested resource.
  490. * Unlike 401, the client's identity is known to the server.
  491. */
  492. HTTPD_403_FORBIDDEN,
  493. /* When requested URI is not found */
  494. HTTPD_404_NOT_FOUND,
  495. /* When URI found, but method has no handler registered */
  496. HTTPD_405_METHOD_NOT_ALLOWED,
  497. /* Intended for recv timeout. Presently it's being sent
  498. * for other recv errors as well. Client should expect the
  499. * server to immediately close the connection after
  500. * responding with this.
  501. */
  502. HTTPD_408_REQ_TIMEOUT,
  503. /* Intended for responding to chunked encoding, which is
  504. * not supported currently. Though unhandled http_parser
  505. * callback for chunked request returns "400 Bad Request"
  506. */
  507. HTTPD_411_LENGTH_REQUIRED,
  508. /* URI length greater than CONFIG_HTTPD_MAX_URI_LEN */
  509. HTTPD_414_URI_TOO_LONG,
  510. /* Headers section larger than CONFIG_HTTPD_MAX_REQ_HDR_LEN */
  511. HTTPD_431_REQ_HDR_FIELDS_TOO_LARGE,
  512. /* Used internally for retrieving the total count of errors */
  513. HTTPD_ERR_CODE_MAX
  514. } httpd_err_code_t;
  515. /**
  516. * @brief Function prototype for HTTP error handling.
  517. *
  518. * This function is executed upon HTTP errors generated during
  519. * internal processing of an HTTP request. This is used to override
  520. * the default behavior on error, which is to send HTTP error response
  521. * and close the underlying socket.
  522. *
  523. * @note
  524. * - If implemented, the server will not automatically send out HTTP
  525. * error response codes, therefore, httpd_resp_send_err() must be
  526. * invoked inside this function if user wishes to generate HTTP
  527. * error responses.
  528. * - When invoked, the validity of `uri`, `method`, `content_len`
  529. * and `user_ctx` fields of the httpd_req_t parameter is not
  530. * guaranteed as the HTTP request may be partially received/parsed.
  531. * - The function must return ESP_OK if underlying socket needs to
  532. * be kept open. Any other value will ensure that the socket is
  533. * closed. The return value is ignored when error is of type
  534. * `HTTPD_500_INTERNAL_SERVER_ERROR` and the socket closed anyway.
  535. *
  536. * @param[in] req HTTP request for which the error needs to be handled
  537. * @param[in] error Error type
  538. *
  539. * @return
  540. * - ESP_OK : error handled successful
  541. * - ESP_FAIL : failure indicates that the underlying socket needs to be closed
  542. */
  543. typedef esp_err_t (*httpd_err_handler_func_t)(httpd_req_t *req,
  544. httpd_err_code_t error);
  545. /**
  546. * @brief Function for registering HTTP error handlers
  547. *
  548. * This function maps a handler function to any supported error code
  549. * given by `httpd_err_code_t`. See prototype `httpd_err_handler_func_t`
  550. * above for details.
  551. *
  552. * @param[in] handle HTTP server handle
  553. * @param[in] error Error type
  554. * @param[in] handler_fn User implemented handler function
  555. * (Pass NULL to unset any previously set handler)
  556. *
  557. * @return
  558. * - ESP_OK : handler registered successfully
  559. * - ESP_ERR_INVALID_ARG : invalid error code or server handle
  560. */
  561. esp_err_t httpd_register_err_handler(httpd_handle_t handle,
  562. httpd_err_code_t error,
  563. httpd_err_handler_func_t handler_fn);
  564. /** End of HTTP Error
  565. * @}
  566. */
  567. /* ************** Group: TX/RX ************** */
  568. /** @name TX / RX
  569. * Prototype for HTTPDs low-level send/recv functions
  570. * @{
  571. */
  572. #define HTTPD_SOCK_ERR_FAIL -1
  573. #define HTTPD_SOCK_ERR_INVALID -2
  574. #define HTTPD_SOCK_ERR_TIMEOUT -3
  575. /**
  576. * @brief Prototype for HTTPDs low-level send function
  577. *
  578. * @note User specified send function must handle errors internally,
  579. * depending upon the set value of errno, and return specific
  580. * HTTPD_SOCK_ERR_ codes, which will eventually be conveyed as
  581. * return value of httpd_send() function
  582. *
  583. * @param[in] hd server instance
  584. * @param[in] sockfd session socket file descriptor
  585. * @param[in] buf buffer with bytes to send
  586. * @param[in] buf_len data size
  587. * @param[in] flags flags for the send() function
  588. * @return
  589. * - Bytes : The number of bytes sent successfully
  590. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  591. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  592. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()
  593. */
  594. typedef int (*httpd_send_func_t)(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags);
  595. /**
  596. * @brief Prototype for HTTPDs low-level recv function
  597. *
  598. * @note User specified recv function must handle errors internally,
  599. * depending upon the set value of errno, and return specific
  600. * HTTPD_SOCK_ERR_ codes, which will eventually be conveyed as
  601. * return value of httpd_req_recv() function
  602. *
  603. * @param[in] hd server instance
  604. * @param[in] sockfd session socket file descriptor
  605. * @param[in] buf buffer with bytes to send
  606. * @param[in] buf_len data size
  607. * @param[in] flags flags for the send() function
  608. * @return
  609. * - Bytes : The number of bytes received successfully
  610. * - 0 : Buffer length parameter is zero / connection closed by peer
  611. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  612. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  613. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()
  614. */
  615. typedef int (*httpd_recv_func_t)(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags);
  616. /**
  617. * @brief Prototype for HTTPDs low-level "get pending bytes" function
  618. *
  619. * @note User specified pending function must handle errors internally,
  620. * depending upon the set value of errno, and return specific
  621. * HTTPD_SOCK_ERR_ codes, which will be handled accordingly in
  622. * the server task.
  623. *
  624. * @param[in] hd server instance
  625. * @param[in] sockfd session socket file descriptor
  626. * @return
  627. * - Bytes : The number of bytes waiting to be received
  628. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  629. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket pending()
  630. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket pending()
  631. */
  632. typedef int (*httpd_pending_func_t)(httpd_handle_t hd, int sockfd);
  633. /** End of TX / RX
  634. * @}
  635. */
  636. /* ************** Group: Request/Response ************** */
  637. /** @name Request / Response
  638. * APIs related to the data send/receive by URI handlers.
  639. * These APIs are supposed to be called only from the context of
  640. * a URI handler where httpd_req_t* request pointer is valid.
  641. * @{
  642. */
  643. /**
  644. * @brief Override web server's receive function (by session FD)
  645. *
  646. * This function overrides the web server's receive function. This same function is
  647. * used to read HTTP request packets.
  648. *
  649. * @note This API is supposed to be called either from the context of
  650. * - an http session APIs where sockfd is a valid parameter
  651. * - a URI handler where sockfd is obtained using httpd_req_to_sockfd()
  652. *
  653. * @param[in] hd HTTPD instance handle
  654. * @param[in] sockfd Session socket FD
  655. * @param[in] recv_func The receive function to be set for this session
  656. *
  657. * @return
  658. * - ESP_OK : On successfully registering override
  659. * - ESP_ERR_INVALID_ARG : Null arguments
  660. */
  661. esp_err_t httpd_sess_set_recv_override(httpd_handle_t hd, int sockfd, httpd_recv_func_t recv_func);
  662. /**
  663. * @brief Override web server's send function (by session FD)
  664. *
  665. * This function overrides the web server's send function. This same function is
  666. * used to send out any response to any HTTP request.
  667. *
  668. * @note This API is supposed to be called either from the context of
  669. * - an http session APIs where sockfd is a valid parameter
  670. * - a URI handler where sockfd is obtained using httpd_req_to_sockfd()
  671. *
  672. * @param[in] hd HTTPD instance handle
  673. * @param[in] sockfd Session socket FD
  674. * @param[in] send_func The send function to be set for this session
  675. *
  676. * @return
  677. * - ESP_OK : On successfully registering override
  678. * - ESP_ERR_INVALID_ARG : Null arguments
  679. */
  680. esp_err_t httpd_sess_set_send_override(httpd_handle_t hd, int sockfd, httpd_send_func_t send_func);
  681. /**
  682. * @brief Override web server's pending function (by session FD)
  683. *
  684. * This function overrides the web server's pending function. This function is
  685. * used to test for pending bytes in a socket.
  686. *
  687. * @note This API is supposed to be called either from the context of
  688. * - an http session APIs where sockfd is a valid parameter
  689. * - a URI handler where sockfd is obtained using httpd_req_to_sockfd()
  690. *
  691. * @param[in] hd HTTPD instance handle
  692. * @param[in] sockfd Session socket FD
  693. * @param[in] pending_func The receive function to be set for this session
  694. *
  695. * @return
  696. * - ESP_OK : On successfully registering override
  697. * - ESP_ERR_INVALID_ARG : Null arguments
  698. */
  699. esp_err_t httpd_sess_set_pending_override(httpd_handle_t hd, int sockfd, httpd_pending_func_t pending_func);
  700. /**
  701. * @brief Get the Socket Descriptor from the HTTP request
  702. *
  703. * This API will return the socket descriptor of the session for
  704. * which URI handler was executed on reception of HTTP request.
  705. * This is useful when user wants to call functions that require
  706. * session socket fd, from within a URI handler, ie. :
  707. * httpd_sess_get_ctx(),
  708. * httpd_sess_trigger_close(),
  709. * httpd_sess_update_lru_counter().
  710. *
  711. * @note This API is supposed to be called only from the context of
  712. * a URI handler where httpd_req_t* request pointer is valid.
  713. *
  714. * @param[in] r The request whose socket descriptor should be found
  715. *
  716. * @return
  717. * - Socket descriptor : The socket descriptor for this request
  718. * - -1 : Invalid/NULL request pointer
  719. */
  720. int httpd_req_to_sockfd(httpd_req_t *r);
  721. /**
  722. * @brief API to read content data from the HTTP request
  723. *
  724. * This API will read HTTP content data from the HTTP request into
  725. * provided buffer. Use content_len provided in httpd_req_t structure
  726. * to know the length of data to be fetched. If content_len is too
  727. * large for the buffer then user may have to make multiple calls to
  728. * this function, each time fetching 'buf_len' number of bytes,
  729. * while the pointer to content data is incremented internally by
  730. * the same number.
  731. *
  732. * @note
  733. * - This API is supposed to be called only from the context of
  734. * a URI handler where httpd_req_t* request pointer is valid.
  735. * - If an error is returned, the URI handler must further return an error.
  736. * This will ensure that the erroneous socket is closed and cleaned up by
  737. * the web server.
  738. * - Presently Chunked Encoding is not supported
  739. *
  740. * @param[in] r The request being responded to
  741. * @param[in] buf Pointer to a buffer that the data will be read into
  742. * @param[in] buf_len Length of the buffer
  743. *
  744. * @return
  745. * - Bytes : Number of bytes read into the buffer successfully
  746. * - 0 : Buffer length parameter is zero / connection closed by peer
  747. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  748. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  749. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()
  750. */
  751. int httpd_req_recv(httpd_req_t *r, char *buf, size_t buf_len);
  752. /**
  753. * @brief Search for a field in request headers and
  754. * return the string length of it's value
  755. *
  756. * @note
  757. * - This API is supposed to be called only from the context of
  758. * a URI handler where httpd_req_t* request pointer is valid.
  759. * - Once httpd_resp_send() API is called all request headers
  760. * are purged, so request headers need be copied into separate
  761. * buffers if they are required later.
  762. *
  763. * @param[in] r The request being responded to
  764. * @param[in] field The header field to be searched in the request
  765. *
  766. * @return
  767. * - Length : If field is found in the request URL
  768. * - Zero : Field not found / Invalid request / Null arguments
  769. */
  770. size_t httpd_req_get_hdr_value_len(httpd_req_t *r, const char *field);
  771. /**
  772. * @brief Get the value string of a field from the request headers
  773. *
  774. * @note
  775. * - This API is supposed to be called only from the context of
  776. * a URI handler where httpd_req_t* request pointer is valid.
  777. * - Once httpd_resp_send() API is called all request headers
  778. * are purged, so request headers need be copied into separate
  779. * buffers if they are required later.
  780. * - If output size is greater than input, then the value is truncated,
  781. * accompanied by truncation error as return value.
  782. * - Use httpd_req_get_hdr_value_len() to know the right buffer length
  783. *
  784. * @param[in] r The request being responded to
  785. * @param[in] field The field to be searched in the header
  786. * @param[out] val Pointer to the buffer into which the value will be copied if the field is found
  787. * @param[in] val_size Size of the user buffer "val"
  788. *
  789. * @return
  790. * - ESP_OK : Field found in the request header and value string copied
  791. * - ESP_ERR_NOT_FOUND : Key not found
  792. * - ESP_ERR_INVALID_ARG : Null arguments
  793. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid HTTP request pointer
  794. * - ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
  795. */
  796. esp_err_t httpd_req_get_hdr_value_str(httpd_req_t *r, const char *field, char *val, size_t val_size);
  797. /**
  798. * @brief Get Query string length from the request URL
  799. *
  800. * @note This API is supposed to be called only from the context of
  801. * a URI handler where httpd_req_t* request pointer is valid
  802. *
  803. * @param[in] r The request being responded to
  804. *
  805. * @return
  806. * - Length : Query is found in the request URL
  807. * - Zero : Query not found / Null arguments / Invalid request
  808. */
  809. size_t httpd_req_get_url_query_len(httpd_req_t *r);
  810. /**
  811. * @brief Get Query string from the request URL
  812. *
  813. * @note
  814. * - Presently, the user can fetch the full URL query string, but decoding
  815. * will have to be performed by the user. Request headers can be read using
  816. * httpd_req_get_hdr_value_str() to know the 'Content-Type' (eg. Content-Type:
  817. * application/x-www-form-urlencoded) and then the appropriate decoding
  818. * algorithm needs to be applied.
  819. * - This API is supposed to be called only from the context of
  820. * a URI handler where httpd_req_t* request pointer is valid
  821. * - If output size is greater than input, then the value is truncated,
  822. * accompanied by truncation error as return value
  823. * - Prior to calling this function, one can use httpd_req_get_url_query_len()
  824. * to know the query string length beforehand and hence allocate the buffer
  825. * of right size (usually query string length + 1 for null termination)
  826. * for storing the query string
  827. *
  828. * @param[in] r The request being responded to
  829. * @param[out] buf Pointer to the buffer into which the query string will be copied (if found)
  830. * @param[in] buf_len Length of output buffer
  831. *
  832. * @return
  833. * - ESP_OK : Query is found in the request URL and copied to buffer
  834. * - ESP_ERR_NOT_FOUND : Query not found
  835. * - ESP_ERR_INVALID_ARG : Null arguments
  836. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid HTTP request pointer
  837. * - ESP_ERR_HTTPD_RESULT_TRUNC : Query string truncated
  838. */
  839. esp_err_t httpd_req_get_url_query_str(httpd_req_t *r, char *buf, size_t buf_len);
  840. /**
  841. * @brief Helper function to get a URL query tag from a query
  842. * string of the type param1=val1&param2=val2
  843. *
  844. * @note
  845. * - The components of URL query string (keys and values) are not URLdecoded.
  846. * The user must check for 'Content-Type' field in the request headers and
  847. * then depending upon the specified encoding (URLencoded or otherwise) apply
  848. * the appropriate decoding algorithm.
  849. * - If actual value size is greater than val_size, then the value is truncated,
  850. * accompanied by truncation error as return value.
  851. *
  852. * @param[in] qry Pointer to query string
  853. * @param[in] key The key to be searched in the query string
  854. * @param[out] val Pointer to the buffer into which the value will be copied if the key is found
  855. * @param[in] val_size Size of the user buffer "val"
  856. *
  857. * @return
  858. * - ESP_OK : Key is found in the URL query string and copied to buffer
  859. * - ESP_ERR_NOT_FOUND : Key not found
  860. * - ESP_ERR_INVALID_ARG : Null arguments
  861. * - ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
  862. */
  863. esp_err_t httpd_query_key_value(const char *qry, const char *key, char *val, size_t val_size);
  864. /**
  865. * @brief Test if a URI matches the given wildcard template.
  866. *
  867. * Template may end with "?" to make the previous character optional (typically a slash),
  868. * "*" for a wildcard match, and "?*" to make the previous character optional, and if present,
  869. * allow anything to follow.
  870. *
  871. * Example:
  872. * - * matches everything
  873. * - /foo/? matches /foo and /foo/
  874. * - /foo/\* (sans the backslash) matches /foo/ and /foo/bar, but not /foo or /fo
  875. * - /foo/?* or /foo/\*? (sans the backslash) matches /foo/, /foo/bar, and also /foo, but not /foox or /fo
  876. *
  877. * The special characters "?" and "*" anywhere else in the template will be taken literally.
  878. *
  879. * @param[in] uri_template URI template (pattern)
  880. * @param[in] uri_to_match URI to be matched
  881. * @param[in] match_upto how many characters of the URI buffer to test
  882. * (there may be trailing query string etc.)
  883. *
  884. * @return true if a match was found
  885. */
  886. bool httpd_uri_match_wildcard(const char *uri_template, const char *uri_to_match, size_t match_upto);
  887. /**
  888. * @brief API to send a complete HTTP response.
  889. *
  890. * This API will send the data as an HTTP response to the request.
  891. * This assumes that you have the entire response ready in a single
  892. * buffer. If you wish to send response in incremental chunks use
  893. * httpd_resp_send_chunk() instead.
  894. *
  895. * If no status code and content-type were set, by default this
  896. * will send 200 OK status code and content type as text/html.
  897. * You may call the following functions before this API to configure
  898. * the response headers :
  899. * httpd_resp_set_status() - for setting the HTTP status string,
  900. * httpd_resp_set_type() - for setting the Content Type,
  901. * httpd_resp_set_hdr() - for appending any additional field
  902. * value entries in the response header
  903. *
  904. * @note
  905. * - This API is supposed to be called only from the context of
  906. * a URI handler where httpd_req_t* request pointer is valid.
  907. * - Once this API is called, the request has been responded to.
  908. * - No additional data can then be sent for the request.
  909. * - Once this API is called, all request headers are purged, so
  910. * request headers need be copied into separate buffers if
  911. * they are required later.
  912. *
  913. * @param[in] r The request being responded to
  914. * @param[in] buf Buffer from where the content is to be fetched
  915. * @param[in] buf_len Length of the buffer, HTTPD_RESP_USE_STRLEN to use strlen()
  916. *
  917. * @return
  918. * - ESP_OK : On successfully sending the response packet
  919. * - ESP_ERR_INVALID_ARG : Null request pointer
  920. * - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  921. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  922. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request
  923. */
  924. esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len);
  925. /**
  926. * @brief API to send one HTTP chunk
  927. *
  928. * This API will send the data as an HTTP response to the
  929. * request. This API will use chunked-encoding and send the response
  930. * in the form of chunks. If you have the entire response contained in
  931. * a single buffer, please use httpd_resp_send() instead.
  932. *
  933. * If no status code and content-type were set, by default this will
  934. * send 200 OK status code and content type as text/html. You may
  935. * call the following functions before this API to configure the
  936. * response headers
  937. * httpd_resp_set_status() - for setting the HTTP status string,
  938. * httpd_resp_set_type() - for setting the Content Type,
  939. * httpd_resp_set_hdr() - for appending any additional field
  940. * value entries in the response header
  941. *
  942. * @note
  943. * - This API is supposed to be called only from the context of
  944. * a URI handler where httpd_req_t* request pointer is valid.
  945. * - When you are finished sending all your chunks, you must call
  946. * this function with buf_len as 0.
  947. * - Once this API is called, all request headers are purged, so
  948. * request headers need be copied into separate buffers if they
  949. * are required later.
  950. *
  951. * @param[in] r The request being responded to
  952. * @param[in] buf Pointer to a buffer that stores the data
  953. * @param[in] buf_len Length of the buffer, HTTPD_RESP_USE_STRLEN to use strlen()
  954. *
  955. * @return
  956. * - ESP_OK : On successfully sending the response packet chunk
  957. * - ESP_ERR_INVALID_ARG : Null request pointer
  958. * - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  959. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  960. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  961. */
  962. esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len);
  963. /**
  964. * @brief API to send a complete string as HTTP response.
  965. *
  966. * This API simply calls http_resp_send with buffer length
  967. * set to string length assuming the buffer contains a null
  968. * terminated string
  969. *
  970. * @param[in] r The request being responded to
  971. * @param[in] str String to be sent as response body
  972. *
  973. * @return
  974. * - ESP_OK : On successfully sending the response packet
  975. * - ESP_ERR_INVALID_ARG : Null request pointer
  976. * - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  977. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  978. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request
  979. */
  980. static inline esp_err_t httpd_resp_sendstr(httpd_req_t *r, const char *str) {
  981. return httpd_resp_send(r, str, (str == NULL) ? 0 : HTTPD_RESP_USE_STRLEN);
  982. }
  983. /**
  984. * @brief API to send a string as an HTTP response chunk.
  985. *
  986. * This API simply calls http_resp_send_chunk with buffer length
  987. * set to string length assuming the buffer contains a null
  988. * terminated string
  989. *
  990. * @param[in] r The request being responded to
  991. * @param[in] str String to be sent as response body (NULL to finish response packet)
  992. *
  993. * @return
  994. * - ESP_OK : On successfully sending the response packet
  995. * - ESP_ERR_INVALID_ARG : Null request pointer
  996. * - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
  997. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  998. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request
  999. */
  1000. static inline esp_err_t httpd_resp_sendstr_chunk(httpd_req_t *r, const char *str) {
  1001. return httpd_resp_send_chunk(r, str, (str == NULL) ? 0 : HTTPD_RESP_USE_STRLEN);
  1002. }
  1003. /* Some commonly used status codes */
  1004. #define HTTPD_200 "200 OK" /*!< HTTP Response 200 */
  1005. #define HTTPD_204 "204 No Content" /*!< HTTP Response 204 */
  1006. #define HTTPD_207 "207 Multi-Status" /*!< HTTP Response 207 */
  1007. #define HTTPD_400 "400 Bad Request" /*!< HTTP Response 400 */
  1008. #define HTTPD_404 "404 Not Found" /*!< HTTP Response 404 */
  1009. #define HTTPD_408 "408 Request Timeout" /*!< HTTP Response 408 */
  1010. #define HTTPD_500 "500 Internal Server Error" /*!< HTTP Response 500 */
  1011. /**
  1012. * @brief API to set the HTTP status code
  1013. *
  1014. * This API sets the status of the HTTP response to the value specified.
  1015. * By default, the '200 OK' response is sent as the response.
  1016. *
  1017. * @note
  1018. * - This API is supposed to be called only from the context of
  1019. * a URI handler where httpd_req_t* request pointer is valid.
  1020. * - This API only sets the status to this value. The status isn't
  1021. * sent out until any of the send APIs is executed.
  1022. * - Make sure that the lifetime of the status string is valid till
  1023. * send function is called.
  1024. *
  1025. * @param[in] r The request being responded to
  1026. * @param[in] status The HTTP status code of this response
  1027. *
  1028. * @return
  1029. * - ESP_OK : On success
  1030. * - ESP_ERR_INVALID_ARG : Null arguments
  1031. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1032. */
  1033. esp_err_t httpd_resp_set_status(httpd_req_t *r, const char *status);
  1034. /* Some commonly used content types */
  1035. #define HTTPD_TYPE_JSON "application/json" /*!< HTTP Content type JSON */
  1036. #define HTTPD_TYPE_TEXT "text/html" /*!< HTTP Content type text/HTML */
  1037. #define HTTPD_TYPE_OCTET "application/octet-stream" /*!< HTTP Content type octext-stream */
  1038. /**
  1039. * @brief API to set the HTTP content type
  1040. *
  1041. * This API sets the 'Content Type' field of the response.
  1042. * The default content type is 'text/html'.
  1043. *
  1044. * @note
  1045. * - This API is supposed to be called only from the context of
  1046. * a URI handler where httpd_req_t* request pointer is valid.
  1047. * - This API only sets the content type to this value. The type
  1048. * isn't sent out until any of the send APIs is executed.
  1049. * - Make sure that the lifetime of the type string is valid till
  1050. * send function is called.
  1051. *
  1052. * @param[in] r The request being responded to
  1053. * @param[in] type The Content Type of the response
  1054. *
  1055. * @return
  1056. * - ESP_OK : On success
  1057. * - ESP_ERR_INVALID_ARG : Null arguments
  1058. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1059. */
  1060. esp_err_t httpd_resp_set_type(httpd_req_t *r, const char *type);
  1061. /**
  1062. * @brief API to append any additional headers
  1063. *
  1064. * This API sets any additional header fields that need to be sent in the response.
  1065. *
  1066. * @note
  1067. * - This API is supposed to be called only from the context of
  1068. * a URI handler where httpd_req_t* request pointer is valid.
  1069. * - The header isn't sent out until any of the send APIs is executed.
  1070. * - The maximum allowed number of additional headers is limited to
  1071. * value of max_resp_headers in config structure.
  1072. * - Make sure that the lifetime of the field value strings are valid till
  1073. * send function is called.
  1074. *
  1075. * @param[in] r The request being responded to
  1076. * @param[in] field The field name of the HTTP header
  1077. * @param[in] value The value of this HTTP header
  1078. *
  1079. * @return
  1080. * - ESP_OK : On successfully appending new header
  1081. * - ESP_ERR_INVALID_ARG : Null arguments
  1082. * - ESP_ERR_HTTPD_RESP_HDR : Total additional headers exceed max allowed
  1083. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1084. */
  1085. esp_err_t httpd_resp_set_hdr(httpd_req_t *r, const char *field, const char *value);
  1086. /**
  1087. * @brief For sending out error code in response to HTTP request.
  1088. *
  1089. * @note
  1090. * - This API is supposed to be called only from the context of
  1091. * a URI handler where httpd_req_t* request pointer is valid.
  1092. * - Once this API is called, all request headers are purged, so
  1093. * request headers need be copied into separate buffers if
  1094. * they are required later.
  1095. * - If you wish to send additional data in the body of the
  1096. * response, please use the lower-level functions directly.
  1097. *
  1098. * @param[in] req Pointer to the HTTP request for which the response needs to be sent
  1099. * @param[in] error Error type to send
  1100. * @param[in] msg Error message string (pass NULL for default message)
  1101. *
  1102. * @return
  1103. * - ESP_OK : On successfully sending the response packet
  1104. * - ESP_ERR_INVALID_ARG : Null arguments
  1105. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  1106. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1107. */
  1108. esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_code_t error, const char *msg);
  1109. /**
  1110. * @brief Helper function for HTTP 404
  1111. *
  1112. * Send HTTP 404 message. If you wish to send additional data in the body of the
  1113. * response, please use the lower-level functions directly.
  1114. *
  1115. * @note
  1116. * - This API is supposed to be called only from the context of
  1117. * a URI handler where httpd_req_t* request pointer is valid.
  1118. * - Once this API is called, all request headers are purged, so
  1119. * request headers need be copied into separate buffers if
  1120. * they are required later.
  1121. *
  1122. * @param[in] r The request being responded to
  1123. *
  1124. * @return
  1125. * - ESP_OK : On successfully sending the response packet
  1126. * - ESP_ERR_INVALID_ARG : Null arguments
  1127. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  1128. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1129. */
  1130. static inline esp_err_t httpd_resp_send_404(httpd_req_t *r) {
  1131. return httpd_resp_send_err(r, HTTPD_404_NOT_FOUND, NULL);
  1132. }
  1133. /**
  1134. * @brief Helper function for HTTP 408
  1135. *
  1136. * Send HTTP 408 message. If you wish to send additional data in the body of the
  1137. * response, please use the lower-level functions directly.
  1138. *
  1139. * @note
  1140. * - This API is supposed to be called only from the context of
  1141. * a URI handler where httpd_req_t* request pointer is valid.
  1142. * - Once this API is called, all request headers are purged, so
  1143. * request headers need be copied into separate buffers if
  1144. * they are required later.
  1145. *
  1146. * @param[in] r The request being responded to
  1147. *
  1148. * @return
  1149. * - ESP_OK : On successfully sending the response packet
  1150. * - ESP_ERR_INVALID_ARG : Null arguments
  1151. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  1152. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1153. */
  1154. static inline esp_err_t httpd_resp_send_408(httpd_req_t *r) {
  1155. return httpd_resp_send_err(r, HTTPD_408_REQ_TIMEOUT, NULL);
  1156. }
  1157. /**
  1158. * @brief Helper function for HTTP 500
  1159. *
  1160. * Send HTTP 500 message. If you wish to send additional data in the body of the
  1161. * response, please use the lower-level functions directly.
  1162. *
  1163. * @note
  1164. * - This API is supposed to be called only from the context of
  1165. * a URI handler where httpd_req_t* request pointer is valid.
  1166. * - Once this API is called, all request headers are purged, so
  1167. * request headers need be copied into separate buffers if
  1168. * they are required later.
  1169. *
  1170. * @param[in] r The request being responded to
  1171. *
  1172. * @return
  1173. * - ESP_OK : On successfully sending the response packet
  1174. * - ESP_ERR_INVALID_ARG : Null arguments
  1175. * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
  1176. * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer
  1177. */
  1178. static inline esp_err_t httpd_resp_send_500(httpd_req_t *r) {
  1179. return httpd_resp_send_err(r, HTTPD_500_INTERNAL_SERVER_ERROR, NULL);
  1180. }
  1181. /**
  1182. * @brief Raw HTTP send
  1183. *
  1184. * Call this API if you wish to construct your custom response packet.
  1185. * When using this, all essential header, eg. HTTP version, Status Code,
  1186. * Content Type and Length, Encoding, etc. will have to be constructed
  1187. * manually, and HTTP delimeters (CRLF) will need to be placed correctly
  1188. * for separating sub-sections of the HTTP response packet.
  1189. *
  1190. * If the send override function is set, this API will end up
  1191. * calling that function eventually to send data out.
  1192. *
  1193. * @note
  1194. * - This API is supposed to be called only from the context of
  1195. * a URI handler where httpd_req_t* request pointer is valid.
  1196. * - Unless the response has the correct HTTP structure (which the
  1197. * user must now ensure) it is not guaranteed that it will be
  1198. * recognized by the client. For most cases, you wouldn't have
  1199. * to call this API, but you would rather use either of :
  1200. * httpd_resp_send(),
  1201. * httpd_resp_send_chunk()
  1202. *
  1203. * @param[in] r The request being responded to
  1204. * @param[in] buf Buffer from where the fully constructed packet is to be read
  1205. * @param[in] buf_len Length of the buffer
  1206. *
  1207. * @return
  1208. * - Bytes : Number of bytes that were sent successfully
  1209. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  1210. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  1211. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()
  1212. */
  1213. int httpd_send(httpd_req_t *r, const char *buf, size_t buf_len);
  1214. /**
  1215. * A low level API to send data on a given socket
  1216. *
  1217. * @note This API is not recommended to be used in any request handler.
  1218. * Use this only for advanced use cases, wherein some asynchronous
  1219. * data is to be sent over a socket.
  1220. *
  1221. * This internally calls the default send function, or the function registered by
  1222. * httpd_sess_set_send_override().
  1223. *
  1224. * @param[in] hd server instance
  1225. * @param[in] sockfd session socket file descriptor
  1226. * @param[in] buf buffer with bytes to send
  1227. * @param[in] buf_len data size
  1228. * @param[in] flags flags for the send() function
  1229. * @return
  1230. * - Bytes : The number of bytes sent successfully
  1231. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  1232. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send()
  1233. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send()
  1234. */
  1235. int httpd_socket_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags);
  1236. /**
  1237. * A low level API to receive data from a given socket
  1238. *
  1239. * @note This API is not recommended to be used in any request handler.
  1240. * Use this only for advanced use cases, wherein some asynchronous
  1241. * communication is required.
  1242. *
  1243. * This internally calls the default recv function, or the function registered by
  1244. * httpd_sess_set_recv_override().
  1245. *
  1246. * @param[in] hd server instance
  1247. * @param[in] sockfd session socket file descriptor
  1248. * @param[in] buf buffer with bytes to send
  1249. * @param[in] buf_len data size
  1250. * @param[in] flags flags for the send() function
  1251. * @return
  1252. * - Bytes : The number of bytes received successfully
  1253. * - 0 : Buffer length parameter is zero / connection closed by peer
  1254. * - HTTPD_SOCK_ERR_INVALID : Invalid arguments
  1255. * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv()
  1256. * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv()
  1257. */
  1258. int httpd_socket_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags);
  1259. /** End of Request / Response
  1260. * @}
  1261. */
  1262. /* ************** Group: Session ************** */
  1263. /** @name Session
  1264. * Functions for controlling sessions and accessing context data
  1265. * @{
  1266. */
  1267. /**
  1268. * @brief Get session context from socket descriptor
  1269. *
  1270. * Typically if a session context is created, it is available to URI handlers
  1271. * through the httpd_req_t structure. But, there are cases where the web
  1272. * server's send/receive functions may require the context (for example, for
  1273. * accessing keying information etc). Since the send/receive function only have
  1274. * the socket descriptor at their disposal, this API provides them with a way to
  1275. * retrieve the session context.
  1276. *
  1277. * @param[in] handle Handle to server returned by httpd_start
  1278. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  1279. *
  1280. * @return
  1281. * - void* : Pointer to the context associated with this session
  1282. * - NULL : Empty context / Invalid handle / Invalid socket fd
  1283. */
  1284. void *httpd_sess_get_ctx(httpd_handle_t handle, int sockfd);
  1285. /**
  1286. * @brief Set session context by socket descriptor
  1287. *
  1288. * @param[in] handle Handle to server returned by httpd_start
  1289. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  1290. * @param[in] ctx Context object to assign to the session
  1291. * @param[in] free_fn Function that should be called to free the context
  1292. */
  1293. void httpd_sess_set_ctx(httpd_handle_t handle, int sockfd, void *ctx, httpd_free_ctx_fn_t free_fn);
  1294. /**
  1295. * @brief Get session 'transport' context by socket descriptor
  1296. * @see httpd_sess_get_ctx()
  1297. *
  1298. * This context is used by the send/receive functions, for example to manage SSL context.
  1299. *
  1300. * @param[in] handle Handle to server returned by httpd_start
  1301. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  1302. * @return
  1303. * - void* : Pointer to the transport context associated with this session
  1304. * - NULL : Empty context / Invalid handle / Invalid socket fd
  1305. */
  1306. void *httpd_sess_get_transport_ctx(httpd_handle_t handle, int sockfd);
  1307. /**
  1308. * @brief Set session 'transport' context by socket descriptor
  1309. * @see httpd_sess_set_ctx()
  1310. *
  1311. * @param[in] handle Handle to server returned by httpd_start
  1312. * @param[in] sockfd The socket descriptor for which the context should be extracted.
  1313. * @param[in] ctx Transport context object to assign to the session
  1314. * @param[in] free_fn Function that should be called to free the transport context
  1315. */
  1316. void httpd_sess_set_transport_ctx(httpd_handle_t handle, int sockfd, void *ctx, httpd_free_ctx_fn_t free_fn);
  1317. /**
  1318. * @brief Get HTTPD global user context (it was set in the server config struct)
  1319. *
  1320. * @param[in] handle Handle to server returned by httpd_start
  1321. * @return global user context
  1322. */
  1323. void *httpd_get_global_user_ctx(httpd_handle_t handle);
  1324. /**
  1325. * @brief Get HTTPD global transport context (it was set in the server config struct)
  1326. *
  1327. * @param[in] handle Handle to server returned by httpd_start
  1328. * @return global transport context
  1329. */
  1330. void *httpd_get_global_transport_ctx(httpd_handle_t handle);
  1331. /**
  1332. * @brief Trigger an httpd session close externally
  1333. *
  1334. * @note Calling this API is only required in special circumstances wherein
  1335. * some application requires to close an httpd client session asynchronously.
  1336. *
  1337. * @param[in] handle Handle to server returned by httpd_start
  1338. * @param[in] sockfd The socket descriptor of the session to be closed
  1339. *
  1340. * @return
  1341. * - ESP_OK : On successfully initiating closure
  1342. * - ESP_FAIL : Failure to queue work
  1343. * - ESP_ERR_NOT_FOUND : Socket fd not found
  1344. * - ESP_ERR_INVALID_ARG : Null arguments
  1345. */
  1346. esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
  1347. /**
  1348. * @brief Update LRU counter for a given socket
  1349. *
  1350. * LRU Counters are internally associated with each session to monitor
  1351. * how recently a session exchanged traffic. When LRU purge is enabled,
  1352. * if a client is requesting for connection but maximum number of
  1353. * sockets/sessions is reached, then the session having the earliest
  1354. * LRU counter is closed automatically.
  1355. *
  1356. * Updating the LRU counter manually prevents the socket from being purged
  1357. * due to the Least Recently Used (LRU) logic, even though it might not
  1358. * have received traffic for some time. This is useful when all open
  1359. * sockets/session are frequently exchanging traffic but the user specifically
  1360. * wants one of the sessions to be kept open, irrespective of when it last
  1361. * exchanged a packet.
  1362. *
  1363. * @note Calling this API is only necessary if the LRU Purge Enable option
  1364. * is enabled.
  1365. *
  1366. * @param[in] handle Handle to server returned by httpd_start
  1367. * @param[in] sockfd The socket descriptor of the session for which LRU counter
  1368. * is to be updated
  1369. *
  1370. * @return
  1371. * - ESP_OK : Socket found and LRU counter updated
  1372. * - ESP_ERR_NOT_FOUND : Socket not found
  1373. * - ESP_ERR_INVALID_ARG : Null arguments
  1374. */
  1375. esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd);
  1376. /**
  1377. * @brief Returns list of current socket descriptors of active sessions
  1378. *
  1379. * @param[in] handle Handle to server returned by httpd_start
  1380. * @param[in,out] fds In: Size of provided client_fds array
  1381. * Out: Number of valid client fds returned in client_fds,
  1382. * @param[out] client_fds Array of client fds
  1383. *
  1384. * @note Size of provided array has to be equal or greater then maximum number of opened
  1385. * sockets, configured upon initialization with max_open_sockets field in
  1386. * httpd_config_t structure.
  1387. *
  1388. * @return
  1389. * - ESP_OK : Successfully retrieved session list
  1390. * - ESP_ERR_INVALID_ARG : Wrong arguments or list is longer than provided array
  1391. */
  1392. esp_err_t httpd_get_client_list(httpd_handle_t handle, size_t *fds, int *client_fds);
  1393. /** End of Session
  1394. * @}
  1395. */
  1396. /* ************** Group: Work Queue ************** */
  1397. /** @name Work Queue
  1398. * APIs related to the HTTPD Work Queue
  1399. * @{
  1400. */
  1401. /**
  1402. * @brief Prototype of the HTTPD work function
  1403. * Please refer to httpd_queue_work() for more details.
  1404. * @param[in] arg The arguments for this work function
  1405. */
  1406. typedef void (*httpd_work_fn_t)(void *arg);
  1407. /**
  1408. * @brief Queue execution of a function in HTTPD's context
  1409. *
  1410. * This API queues a work function for asynchronous execution
  1411. *
  1412. * @note Some protocols require that the web server generate some asynchronous data
  1413. * and send it to the persistently opened connection. This facility is for use
  1414. * by such protocols.
  1415. *
  1416. * @param[in] handle Handle to server returned by httpd_start
  1417. * @param[in] work Pointer to the function to be executed in the HTTPD's context
  1418. * @param[in] arg Pointer to the arguments that should be passed to this function
  1419. *
  1420. * @return
  1421. * - ESP_OK : On successfully queueing the work
  1422. * - ESP_FAIL : Failure in ctrl socket
  1423. * - ESP_ERR_INVALID_ARG : Null arguments
  1424. */
  1425. esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *arg);
  1426. /** End of Group Work Queue
  1427. * @}
  1428. */
  1429. /* ************** Group: WebSocket ************** */
  1430. /** @name WebSocket
  1431. * Functions and structs for WebSocket server
  1432. * @{
  1433. */
  1434. #ifdef CONFIG_HTTPD_WS_SUPPORT
  1435. /**
  1436. * @brief Enum for WebSocket packet types (Opcode in the header)
  1437. * @note Please refer to RFC6455 Section 5.4 for more details
  1438. */
  1439. typedef enum {
  1440. HTTPD_WS_TYPE_CONTINUE = 0x0,
  1441. HTTPD_WS_TYPE_TEXT = 0x1,
  1442. HTTPD_WS_TYPE_BINARY = 0x2,
  1443. HTTPD_WS_TYPE_CLOSE = 0x8,
  1444. HTTPD_WS_TYPE_PING = 0x9,
  1445. HTTPD_WS_TYPE_PONG = 0xA
  1446. } httpd_ws_type_t;
  1447. /**
  1448. * @brief Enum for client info description
  1449. */
  1450. typedef enum {
  1451. HTTPD_WS_CLIENT_INVALID = 0x0,
  1452. HTTPD_WS_CLIENT_HTTP = 0x1,
  1453. HTTPD_WS_CLIENT_WEBSOCKET = 0x2,
  1454. } httpd_ws_client_info_t;
  1455. /**
  1456. * @brief WebSocket frame format
  1457. */
  1458. typedef struct httpd_ws_frame {
  1459. bool final; /*!< Final frame:
  1460. For received frames this field indicates whether the `FIN` flag was set.
  1461. For frames to be transmitted, this field is only used if the `fragmented`
  1462. option is set as well. If `fragmented` is false, the `FIN` flag is set
  1463. by default, marking the ws_frame as a complete/unfragmented message
  1464. (esp_http_server doesn't automatically fragment messages) */
  1465. bool fragmented; /*!< Indication that the frame allocated for transmission is a message fragment,
  1466. so the `FIN` flag is set manually according to the `final` option.
  1467. This flag is never set for received messages */
  1468. httpd_ws_type_t type; /*!< WebSocket frame type */
  1469. uint8_t *payload; /*!< Pre-allocated data buffer */
  1470. size_t len; /*!< Length of the WebSocket data */
  1471. } httpd_ws_frame_t;
  1472. /**
  1473. * @brief Receive and parse a WebSocket frame
  1474. *
  1475. * @note Calling httpd_ws_recv_frame() with max_len as 0 will give actual frame size in pkt->len.
  1476. * The user can dynamically allocate space for pkt->payload as per this length and call httpd_ws_recv_frame() again to get the actual data.
  1477. * Please refer to the corresponding example for usage.
  1478. *
  1479. * @param[in] req Current request
  1480. * @param[out] pkt WebSocket packet
  1481. * @param[in] max_len Maximum length for receive
  1482. * @return
  1483. * - ESP_OK : On successful
  1484. * - ESP_FAIL : Socket errors occurs
  1485. * - ESP_ERR_INVALID_STATE : Handshake was already done beforehand
  1486. * - ESP_ERR_INVALID_ARG : Argument is invalid (null or non-WebSocket)
  1487. */
  1488. esp_err_t httpd_ws_recv_frame(httpd_req_t *req, httpd_ws_frame_t *pkt, size_t max_len);
  1489. /**
  1490. * @brief Construct and send a WebSocket frame
  1491. * @param[in] req Current request
  1492. * @param[in] pkt WebSocket frame
  1493. * @return
  1494. * - ESP_OK : On successful
  1495. * - ESP_FAIL : When socket errors occurs
  1496. * - ESP_ERR_INVALID_STATE : Handshake was already done beforehand
  1497. * - ESP_ERR_INVALID_ARG : Argument is invalid (null or non-WebSocket)
  1498. */
  1499. esp_err_t httpd_ws_send_frame(httpd_req_t *req, httpd_ws_frame_t *pkt);
  1500. /**
  1501. * @brief Low level send of a WebSocket frame out of the scope of current request
  1502. * using internally configured httpd send function
  1503. *
  1504. * This API should rarely be called directly, with an exception of asynchronous send using httpd_queue_work.
  1505. *
  1506. * @param[in] hd Server instance data
  1507. * @param[in] fd Socket descriptor for sending data
  1508. * @param[in] frame WebSocket frame
  1509. * @return
  1510. * - ESP_OK : On successful
  1511. * - ESP_FAIL : When socket errors occurs
  1512. * - ESP_ERR_INVALID_STATE : Handshake was already done beforehand
  1513. * - ESP_ERR_INVALID_ARG : Argument is invalid (null or non-WebSocket)
  1514. */
  1515. esp_err_t httpd_ws_send_frame_async(httpd_handle_t hd, int fd, httpd_ws_frame_t *frame);
  1516. /**
  1517. * @brief Checks the supplied socket descriptor if it belongs to any active client
  1518. * of this server instance and if the websoket protocol is active
  1519. *
  1520. * @param[in] hd Server instance data
  1521. * @param[in] fd Socket descriptor
  1522. * @return
  1523. * - HTTPD_WS_CLIENT_INVALID : This fd is not a client of this httpd
  1524. * - HTTPD_WS_CLIENT_HTTP : This fd is an active client, protocol is not WS
  1525. * - HTTPD_WS_CLIENT_WEBSOCKET : This fd is an active client, protocol is WS
  1526. */
  1527. httpd_ws_client_info_t httpd_ws_get_fd_info(httpd_handle_t hd, int fd);
  1528. #endif /* CONFIG_HTTPD_WS_SUPPORT */
  1529. /** End of WebSocket related stuff
  1530. * @}
  1531. */
  1532. #ifdef __cplusplus
  1533. }
  1534. #endif
  1535. #endif /* ! _ESP_HTTP_SERVER_H_ */