From ce3e1e7e29354fb391411cdf0f9a8c9f7071fd88 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Mon, 14 Mar 2022 21:27:39 +0800 Subject: [PATCH] net: http: Calculate body_frag_len Calculate the body_frag_length if any part of the body is found in the http response. Signed-off-by: Yong Cong Sin --- include/net/http_client.h | 31 +++++++++++++++++++++++++++++++ subsys/net/lib/http/http_client.c | 5 +++++ 2 files changed, 36 insertions(+) diff --git a/include/net/http_client.h b/include/net/http_client.h index 504fe2b3b70..2c9353ff831 100644 --- a/include/net/http_client.h +++ b/include/net/http_client.h @@ -106,9 +106,40 @@ struct http_response { */ http_response_cb_t cb; + /** + * recv_buffer that contains header + body + * _______________________________________ + * + * |←-------- body_frag_len ---------→| + * |←--------------------- data len --------------------→| + * --------------------------------------------------------------- + * ..header | header | body | body.. + * --------------------------------------------------------------- + * ↑ ↑ + * recv_buf body_frag_start + * + * + * recv_buffer that contains body only + * ___________________________________ + * + * |←------------------ body_frag_len ------------------→| + * |←--------------------- data len --------------------→| + * --------------------------------------------------------------- + * ..header/body | body | body.. + * --------------------------------------------------------------- + * ↑ + * recv_buf + * body_frag_start + * + * body_frag_start >= recv_buf + * body_frag_len = data_len - (body_frag_start - recv_buf) + */ /** Start address of the body fragment contained in the recv_buf */ uint8_t *body_frag_start; + /** Length of the body fragment contained in the recv_buf */ + size_t body_frag_len; + /** Where the response is stored, this is to be * provided by the user. */ diff --git a/subsys/net/lib/http/http_client.c b/subsys/net/lib/http/http_client.c index d42a3badd88..35f0415c199 100644 --- a/subsys/net/lib/http/http_client.c +++ b/subsys/net/lib/http/http_client.c @@ -269,6 +269,10 @@ static int on_body(struct http_parser *parser, const char *at, size_t length) req->internal.response.body_frag_start = (uint8_t *)at; } + /* Calculate the length of the body contained in the recv_buf */ + req->internal.response.body_frag_len = req->internal.response.data_len - + (req->internal.response.body_frag_start - req->internal.response.recv_buf); + return 0; } @@ -464,6 +468,7 @@ static int http_wait_data(int sock, struct http_request *req) /* Re-use the result buffer and start to fill it again */ req->internal.response.data_len = 0; req->internal.response.body_frag_start = NULL; + req->internal.response.body_frag_len = 0; } }