From ffdc621f1b1ebacdf7ed3ce367f00618a5e9d0a8 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Thu, 17 Mar 2022 13:38:13 +0100 Subject: [PATCH] net: http_client: Fix http_client_req() return value http_client_req() was supposed to return the number of bytes sent as a HTTP request. The return value was not riht however due to some bugs in helper functions: * http_send_data() returned the current buffer position istead of the number of bytes actually sent. This could result in counting the same data into the total request size several times. A helper variable was added to track how many bytes were actually sent to the network. * http_flush_data() forwarded the return value of sendall() helper function. That function however did not return number of bytes sent, but 0 or a negative error code. Additionally, change the return type of sendall() function - according to standard the ssize_t type is only capable of holding -1 negative value, but the function could return the full range of negative errno values. Use int instead. Signed-off-by: Robert Lubos --- subsys/net/lib/http/http_client.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/subsys/net/lib/http/http_client.c b/subsys/net/lib/http/http_client.c index 35f0415c199..7ba9a5dfe1f 100644 --- a/subsys/net/lib/http/http_client.c +++ b/subsys/net/lib/http/http_client.c @@ -29,7 +29,7 @@ LOG_MODULE_REGISTER(net_http, CONFIG_NET_HTTP_LOG_LEVEL); #define HTTP_CONTENT_LEN_SIZE 6 #define MAX_SEND_BUF_LEN 192 -static ssize_t sendall(int sock, const void *buf, size_t len) +static int sendall(int sock, const void *buf, size_t len) { while (len) { ssize_t out_len = zsock_send(sock, buf, len, 0); @@ -53,6 +53,7 @@ static int http_send_data(int sock, char *send_buf, va_list va; int ret, end_of_send = *send_buf_pos; int end_of_data, remaining_len; + int sent = 0; va_start(va, send_buf_pos); @@ -85,7 +86,7 @@ static int http_send_data(int sock, char *send_buf, end_of_send, ret); goto err; } - + sent += end_of_send; end_of_send = 0; continue; } else { @@ -110,7 +111,7 @@ static int http_send_data(int sock, char *send_buf, *send_buf_pos = end_of_send; - return end_of_send; + return sent; err: va_end(va); @@ -120,9 +121,16 @@ err: static int http_flush_data(int sock, const char *send_buf, size_t send_buf_len) { + int ret; + LOG_HEXDUMP_DBG(send_buf, send_buf_len, "Data to send"); - return sendall(sock, send_buf, send_buf_len); + ret = sendall(sock, send_buf, send_buf_len); + if (ret < 0) { + return ret; + } + + return (int)send_buf_len; } static void print_header_field(size_t len, const char *str) @@ -664,6 +672,8 @@ int http_client_req(int sock, struct http_request *req, if (ret < 0) { goto out; } + + total_sent += ret; } if (send_buf_pos > 0) {