]> granicus.if.org Git - esp-idf/commitdiff
HTTP Server : Add helper APIs for sending string content
authorAnurag Kar <anurag.kar@espressif.com>
Sat, 5 Jan 2019 21:14:45 +0000 (02:44 +0530)
committerAnurag Kar <anurag.kar@espressif.com>
Mon, 14 Jan 2019 03:06:18 +0000 (08:36 +0530)
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()

components/esp_http_server/include/esp_http_server.h
components/esp_http_server/src/httpd_txrx.c

index 793f3dd806f8f9d1c38f5800d846fa309ef4478a..b3da269f0181e8860e120836b862a25f9adc7489 100644 (file)
@@ -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 */
index 69a5ba0048908cf8ad3d3a8cb2eb369a77aa63d5..7721b11b797624dfa7090729f3203669de762d25 100644 (file)
@@ -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";