mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'bugfix/support_all_http_status_codes' into 'master'
fix(esp_http_server): Add support for sending custom HTTP status Closes IDFGH-11235 See merge request espressif/esp-idf!27419
This commit is contained in:
commit
e691c8b43b
@ -1293,6 +1293,30 @@ esp_err_t httpd_resp_set_hdr(httpd_req_t *r, const char *field, const char *valu
|
|||||||
*/
|
*/
|
||||||
esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_code_t error, const char *msg);
|
esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_code_t error, const char *msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief For sending out custom error code in response to HTTP request.
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
* - If you wish to send additional data in the body of the
|
||||||
|
* response, please use the lower-level functions directly.
|
||||||
|
*
|
||||||
|
* @param[in] req Pointer to the HTTP request for which the response needs to be sent
|
||||||
|
* @param[in] status Error status to send
|
||||||
|
* @param[in] msg Error message string
|
||||||
|
*
|
||||||
|
* @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_custom_err(httpd_req_t *req, const char *status, const char *msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Helper function for HTTP 404
|
* @brief Helper function for HTTP 404
|
||||||
*
|
*
|
||||||
|
@ -483,6 +483,45 @@ esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_code_t error, const ch
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
esp_err_t httpd_resp_send_custom_err(httpd_req_t *req, const char *status, const char *msg)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, LOG_FMT("%s - %s"), status, msg);
|
||||||
|
|
||||||
|
/* Set error code in HTTP response */
|
||||||
|
httpd_resp_set_status(req, status);
|
||||||
|
httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
|
||||||
|
|
||||||
|
#ifdef CONFIG_HTTPD_ERR_RESP_NO_DELAY
|
||||||
|
/* Use TCP_NODELAY option to force socket to send data in buffer
|
||||||
|
* This ensures that the error message is sent before the socket
|
||||||
|
* is closed */
|
||||||
|
struct httpd_req_aux *ra = req->aux;
|
||||||
|
int nodelay = 1;
|
||||||
|
if (setsockopt(ra->sd->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) {
|
||||||
|
/* If failed to turn on TCP_NODELAY, throw warning and continue */
|
||||||
|
ESP_LOGW(TAG, LOG_FMT("error calling setsockopt : %d"), errno);
|
||||||
|
nodelay = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Send HTTP error message */
|
||||||
|
esp_err_t ret = httpd_resp_send(req, msg, HTTPD_RESP_USE_STRLEN);
|
||||||
|
|
||||||
|
#ifdef CONFIG_HTTPD_ERR_RESP_NO_DELAY
|
||||||
|
/* If TCP_NODELAY was set successfully above, time to disable it */
|
||||||
|
if (nodelay == 1) {
|
||||||
|
nodelay = 0;
|
||||||
|
if (setsockopt(ra->sd->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) {
|
||||||
|
/* If failed to turn off TCP_NODELAY, throw error and
|
||||||
|
* return failure to signal for socket closure */
|
||||||
|
ESP_LOGE(TAG, LOG_FMT("error calling setsockopt : %d"), errno);
|
||||||
|
return ESP_ERR_INVALID_STATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
esp_err_t httpd_register_err_handler(httpd_handle_t handle,
|
esp_err_t httpd_register_err_handler(httpd_handle_t handle,
|
||||||
httpd_err_code_t error,
|
httpd_err_code_t error,
|
||||||
httpd_err_handler_func_t err_handler_fn)
|
httpd_err_handler_func_t err_handler_fn)
|
||||||
|
Loading…
Reference in New Issue
Block a user