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 <yongcong.sin@gmail.com>
This commit is contained in:
Yong Cong Sin 2022-03-14 21:27:39 +08:00 committed by Carles Cufí
commit ce3e1e7e29
2 changed files with 36 additions and 0 deletions

View file

@ -106,9 +106,40 @@ struct http_response {
*/ */
http_response_cb_t cb; 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 */ /** Start address of the body fragment contained in the recv_buf */
uint8_t *body_frag_start; 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 /** Where the response is stored, this is to be
* provided by the user. * provided by the user.
*/ */

View file

@ -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; 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; 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 */ /* Re-use the result buffer and start to fill it again */
req->internal.response.data_len = 0; req->internal.response.data_len = 0;
req->internal.response.body_frag_start = NULL; req->internal.response.body_frag_start = NULL;
req->internal.response.body_frag_len = 0;
} }
} }