From ae5989528e119d05d8b1db0027064dc5605e566d Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Tue, 9 Oct 2018 18:03:41 +0530 Subject: [PATCH] HTTP Server : Added helper functions for sending HTTP error 408 and 500 --- components/http_server/include/http_server.h | 47 ++++++++++++++++++++ components/http_server/src/httpd_txrx.c | 10 +++++ 2 files changed, 57 insertions(+) diff --git a/components/http_server/include/http_server.h b/components/http_server/include/http_server.h index dec25768df..6a8e65c794 100644 --- a/components/http_server/include/http_server.h +++ b/components/http_server/include/http_server.h @@ -687,6 +687,7 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, size_t buf_len) #define HTTPD_207 "207 Multi-Status" /*!< HTTP Response 207 */ #define HTTPD_400 "400 Bad Request" /*!< HTTP Response 400 */ #define HTTPD_404 "404 Not Found" /*!< HTTP Response 404 */ +#define HTTPD_408 "408 Request Timeout" /*!< HTTP Response 408 */ #define HTTPD_500 "500 Internal Server Error" /*!< HTTP Response 500 */ /** @@ -791,6 +792,52 @@ esp_err_t httpd_resp_set_hdr(httpd_req_t *r, const char *field, const char *valu */ esp_err_t httpd_resp_send_404(httpd_req_t *r); +/** + * @brief Helper function for HTTP 408 + * + * Send HTTP 408 message. If you wish to send additional data in the body of the + * response, please use the lower-level functions directly. + * + * @note + * - This API is supposed to be called only from the context of + * a URI handler where httpd_req_t* request pointer is valid. + * - Once this API is called, all request headers are purged, so + * request headers need be copied into separate buffers if + * they are required later. + * + * @param[in] r The request being responded to + * + * @return + * - ESP_OK : On successfully sending the response packet + * - ESP_ERR_INVALID_ARG : Null arguments + * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send + * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer + */ +esp_err_t httpd_resp_send_408(httpd_req_t *r); + +/** + * @brief Helper function for HTTP 500 + * + * Send HTTP 500 message. If you wish to send additional data in the body of the + * response, please use the lower-level functions directly. + * + * @note + * - This API is supposed to be called only from the context of + * a URI handler where httpd_req_t* request pointer is valid. + * - Once this API is called, all request headers are purged, so + * request headers need be copied into separate buffers if + * they are required later. + * + * @param[in] r The request being responded to + * + * @return + * - ESP_OK : On successfully sending the response packet + * - ESP_ERR_INVALID_ARG : Null arguments + * - ESP_ERR_HTTPD_RESP_SEND : Error in raw send + * - ESP_ERR_HTTPD_INVALID_REQ : Invalid request pointer + */ +esp_err_t httpd_resp_send_500(httpd_req_t *r); + /** * @brief Raw HTTP send * diff --git a/components/http_server/src/httpd_txrx.c b/components/http_server/src/httpd_txrx.c index ed90cb75da..1ccebabe4f 100644 --- a/components/http_server/src/httpd_txrx.c +++ b/components/http_server/src/httpd_txrx.c @@ -376,6 +376,16 @@ esp_err_t httpd_resp_send_404(httpd_req_t *r) return httpd_resp_send_err(r, HTTPD_404_NOT_FOUND); } +esp_err_t httpd_resp_send_408(httpd_req_t *r) +{ + return httpd_resp_send_err(r, HTTPD_408_REQ_TIMEOUT); +} + +esp_err_t httpd_resp_send_500(httpd_req_t *r) +{ + return httpd_resp_send_err(r, HTTPD_500_SERVER_ERROR); +} + esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_resp_t error) { const char *msg; -- 2.40.0