Skip to content

samples: net: http_client: Update the sample to work out-of-the-box with the net-tools scripts #92640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions samples/net/sockets/http_client/overlay-tls.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=60000
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED=y
CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED=y
CONFIG_MBEDTLS_ECDH_C=y
CONFIG_MBEDTLS_ECP_C=y

CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
114 changes: 60 additions & 54 deletions samples/net/sockets/http_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ static int run_queries(void)
int32_t timeout = 3 * MSEC_PER_SEC;
int ret = 0;
int port = HTTP_PORT;
struct http_request req;

if (IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS)) {
ret = tls_credential_add(CA_CERTIFICATE_TAG,
Expand All @@ -178,21 +179,10 @@ static int run_queries(void)
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
&sock4, (struct sockaddr *)&addr4,
sizeof(addr4));
}

if (IS_ENABLED(CONFIG_NET_IPV6)) {
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
&sock6, (struct sockaddr *)&addr6,
sizeof(addr6));
}

if (sock4 < 0 && sock6 < 0) {
LOG_ERR("Cannot create HTTP connection.");
return -ECONNABORTED;
}

if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
struct http_request req;
if (sock4 < 0) {
LOG_ERR("Cannot create HTTP IPv4 connection.");
return -ECONNABORTED;
}

memset(&req, 0, sizeof(req));

Expand All @@ -205,12 +195,21 @@ static int run_queries(void)
req.recv_buf_len = sizeof(recv_buf_ipv4);

ret = http_client_req(sock4, &req, timeout, "IPv4 GET");
if (ret < 0) {
LOG_ERR("Client error %d", ret);
}

close(sock4);
}

if (sock6 >= 0 && IS_ENABLED(CONFIG_NET_IPV6)) {
struct http_request req;
if (IS_ENABLED(CONFIG_NET_IPV6)) {
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
&sock6, (struct sockaddr *)&addr6,
sizeof(addr6));
if (sock6 < 0) {
LOG_ERR("Cannot create HTTP IPv6 connection.");
return -ECONNABORTED;
}

memset(&req, 0, sizeof(req));

Expand All @@ -223,6 +222,9 @@ static int run_queries(void)
req.recv_buf_len = sizeof(recv_buf_ipv6);

ret = http_client_req(sock6, &req, timeout, "IPv6 GET");
if (ret < 0) {
LOG_ERR("Client error %d", ret);
}

close(sock6);
}
Expand All @@ -234,21 +236,10 @@ static int run_queries(void)
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
&sock4, (struct sockaddr *)&addr4,
sizeof(addr4));
}

if (IS_ENABLED(CONFIG_NET_IPV6)) {
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
&sock6, (struct sockaddr *)&addr6,
sizeof(addr6));
}

if (sock4 < 0 && sock6 < 0) {
LOG_ERR("Cannot create HTTP connection.");
return -ECONNABORTED;
}

if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
struct http_request req;
if (sock4 < 0) {
LOG_ERR("Cannot create HTTP IPv4 connection.");
return -ECONNABORTED;
}

memset(&req, 0, sizeof(req));

Expand All @@ -263,12 +254,21 @@ static int run_queries(void)
req.recv_buf_len = sizeof(recv_buf_ipv4);

ret = http_client_req(sock4, &req, timeout, "IPv4 POST");
if (ret < 0) {
LOG_ERR("Client error %d", ret);
}

close(sock4);
}

if (sock6 >= 0 && IS_ENABLED(CONFIG_NET_IPV6)) {
struct http_request req;
if (IS_ENABLED(CONFIG_NET_IPV6)) {
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
&sock6, (struct sockaddr *)&addr6,
sizeof(addr6));
if (sock6 < 0) {
LOG_ERR("Cannot create HTTP IPv6 connection.");
return -ECONNABORTED;
}

memset(&req, 0, sizeof(req));

Expand All @@ -283,6 +283,9 @@ static int run_queries(void)
req.recv_buf_len = sizeof(recv_buf_ipv6);

ret = http_client_req(sock6, &req, timeout, "IPv6 POST");
if (ret < 0) {
LOG_ERR("Client error %d", ret);
}

close(sock6);
}
Expand All @@ -293,29 +296,19 @@ static int run_queries(void)
sock6 = -1;

if (IS_ENABLED(CONFIG_NET_IPV4)) {
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
&sock4, (struct sockaddr *)&addr4,
sizeof(addr4));
}

if (IS_ENABLED(CONFIG_NET_IPV6)) {
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
&sock6, (struct sockaddr *)&addr6,
sizeof(addr6));
}

if (sock4 < 0 && sock6 < 0) {
LOG_ERR("Cannot create HTTP connection.");
return -ECONNABORTED;
}

if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
struct http_request req;
const char *headers[] = {
"Transfer-Encoding: chunked\r\n",
NULL
};

(void)connect_socket(AF_INET, SERVER_ADDR4, port,
&sock4, (struct sockaddr *)&addr4,
sizeof(addr4));
if (sock4 < 0) {
LOG_ERR("Cannot create HTTP IPv4 connection.");
return -ECONNABORTED;
}

memset(&req, 0, sizeof(req));

req.method = HTTP_POST;
Expand All @@ -329,17 +322,27 @@ static int run_queries(void)
req.recv_buf_len = sizeof(recv_buf_ipv4);

ret = http_client_req(sock4, &req, timeout, "IPv4 POST");
if (ret < 0) {
LOG_ERR("Client error %d", ret);
}

close(sock4);
}

if (sock6 >= 0 && IS_ENABLED(CONFIG_NET_IPV6)) {
struct http_request req;
if (IS_ENABLED(CONFIG_NET_IPV6)) {
const char *headers[] = {
"Transfer-Encoding: chunked\r\n",
NULL
};

(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
&sock6, (struct sockaddr *)&addr6,
sizeof(addr6));
if (sock6 < 0) {
LOG_ERR("Cannot create HTTP IPv6 connection.");
return -ECONNABORTED;
}

memset(&req, 0, sizeof(req));

req.method = HTTP_POST;
Expand All @@ -353,6 +356,9 @@ static int run_queries(void)
req.recv_buf_len = sizeof(recv_buf_ipv6);

ret = http_client_req(sock6, &req, timeout, "IPv6 POST");
if (ret < 0) {
LOG_ERR("Client error %d", ret);
}

close(sock6);
}
Expand Down
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ manifest:
- debug
revision: 33e5c23cbedda5ba12dbe50c4baefb362a791001
- name: net-tools
revision: 986bfeb040df3d9029366de8aea4ce1f84e93780
revision: 64bf49ad9b6d1d1f9d24bf8b94d82d9bcb52f61a
path: tools/net-tools
groups:
- tools
Expand Down