diff --git a/components/esp_http_server/src/httpd_txrx.c b/components/esp_http_server/src/httpd_txrx.c index aab3000cff..0467f7b5a1 100644 --- a/components/esp_http_server/src/httpd_txrx.c +++ b/components/esp_http_server/src/httpd_txrx.c @@ -257,13 +257,13 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len) ESP_LOGE(TAG, "Unable to allocate httpd send buffer"); return ESP_ERR_HTTPD_ALLOC_MEM; } - ESP_LOGD(TAG, "httpd send buffer size = %d", strlen(res_buf)); esp_err_t ret = snprintf(res_buf, required_size, httpd_hdr_str, ra->status, ra->content_type, buf_len); if (ret < 0 || ret >= required_size) { free(res_buf); return ESP_ERR_HTTPD_RESP_HDR; } + ESP_LOGD(TAG, "httpd send buffer size = %d", strlen(res_buf)); ret = httpd_send_all(r, res_buf, strlen(res_buf)); free(res_buf); if (ret != ESP_OK) { @@ -332,23 +332,23 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len /* Request headers are no longer available */ ra->req_hdrs_count = 0; - /* Calculate the size of the headers. +1 for the null terminator */ - size_t required_size = snprintf(NULL, 0, httpd_chunked_hdr_str, ra->status, ra->content_type) + 1; - if (required_size > ra->max_req_hdr_len) { - return ESP_ERR_HTTPD_RESP_HDR; - } - char *res_buf = malloc(required_size); /* Temporary buffer to store the headers */ - if (res_buf == NULL) { - ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer"); - return ESP_ERR_HTTPD_ALLOC_MEM; - } - ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf)); if (!ra->first_chunk_sent) { + /* Calculate the size of the headers. +1 for the null terminator */ + size_t required_size = snprintf(NULL, 0, httpd_chunked_hdr_str, ra->status, ra->content_type) + 1; + if (required_size > ra->max_req_hdr_len) { + return ESP_ERR_HTTPD_RESP_HDR; + } + char *res_buf = malloc(required_size); /* Temporary buffer to store the headers */ + if (res_buf == NULL) { + ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer"); + return ESP_ERR_HTTPD_ALLOC_MEM; + } esp_err_t ret = snprintf(res_buf, required_size, httpd_chunked_hdr_str, ra->status, ra->content_type); if (ret < 0 || ret >= required_size) { free(res_buf); return ESP_ERR_HTTPD_RESP_HDR; } + ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf)); /* Size of essential headers is limited by scratch buffer size */ ret = httpd_send_all(r, res_buf, strlen(res_buf)); free(res_buf);