esp_http_client: Fix minor bugs in esp_http_client_write and esp_http_client_open APIs

`esp_http_client_write` API puts a constraint on the maximum length of the data that can be
written, which is equal to client handle buffer size, but the data to be sent can be more
than that, so in this case, this API has to be called multiple times.

In `esp_http_client_open` API, the return value of `transport_write` API, used to send HTTP
request, is not checked, and in rare cases, data written will be less than expected which will
cause a problem. So there are fixes for these minor issues in this MR.
This commit is contained in:
Jitin George 2018-08-02 17:21:35 +05:30 committed by bot
parent 76f864511d
commit 2122e5f83d

View File

@ -932,10 +932,17 @@ esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)
}
client->request->buffer->data[wlen] = 0;
ESP_LOGD(TAG, "Write header[%d]: %s", header_index, client->request->buffer->data);
if (transport_write(client->transport, client->request->buffer->data, wlen, client->timeout_ms) <= 0) {
ESP_LOGE(TAG, "Error write request");
esp_http_client_close(client);
return ESP_ERR_HTTP_WRITE_DATA;
int widx = 0, wret = 0;
while (wlen > 0) {
wret = transport_write(client->transport, client->request->buffer->data + widx, wlen, client->timeout_ms);
if (wret <= 0) {
ESP_LOGE(TAG, "Error write request");
esp_http_client_close(client);
return ESP_ERR_HTTP_WRITE_DATA;
}
widx += wret;
wlen -= wret;
}
wlen = client->buffer_size;
}
@ -949,14 +956,10 @@ int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, i
if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
return ESP_FAIL;
}
int need_write;
int wlen = 0, widx = 0;
while (len > 0) {
need_write = len;
if (need_write > client->buffer_size) {
need_write = client->buffer_size;
}
wlen = transport_write(client->transport, buffer + widx, need_write, client->timeout_ms);
wlen = transport_write(client->transport, buffer + widx, len, client->timeout_ms);
if (wlen <= 0) {
return wlen;
}