netutils/webclient: Notify HTTP header data via dedicated callback

Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
This commit is contained in:
Gustavo Henrique Nihei 2021-07-21 12:05:18 -03:00 committed by Xiang Xiao
parent 496aa3ef4f
commit a1026c9f23
2 changed files with 33 additions and 3 deletions

View File

@ -47,6 +47,8 @@
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <sys/types.h>
/****************************************************************************
@ -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;

View File

@ -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;