From 24587ccbbf8787fbf5bac356de2aa58f6df2c3f0 Mon Sep 17 00:00:00 2001 From: Piyush Shah Date: Sat, 6 Jun 2020 00:42:44 +0530 Subject: [PATCH] esp_http_server: Expose low level socket send/recv APIs For some advanced use cases, the low level APIs may be useful. --- .../esp_http_server/include/esp_http_server.h | 47 +++++++++++++++++++ components/esp_http_server/src/httpd_txrx.c | 24 ++++++++++ 2 files changed, 71 insertions(+) diff --git a/components/esp_http_server/include/esp_http_server.h b/components/esp_http_server/include/esp_http_server.h index e1e5c288d..b985f8a29 100644 --- a/components/esp_http_server/include/esp_http_server.h +++ b/components/esp_http_server/include/esp_http_server.h @@ -1292,6 +1292,53 @@ static inline esp_err_t httpd_resp_send_500(httpd_req_t *r) { */ int httpd_send(httpd_req_t *r, const char *buf, size_t buf_len); +/** + * A low level API to send data on a given socket + * + * @note This API is not recommended to be used in any request handler. + * Use this only for advanced use cases, wherein some asynchronous + * data is to be sent over a socket. + * + * This internally calls the default send function, or the function registered by + * httpd_sess_set_send_override(). + * + * @param[in] hd server instance + * @param[in] sockfd session socket file descriptor + * @param[in] buf buffer with bytes to send + * @param[in] buf_len data size + * @param[in] flags flags for the send() function + * @return + * - Bytes : The number of bytes sent successfully + * - HTTPD_SOCK_ERR_INVALID : Invalid arguments + * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket send() + * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket send() + */ +int httpd_socket_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags); + +/** + * A low level API to receive data from a given socket + * + * @note This API is not recommended to be used in any request handler. + * Use this only for advanced use cases, wherein some asynchronous + * communication is required. + * + * This internally calls the default recv function, or the function registered by + * httpd_sess_set_recv_override(). + * + * @param[in] hd server instance + * @param[in] sockfd session socket file descriptor + * @param[in] buf buffer with bytes to send + * @param[in] buf_len data size + * @param[in] flags flags for the send() function + * @return + * - Bytes : The number of bytes received successfully + * - 0 : Buffer length parameter is zero / connection closed by peer + * - HTTPD_SOCK_ERR_INVALID : Invalid arguments + * - HTTPD_SOCK_ERR_TIMEOUT : Timeout/interrupted while calling socket recv() + * - HTTPD_SOCK_ERR_FAIL : Unrecoverable error while calling socket recv() + */ +int httpd_socket_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags); + /** End of Request / Response * @} */ diff --git a/components/esp_http_server/src/httpd_txrx.c b/components/esp_http_server/src/httpd_txrx.c index 8f81ae2b1..bb359c4b0 100644 --- a/components/esp_http_server/src/httpd_txrx.c +++ b/components/esp_http_server/src/httpd_txrx.c @@ -599,3 +599,27 @@ int httpd_default_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, } return ret; } + +int httpd_socket_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_len, int flags) +{ + struct sock_db *sess = httpd_sess_get(hd, sockfd); + if (!sess) { + return ESP_ERR_INVALID_ARG; + } + if (!sess->send_fn) { + return ESP_ERR_INVALID_STATE; + } + return sess->send_fn(hd, sockfd, buf, buf_len, flags); +} + +int httpd_socket_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len, int flags) +{ + struct sock_db *sess = httpd_sess_get(hd, sockfd); + if (!sess) { + return ESP_ERR_INVALID_ARG; + } + if (!sess->recv_fn) { + return ESP_ERR_INVALID_STATE; + } + return sess->recv_fn(hd, sockfd, buf, buf_len, flags); +}