Skip to content

Commit 64aebb6

Browse files
committed
samples: net: http_client: Avoid parallel IPv4/IPv6 sessions
Apparently the simple python HTTPS server the sample is interfacing, cannot handle parallel TLS sessions (just one at a time), hence establishing both IPv4/6 connections before sending request doesn't work well, half of the requests are dropped. Therefore, modify the sample a little to run only one TLS (or TCP if no TLS is used) connection at a time. Additionally, add a log in case HTTP client request fails, as it could easily be overlooked if something went wrong. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent be19021 commit 64aebb6

File tree

1 file changed

+60
-54
lines changed
  • samples/net/sockets/http_client/src

1 file changed

+60
-54
lines changed

samples/net/sockets/http_client/src/main.c

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static int run_queries(void)
159159
int32_t timeout = 3 * MSEC_PER_SEC;
160160
int ret = 0;
161161
int port = HTTP_PORT;
162+
struct http_request req;
162163

163164
if (IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS)) {
164165
ret = tls_credential_add(CA_CERTIFICATE_TAG,
@@ -178,21 +179,10 @@ static int run_queries(void)
178179
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
179180
&sock4, (struct sockaddr *)&addr4,
180181
sizeof(addr4));
181-
}
182-
183-
if (IS_ENABLED(CONFIG_NET_IPV6)) {
184-
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
185-
&sock6, (struct sockaddr *)&addr6,
186-
sizeof(addr6));
187-
}
188-
189-
if (sock4 < 0 && sock6 < 0) {
190-
LOG_ERR("Cannot create HTTP connection.");
191-
return -ECONNABORTED;
192-
}
193-
194-
if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
195-
struct http_request req;
182+
if (sock4 < 0) {
183+
LOG_ERR("Cannot create HTTP IPv4 connection.");
184+
return -ECONNABORTED;
185+
}
196186

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

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

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

209202
close(sock4);
210203
}
211204

212-
if (sock6 >= 0 && IS_ENABLED(CONFIG_NET_IPV6)) {
213-
struct http_request req;
205+
if (IS_ENABLED(CONFIG_NET_IPV6)) {
206+
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
207+
&sock6, (struct sockaddr *)&addr6,
208+
sizeof(addr6));
209+
if (sock6 < 0) {
210+
LOG_ERR("Cannot create HTTP IPv6 connection.");
211+
return -ECONNABORTED;
212+
}
214213

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

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

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

227229
close(sock6);
228230
}
@@ -234,21 +236,10 @@ static int run_queries(void)
234236
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
235237
&sock4, (struct sockaddr *)&addr4,
236238
sizeof(addr4));
237-
}
238-
239-
if (IS_ENABLED(CONFIG_NET_IPV6)) {
240-
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
241-
&sock6, (struct sockaddr *)&addr6,
242-
sizeof(addr6));
243-
}
244-
245-
if (sock4 < 0 && sock6 < 0) {
246-
LOG_ERR("Cannot create HTTP connection.");
247-
return -ECONNABORTED;
248-
}
249-
250-
if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
251-
struct http_request req;
239+
if (sock4 < 0) {
240+
LOG_ERR("Cannot create HTTP IPv4 connection.");
241+
return -ECONNABORTED;
242+
}
252243

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

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

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

267261
close(sock4);
268262
}
269263

270-
if (sock6 >= 0 && IS_ENABLED(CONFIG_NET_IPV6)) {
271-
struct http_request req;
264+
if (IS_ENABLED(CONFIG_NET_IPV6)) {
265+
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
266+
&sock6, (struct sockaddr *)&addr6,
267+
sizeof(addr6));
268+
if (sock6 < 0) {
269+
LOG_ERR("Cannot create HTTP IPv6 connection.");
270+
return -ECONNABORTED;
271+
}
272272

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

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

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

287290
close(sock6);
288291
}
@@ -293,29 +296,19 @@ static int run_queries(void)
293296
sock6 = -1;
294297

295298
if (IS_ENABLED(CONFIG_NET_IPV4)) {
296-
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
297-
&sock4, (struct sockaddr *)&addr4,
298-
sizeof(addr4));
299-
}
300-
301-
if (IS_ENABLED(CONFIG_NET_IPV6)) {
302-
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
303-
&sock6, (struct sockaddr *)&addr6,
304-
sizeof(addr6));
305-
}
306-
307-
if (sock4 < 0 && sock6 < 0) {
308-
LOG_ERR("Cannot create HTTP connection.");
309-
return -ECONNABORTED;
310-
}
311-
312-
if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
313-
struct http_request req;
314299
const char *headers[] = {
315300
"Transfer-Encoding: chunked\r\n",
316301
NULL
317302
};
318303

304+
(void)connect_socket(AF_INET, SERVER_ADDR4, port,
305+
&sock4, (struct sockaddr *)&addr4,
306+
sizeof(addr4));
307+
if (sock4 < 0) {
308+
LOG_ERR("Cannot create HTTP IPv4 connection.");
309+
return -ECONNABORTED;
310+
}
311+
319312
memset(&req, 0, sizeof(req));
320313

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

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

333329
close(sock4);
334330
}
335331

336-
if (sock6 >= 0 && IS_ENABLED(CONFIG_NET_IPV6)) {
337-
struct http_request req;
332+
if (IS_ENABLED(CONFIG_NET_IPV6)) {
338333
const char *headers[] = {
339334
"Transfer-Encoding: chunked\r\n",
340335
NULL
341336
};
342337

338+
(void)connect_socket(AF_INET6, SERVER_ADDR6, port,
339+
&sock6, (struct sockaddr *)&addr6,
340+
sizeof(addr6));
341+
if (sock6 < 0) {
342+
LOG_ERR("Cannot create HTTP IPv6 connection.");
343+
return -ECONNABORTED;
344+
}
345+
343346
memset(&req, 0, sizeof(req));
344347

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

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

357363
close(sock6);
358364
}

0 commit comments

Comments
 (0)