From a1026c9f23450abeb4611a23f7c7c09370054b55 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Nihei Date: Wed, 21 Jul 2021 12:05:18 -0300 Subject: [PATCH] netutils/webclient: Notify HTTP header data via dedicated callback Signed-off-by: Gustavo Henrique Nihei --- include/netutils/webclient.h | 20 +++++++++++++++++++- netutils/webclient/webclient.c | 16 ++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/netutils/webclient.h b/include/netutils/webclient.h index 77b7e19e8..5f5c8ceff 100644 --- a/include/netutils/webclient.h +++ b/include/netutils/webclient.h @@ -47,6 +47,8 @@ ****************************************************************************/ #include + +#include #include /**************************************************************************** @@ -103,7 +105,7 @@ typedef void (*wget_callback_t)(FAR char **buffer, int offset, int datend, FAR int *buflen, FAR void *arg); -/* webclient_sink_callback_t: callback to consume data +/* webclient_sink_callback_t: callback to consume body data * * Same as wget_callback_t, but allowed to fail. * @@ -119,6 +121,20 @@ typedef CODE int (*webclient_sink_callback_t)(FAR char **buffer, int offset, int datend, FAR int *buflen, FAR void *arg); +/* webclient_header_callback_t: callback to consume header data + * + * Input Parameters: + * line - A NULL-terminated string containing a header line. + * truncated - Flag for indicating whether the received header line is + * truncated for exceeding the CONFIG_WEBCLIENT_MAXHTTPLINE + * length limit. + * arg - User argument passed to callback. + */ + +typedef CODE int (*webclient_header_callback_t)(FAR const char *line, + bool truncated, + FAR void *arg); + /* webclient_body_callback_t: a callback to provide request body * * This callback can be called multiple times to provide @@ -239,6 +255,8 @@ struct webclient_context wget_callback_t callback; webclient_sink_callback_t sink_callback; FAR void *sink_callback_arg; + webclient_header_callback_t header_callback; + FAR void *header_callback_arg; webclient_body_callback_t body_callback; FAR void *body_callback_arg; FAR const struct webclient_tls_ops *tls_ops; diff --git a/netutils/webclient/webclient.c b/netutils/webclient/webclient.c index 8a64b2616..19d66ee35 100644 --- a/netutils/webclient/webclient.c +++ b/netutils/webclient/webclient.c @@ -519,7 +519,8 @@ static int parseurl(FAR const char *url, FAR struct wget_s *ws) * Name: wget_parseheaders ****************************************************************************/ -static inline int wget_parseheaders(struct wget_s *ws) +static inline int wget_parseheaders(struct webclient_context *ctx, + struct wget_s *ws) { int offset; int ndx; @@ -559,6 +560,7 @@ static inline int wget_parseheaders(struct wget_s *ws) ninfo("Got HTTP header line%s: %.*s\n", got_nl ? "" : " (truncated)", ndx - 1, &ws->line[0]); + if (ws->line[0] == ISO_CR) { /* This was the last header line (i.e., and empty "\r\n"), @@ -597,6 +599,16 @@ static inline int wget_parseheaders(struct wget_s *ws) return -EPROTO; } + if (ctx->header_callback) + { + ret = ctx->header_callback(&ws->line[0], !got_nl, + ctx->header_callback_arg); + if (ret != 0) + { + goto exit; + } + } + /* Check for specific HTTP header fields. */ #ifdef CONFIG_WEBCLIENT_GETMIMETYPE @@ -1046,7 +1058,7 @@ int webclient_perform(FAR struct webclient_context *ctx) if (ws->state == WEBCLIENT_STATE_HEADERS) { - ret = wget_parseheaders(ws); + ret = wget_parseheaders(ctx, ws); if (ret < 0) { goto errout_with_errno;