net: http_client: Fix payload issue on HTTP upload
Bug fix and improved `payload` handling in `http_client_req`. Changes to `http_client_req` behaviour: If the user provides `payload_len` it is used to generate the `Content-Length` header. This is done even if `payload_cb` is used to provide the actual data. If no `payload_len` is specified then no `Content-Length` is generated. If `payload_cb` is provided it is called to send the payload data. Otherwise `payload` is used as the payload buffer and sent. If `payload_len` is not zero, it is used as the size of `payload`. Otherwise `payload` is assumed to be a string and `strlen` is used to determine its size. This is to maintain current behaviour and not break existing samples. Fixes #24431 Signed-off-by: Arvin Farahmand <arvinf@ip-logix.com>
This commit is contained in:
parent
e241de8a31
commit
6d5f3debaa
2 changed files with 44 additions and 35 deletions
|
@ -238,7 +238,9 @@ struct http_request {
|
||||||
/** Payload, may be NULL */
|
/** Payload, may be NULL */
|
||||||
const char *payload;
|
const char *payload;
|
||||||
|
|
||||||
/** Payload length, may be 0. Only used if payload field is not NULL */
|
/** Payload length is used to calculate Content-Length. Set to 0
|
||||||
|
* for chunked transfers.
|
||||||
|
*/
|
||||||
size_t payload_len;
|
size_t payload_len;
|
||||||
|
|
||||||
/** User supplied callback function to call when optional headers need
|
/** User supplied callback function to call when optional headers need
|
||||||
|
|
|
@ -568,9 +568,26 @@ int http_client_req(int sock, struct http_request *req,
|
||||||
total_sent += ret;
|
total_sent += ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req->payload_cb) {
|
if (req->payload || req->payload_cb) {
|
||||||
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
if (req->payload_len) {
|
||||||
|
char content_len_str[HTTP_CONTENT_LEN_SIZE];
|
||||||
|
|
||||||
|
ret = snprintk(content_len_str, HTTP_CONTENT_LEN_SIZE,
|
||||||
|
"%zd", req->payload_len);
|
||||||
|
if (ret <= 0 || ret >= HTTP_CONTENT_LEN_SIZE) {
|
||||||
|
ret = -ENOMEM;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
||||||
|
&send_buf_pos, "Content-Length", ": ",
|
||||||
|
content_len_str, HTTP_CRLF,
|
||||||
|
HTTP_CRLF, NULL);
|
||||||
|
} else {
|
||||||
|
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
||||||
&send_buf_pos, HTTP_CRLF, NULL);
|
&send_buf_pos, HTTP_CRLF, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -585,39 +602,29 @@ int http_client_req(int sock, struct http_request *req,
|
||||||
send_buf_pos = 0;
|
send_buf_pos = 0;
|
||||||
total_sent += ret;
|
total_sent += ret;
|
||||||
|
|
||||||
ret = req->payload_cb(sock, req, user_data);
|
if (req->payload_cb) {
|
||||||
if (ret < 0) {
|
ret = req->payload_cb(sock, req, user_data);
|
||||||
goto out;
|
if (ret < 0) {
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
total_sent += ret;
|
||||||
|
} else {
|
||||||
|
u32_t length;
|
||||||
|
|
||||||
|
if (req->payload_len == 0) {
|
||||||
|
length = strlen(req->payload);
|
||||||
|
} else {
|
||||||
|
length = req->payload_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = sendall(sock, req->payload, length);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
total_sent += length;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_sent += ret;
|
|
||||||
} else if (req->payload) {
|
|
||||||
char content_len_str[HTTP_CONTENT_LEN_SIZE];
|
|
||||||
|
|
||||||
ret = snprintk(content_len_str, HTTP_CONTENT_LEN_SIZE,
|
|
||||||
"%zd", req->payload_len);
|
|
||||||
if (ret <= 0 || ret >= HTTP_CONTENT_LEN_SIZE) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
|
||||||
&send_buf_pos, "Content-Length", ": ",
|
|
||||||
content_len_str, HTTP_CRLF,
|
|
||||||
HTTP_CRLF, NULL);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
total_sent += ret;
|
|
||||||
|
|
||||||
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
|
||||||
&send_buf_pos, req->payload, NULL);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
total_sent += ret;
|
|
||||||
} else {
|
} else {
|
||||||
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
ret = http_send_data(sock, send_buf, send_buf_max_len,
|
||||||
&send_buf_pos, HTTP_CRLF, NULL);
|
&send_buf_pos, HTTP_CRLF, NULL);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue