Skip to content

Commit ffdc621

Browse files
rluboscarlescufi
authored andcommitted
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 <[email protected]>
1 parent 6ac77d6 commit ffdc621

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

subsys/net/lib/http/http_client.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LOG_MODULE_REGISTER(net_http, CONFIG_NET_HTTP_LOG_LEVEL);
2929
#define HTTP_CONTENT_LEN_SIZE 6
3030
#define MAX_SEND_BUF_LEN 192
3131

32-
static ssize_t sendall(int sock, const void *buf, size_t len)
32+
static int sendall(int sock, const void *buf, size_t len)
3333
{
3434
while (len) {
3535
ssize_t out_len = zsock_send(sock, buf, len, 0);
@@ -53,6 +53,7 @@ static int http_send_data(int sock, char *send_buf,
5353
va_list va;
5454
int ret, end_of_send = *send_buf_pos;
5555
int end_of_data, remaining_len;
56+
int sent = 0;
5657

5758
va_start(va, send_buf_pos);
5859

@@ -85,7 +86,7 @@ static int http_send_data(int sock, char *send_buf,
8586
end_of_send, ret);
8687
goto err;
8788
}
88-
89+
sent += end_of_send;
8990
end_of_send = 0;
9091
continue;
9192
} else {
@@ -110,7 +111,7 @@ static int http_send_data(int sock, char *send_buf,
110111

111112
*send_buf_pos = end_of_send;
112113

113-
return end_of_send;
114+
return sent;
114115

115116
err:
116117
va_end(va);
@@ -120,9 +121,16 @@ static int http_send_data(int sock, char *send_buf,
120121

121122
static int http_flush_data(int sock, const char *send_buf, size_t send_buf_len)
122123
{
124+
int ret;
125+
123126
LOG_HEXDUMP_DBG(send_buf, send_buf_len, "Data to send");
124127

125-
return sendall(sock, send_buf, send_buf_len);
128+
ret = sendall(sock, send_buf, send_buf_len);
129+
if (ret < 0) {
130+
return ret;
131+
}
132+
133+
return (int)send_buf_len;
126134
}
127135

128136
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,
664672
if (ret < 0) {
665673
goto out;
666674
}
675+
676+
total_sent += ret;
667677
}
668678

669679
if (send_buf_pos > 0) {

0 commit comments

Comments
 (0)