mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-01 03:34:32 +02:00
HTTP Server : Add helper APIs for sending string content
Note : In future consider deprecating usage of -1 for setting buffer length equal to string length in APIs httpd_resp_send() and httpd_resp_send_chunk()
This commit is contained in:
@@ -744,6 +744,10 @@ esp_err_t httpd_req_get_url_query_str(httpd_req_t *r, char *buf, size_t buf_len)
|
||||
*/
|
||||
esp_err_t httpd_query_key_value(const char *qry, const char *key, char *val, size_t val_size);
|
||||
|
||||
/* Symbol to be used as length parameter in httpd_resp_send APIs
|
||||
* for setting buffer length to string length */
|
||||
#define HTTPD_RESP_USE_STRLEN -1
|
||||
|
||||
/**
|
||||
* @brief API to send a complete HTTP response.
|
||||
*
|
||||
@@ -772,7 +776,7 @@ esp_err_t httpd_query_key_value(const char *qry, const char *key, char *val, siz
|
||||
*
|
||||
* @param[in] r The request being responded to
|
||||
* @param[in] buf Buffer from where the content is to be fetched
|
||||
* @param[in] buf_len Length of the buffer, -1 to use strlen()
|
||||
* @param[in] buf_len Length of the buffer, HTTPD_RESP_USE_STRLEN to use strlen()
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK : On successfully sending the response packet
|
||||
@@ -811,7 +815,7 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len);
|
||||
*
|
||||
* @param[in] r The request being responded to
|
||||
* @param[in] buf Pointer to a buffer that stores the data
|
||||
* @param[in] buf_len Length of the data from the buffer that should be sent out, -1 to use strlen()
|
||||
* @param[in] buf_len Length of the buffer, HTTPD_RESP_USE_STRLEN to use strlen()
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK : On successfully sending the response packet chunk
|
||||
@@ -822,6 +826,48 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len);
|
||||
*/
|
||||
esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len);
|
||||
|
||||
/**
|
||||
* @brief API to send a complete string as HTTP response.
|
||||
*
|
||||
* This API simply calls http_resp_send with buffer length
|
||||
* set to string length assuming the buffer contains a null
|
||||
* terminated string
|
||||
*
|
||||
* @param[in] r The request being responded to
|
||||
* @param[in] str String to be sent as response body
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK : On successfully sending the response packet
|
||||
* - ESP_ERR_INVALID_ARG : Null request pointer
|
||||
* - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
|
||||
* - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
|
||||
* - ESP_ERR_HTTPD_INVALID_REQ : Invalid request
|
||||
*/
|
||||
inline esp_err_t httpd_resp_sendstr(httpd_req_t *r, const char *str) {
|
||||
return httpd_resp_send(r, str, (str == NULL) ? 0 : strlen(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief API to send a string as an HTTP response chunk.
|
||||
*
|
||||
* This API simply calls http_resp_send_chunk with buffer length
|
||||
* set to string length assuming the buffer contains a null
|
||||
* terminated string
|
||||
*
|
||||
* @param[in] r The request being responded to
|
||||
* @param[in] str String to be sent as response body (NULL to finish response packet)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK : On successfully sending the response packet
|
||||
* - ESP_ERR_INVALID_ARG : Null request pointer
|
||||
* - ESP_ERR_HTTPD_RESP_HDR : Essential headers are too large for internal buffer
|
||||
* - ESP_ERR_HTTPD_RESP_SEND : Error in raw send
|
||||
* - ESP_ERR_HTTPD_INVALID_REQ : Invalid request
|
||||
*/
|
||||
inline esp_err_t httpd_resp_sendstr_chunk(httpd_req_t *r, const char *str) {
|
||||
return httpd_resp_send_chunk(r, str, (str == NULL) ? 0 : strlen(str));
|
||||
}
|
||||
|
||||
/* Some commonly used status codes */
|
||||
#define HTTPD_200 "200 OK" /*!< HTTP Response 200 */
|
||||
#define HTTPD_204 "204 No Content" /*!< HTTP Response 204 */
|
||||
|
@@ -246,7 +246,9 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len)
|
||||
const char *colon_separator = ": ";
|
||||
const char *cr_lf_seperator = "\r\n";
|
||||
|
||||
if (buf_len == -1) buf_len = strlen(buf);
|
||||
if (buf_len == HTTPD_RESP_USE_STRLEN) {
|
||||
buf_len = strlen(buf);
|
||||
}
|
||||
|
||||
/* Request headers are no longer available */
|
||||
ra->req_hdrs_count = 0;
|
||||
@@ -306,7 +308,9 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len
|
||||
return ESP_ERR_HTTPD_INVALID_REQ;
|
||||
}
|
||||
|
||||
if (buf_len == -1) buf_len = strlen(buf);
|
||||
if (buf_len == HTTPD_RESP_USE_STRLEN) {
|
||||
buf_len = strlen(buf);
|
||||
}
|
||||
|
||||
struct httpd_req_aux *ra = r->aux;
|
||||
const char *httpd_chunked_hdr_str = "HTTP/1.1 %s\r\nContent-Type: %s\r\nTransfer-Encoding: chunked\r\n";
|
||||
|
Reference in New Issue
Block a user