Skip to content

Commit 3572c9f

Browse files
rluboskartben
authored andcommitted
net: http: client: Allow to abort download from response callback
Update the response callback function signature to allow the callback to return an error code, which in turn will cause the HTTP client to abort the download. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent 73e248f commit 3572c9f

File tree

7 files changed

+45
-23
lines changed

7 files changed

+45
-23
lines changed

doc/connectivity/networking/api/http_client.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ The following is an example of a very simple response handling function:
5656

5757
.. code-block:: c
5858
59-
static void response_cb(struct http_response *rsp,
60-
enum http_final_call final_data,
61-
void *user_data)
59+
static int response_cb(struct http_response *rsp,
60+
enum http_final_call final_data,
61+
void *user_data)
6262
{
6363
if (final_data == HTTP_DATA_MORE) {
6464
LOG_INF("Partial data received (%zd bytes)", rsp->data_len);
@@ -67,6 +67,8 @@ The following is an example of a very simple response handling function:
6767
}
6868
6969
LOG_INF("Response status %s", rsp->http_status);
70+
71+
return 0;
7072
}
7173
7274
See :zephyr:code-sample:`HTTP client sample application <sockets-http-client>` for

include/zephyr/net/http/client.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ typedef int (*http_header_cb_t)(int sock,
9494
* @param final_data Does this data buffer contain all the data or
9595
* is there still more data to come.
9696
* @param user_data User specified data specified in http_client_req()
97+
*
98+
* @return 0 if http_client_req() should proceed with the download,
99+
* <0 if http_client_req() should abort the download.
97100
*/
98-
typedef void (*http_response_cb_t)(struct http_response *rsp,
99-
enum http_final_call final_data,
100-
void *user_data);
101+
typedef int (*http_response_cb_t)(struct http_response *rsp,
102+
enum http_final_call final_data,
103+
void *user_data);
101104

102105
/**
103106
* HTTP response from the server.

samples/net/cloud/tagoio_http_post/src/main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ LOG_MODULE_REGISTER(tagoio_http_post, CONFIG_TAGOIO_HTTP_POST_LOG_LEVEL);
1818

1919
static struct tagoio_context ctx;
2020

21-
static void response_cb(struct http_response *rsp,
22-
enum http_final_call final_data,
23-
void *user_data)
21+
static int response_cb(struct http_response *rsp,
22+
enum http_final_call final_data,
23+
void *user_data)
2424
{
2525
if (final_data == HTTP_DATA_MORE) {
2626
LOG_DBG("Partial data received (%zd bytes)", rsp->data_len);
@@ -29,6 +29,8 @@ static void response_cb(struct http_response *rsp,
2929
}
3030

3131
LOG_DBG("Response status %s", rsp->http_status);
32+
33+
return 0;
3234
}
3335

3436
static int collect_data(void)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ static int payload_cb(int sock, struct http_request *req, void *user_data)
112112
return pos;
113113
}
114114

115-
static void response_cb(struct http_response *rsp,
116-
enum http_final_call final_data,
117-
void *user_data)
115+
static int response_cb(struct http_response *rsp,
116+
enum http_final_call final_data,
117+
void *user_data)
118118
{
119119
if (final_data == HTTP_DATA_MORE) {
120120
LOG_INF("Partial data received (%zd bytes)", rsp->data_len);
@@ -124,6 +124,8 @@ static void response_cb(struct http_response *rsp,
124124

125125
LOG_INF("Response to %s", (const char *)user_data);
126126
LOG_INF("Response status %s", rsp->http_status);
127+
128+
return 0;
127129
}
128130

129131
static int connect_socket(sa_family_t family, const char *server, int port,

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ static void response_download_cb(struct http_response *rsp, enum http_final_call
996996
}
997997
}
998998

999-
static void response_cb(struct http_response *rsp, enum http_final_call final_data, void *userdata)
999+
static int response_cb(struct http_response *rsp, enum http_final_call final_data, void *userdata)
10001000
{
10011001
struct hawkbit_context *hb_context = userdata;
10021002

@@ -1007,7 +1007,7 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
10071007
} else {
10081008
hb_context->code_status = HAWKBIT_METADATA_ERROR;
10091009
}
1010-
return;
1010+
return 0;
10111011
}
10121012

10131013
switch (hb_context->type) {
@@ -1023,6 +1023,8 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
10231023
default:
10241024
break;
10251025
}
1026+
1027+
return 0;
10261028
}
10271029

10281030
static bool send_request(struct hawkbit_context *hb_context, enum hawkbit_http_request type,

subsys/net/lib/http/http_client.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,21 +460,25 @@ static void http_report_complete(struct http_request *req)
460460
{
461461
if (req->internal.response.cb) {
462462
NET_DBG("Calling callback for %zd len data", req->internal.response.data_len);
463-
req->internal.response.cb(&req->internal.response, HTTP_DATA_FINAL,
464-
req->internal.user_data);
463+
(void)req->internal.response.cb(&req->internal.response,
464+
HTTP_DATA_FINAL,
465+
req->internal.user_data);
465466
}
466467
}
467468

468469
/* Report that some data has been received, but the HTTP transaction is still ongoing. */
469-
static void http_report_progress(struct http_request *req)
470+
static int http_report_progress(struct http_request *req)
470471
{
471472
if (req->internal.response.cb) {
472473
NET_DBG("Calling callback for partitioned %zd len data",
473474
req->internal.response.data_len);
474475

475-
req->internal.response.cb(&req->internal.response, HTTP_DATA_MORE,
476-
req->internal.user_data);
476+
return req->internal.response.cb(&req->internal.response,
477+
HTTP_DATA_MORE,
478+
req->internal.user_data);
477479
}
480+
481+
return 0;
478482
}
479483

480484
static int http_wait_data(int sock, struct http_request *req, const k_timepoint_t req_end_timepoint)
@@ -568,7 +572,12 @@ static int http_wait_data(int sock, struct http_request *req, const k_timepoint_
568572
if (req->internal.response.message_complete) {
569573
http_report_complete(req);
570574
} else {
571-
http_report_progress(req);
575+
ret = http_report_progress(req);
576+
if (ret < 0) {
577+
LOG_DBG("Connection aborted by the application (%d)",
578+
ret);
579+
return -ECONNABORTED;
580+
}
572581

573582
/* Re-use the result buffer and start to fill it again */
574583
req->internal.response.data_len = 0;

subsys/net/lib/websocket/websocket.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ static struct websocket_context *websocket_find(int real_sock)
149149
return ctx;
150150
}
151151

152-
static void response_cb(struct http_response *rsp,
153-
enum http_final_call final_data,
154-
void *user_data)
152+
static int response_cb(struct http_response *rsp,
153+
enum http_final_call final_data,
154+
void *user_data)
155155
{
156156
struct websocket_context *ctx = user_data;
157157

@@ -164,6 +164,8 @@ static void response_cb(struct http_response *rsp,
164164
rsp->data_len);
165165
ctx->all_received = true;
166166
}
167+
168+
return 0;
167169
}
168170

169171
static int on_header_field(struct http_parser *parser, const char *at,

0 commit comments

Comments
 (0)