2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-04-07 07:39:40 +02:00
|
|
|
* apps/netutils/webclient/webclient.c
|
2011-03-19 22:04:13 +01:00
|
|
|
* Implementation of the HTTP client.
|
|
|
|
*
|
2020-04-22 13:36:07 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2011-2012, 2014, 2020 Gregory Nutt.
|
|
|
|
* All rights reserved.
|
2012-02-18 19:13:30 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Based on uIP which also has a BSD style license:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@dunkels.com>
|
|
|
|
* Copyright (c) 2002, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This example shows a HTTP client that is able to download web pages
|
|
|
|
* and files from web servers. It requires a number of callback
|
|
|
|
* functions to be implemented by the module that utilizes the code:
|
|
|
|
* webclient_datahandler().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
#include <debug.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <sys/socket.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdint.h>
|
2012-11-04 19:54:04 +01:00
|
|
|
#include <stdio.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
2015-07-10 22:03:28 +02:00
|
|
|
#include <netdb.h>
|
2020-05-28 08:31:46 +02:00
|
|
|
#include <string.h>
|
2017-02-16 22:58:15 +01:00
|
|
|
#include <strings.h>
|
2020-04-22 10:29:02 +02:00
|
|
|
#include <stdlib.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <errno.h>
|
2021-08-11 06:10:39 +02:00
|
|
|
#include <inttypes.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2021-01-14 04:22:47 +01:00
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
#include <nuttx/version.h>
|
2015-07-10 22:03:28 +02:00
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
|
|
|
#include "netutils/webclient.h"
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
#if defined(CONFIG_NETUTILS_CODECS)
|
|
|
|
# if defined(CONFIG_CODECS_URLCODE)
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/urldecode.h"
|
2012-11-03 01:00:56 +01:00
|
|
|
# endif
|
|
|
|
# if defined(CONFIG_CODECS_BASE64)
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/base64.h"
|
2012-11-03 01:00:56 +01:00
|
|
|
# endif
|
2012-11-06 17:59:45 +01:00
|
|
|
#else
|
|
|
|
# undef CONFIG_CODECS_URLCODE
|
|
|
|
# undef CONFIG_CODECS_BASE64
|
2012-11-03 01:00:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_WGET_USERAGENT
|
|
|
|
# if CONFIG_VERSION_MAJOR != 0 || CONFIG_VERSION_MINOR != 0
|
|
|
|
# define CONFIG_NSH_WGET_USERAGENT \
|
|
|
|
"NuttX/" CONFIG_VERSION_STRING " (; http://www.nuttx.org/)"
|
|
|
|
# else
|
|
|
|
# define CONFIG_NSH_WGET_USERAGENT \
|
|
|
|
"NuttX/6.xx.x (; http://www.nuttx.org/)"
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-30 01:10:49 +02:00
|
|
|
#ifndef CONFIG_WEBCLIENT_TIMEOUT
|
|
|
|
# define CONFIG_WEBCLIENT_TIMEOUT 10
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-05-25 16:21:07 +02:00
|
|
|
#ifndef CONFIG_WEBCLIENT_MAX_REDIRECT
|
|
|
|
/* The default value 50 is taken from curl's --max-redirs option. */
|
|
|
|
# define CONFIG_WEBCLIENT_MAX_REDIRECT 50
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#define HTTPSTATUS_NONE 0
|
|
|
|
#define HTTPSTATUS_OK 1
|
|
|
|
#define HTTPSTATUS_MOVED 2
|
|
|
|
#define HTTPSTATUS_ERROR 3
|
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
#define ISO_NL 0x0a
|
|
|
|
#define ISO_CR 0x0d
|
|
|
|
#define ISO_SPACE 0x20
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
#define WGET_MODE_GET 0
|
|
|
|
#define WGET_MODE_POST 1
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* The following CONN_ flags are for conn::flags.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CONN_WANT_READ WEBCLIENT_POLL_INFO_WANT_READ
|
|
|
|
#define CONN_WANT_WRITE WEBCLIENT_POLL_INFO_WANT_WRITE
|
|
|
|
|
2021-05-14 02:44:06 +02:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
|
|
|
#define _CHECK_STATE(ctx, s) DEBUGASSERT((ctx)->state == (s))
|
|
|
|
#define _SET_STATE(ctx, s) ctx->state = (s)
|
|
|
|
#else
|
|
|
|
#define _CHECK_STATE(ctx, s) do {} while (0)
|
|
|
|
#define _SET_STATE(ctx, s) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
enum webclient_state_e
|
|
|
|
{
|
|
|
|
WEBCLIENT_STATE_SOCKET,
|
|
|
|
WEBCLIENT_STATE_CONNECT,
|
|
|
|
WEBCLIENT_STATE_PREPARE_REQUEST,
|
|
|
|
WEBCLIENT_STATE_SEND_REQUEST,
|
|
|
|
WEBCLIENT_STATE_SEND_REQUEST_BODY,
|
|
|
|
WEBCLIENT_STATE_STATUSLINE,
|
|
|
|
WEBCLIENT_STATE_HEADERS,
|
|
|
|
WEBCLIENT_STATE_DATA,
|
2022-03-07 04:48:59 +01:00
|
|
|
WEBCLIENT_STATE_CHUNKED_HEADER,
|
|
|
|
WEBCLIENT_STATE_CHUNKED_DATA,
|
|
|
|
WEBCLIENT_STATE_CHUNKED_ENDDATA,
|
|
|
|
WEBCLIENT_STATE_CHUNKED_TRAILER,
|
|
|
|
WEBCLIENT_STATE_WAIT_CLOSE,
|
2021-02-17 09:17:42 +01:00
|
|
|
WEBCLIENT_STATE_CLOSE,
|
|
|
|
WEBCLIENT_STATE_DONE,
|
|
|
|
};
|
|
|
|
|
2021-08-10 04:30:32 +02:00
|
|
|
struct conn_s
|
2021-02-17 09:17:42 +01:00
|
|
|
{
|
|
|
|
bool tls;
|
|
|
|
|
|
|
|
/* for !tls */
|
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
unsigned int flags;
|
|
|
|
|
|
|
|
/* for tls */
|
|
|
|
|
|
|
|
struct webclient_tls_connection *tls_conn;
|
|
|
|
};
|
|
|
|
|
2021-08-11 06:10:39 +02:00
|
|
|
/* flags for wget_s::internal_flags */
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
#define WGET_FLAG_GOT_CONTENT_LENGTH 1U
|
|
|
|
#define WGET_FLAG_CHUNKED 2U
|
2021-08-11 06:10:39 +02:00
|
|
|
|
2022-05-16 04:40:04 +02:00
|
|
|
struct wget_target_s
|
|
|
|
{
|
|
|
|
char scheme[sizeof("https") + 1];
|
|
|
|
char hostname[CONFIG_WEBCLIENT_MAXHOSTNAME];
|
|
|
|
char filename[CONFIG_WEBCLIENT_MAXFILENAME];
|
|
|
|
uint16_t port; /* The port number to use in the connection */
|
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
struct wget_s
|
|
|
|
{
|
|
|
|
/* Internal status */
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
enum webclient_state_e state;
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t httpstatus;
|
|
|
|
|
|
|
|
/* These describe the just-received buffer of data */
|
|
|
|
|
|
|
|
FAR char *buffer; /* user-provided buffer */
|
|
|
|
int buflen; /* Length of the user provided buffer */
|
|
|
|
int offset; /* Offset to the beginning of interesting data */
|
2012-10-06 16:50:37 +02:00
|
|
|
int datend; /* Offset+1 to the last valid byte of data in the buffer */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Buffer HTTP header data and parse line at a time */
|
|
|
|
|
|
|
|
char line[CONFIG_WEBCLIENT_MAXHTTPLINE];
|
|
|
|
int ndx;
|
2021-05-14 04:09:32 +02:00
|
|
|
bool skip_to_next_line;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-08-11 06:10:39 +02:00
|
|
|
unsigned int internal_flags; /* OR'ed WGET_FLAG_xxx */
|
|
|
|
uintmax_t expected_resp_body_len;
|
|
|
|
uintmax_t received_body_len;
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
uintmax_t chunk_len;
|
|
|
|
uintmax_t chunk_received;
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#ifdef CONFIG_WEBCLIENT_GETMIMETYPE
|
|
|
|
char mimetype[CONFIG_WEBCLIENT_MAXMIMESIZE];
|
|
|
|
#endif
|
2022-05-16 04:40:04 +02:00
|
|
|
|
|
|
|
struct wget_target_s target;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
bool need_conn_close;
|
2021-08-10 04:30:32 +02:00
|
|
|
struct conn_s conn;
|
2021-02-17 09:17:42 +01:00
|
|
|
unsigned int nredirect;
|
|
|
|
int redirected;
|
|
|
|
|
|
|
|
/* progress and todo for the current state (WEBCLIENT_STATE_) */
|
|
|
|
|
|
|
|
off_t state_offset;
|
|
|
|
size_t state_len;
|
|
|
|
FAR const void *data_buffer;
|
|
|
|
size_t data_len;
|
2020-07-12 01:54:28 +02:00
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
static const char g_http10[] = "HTTP/1.0";
|
|
|
|
static const char g_http11[] = "HTTP/1.1";
|
2011-03-19 22:04:13 +01:00
|
|
|
#ifdef CONFIG_WEBCLIENT_GETMIMETYPE
|
2022-03-07 04:48:59 +01:00
|
|
|
static const char g_httpcontenttype[] = "content-type: ";
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
2022-03-07 04:48:59 +01:00
|
|
|
static const char g_httphost[] = "host: ";
|
|
|
|
static const char g_httplocation[] = "location: ";
|
|
|
|
static const char g_httptransferencoding[] = "transfer-encoding: ";
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
static const char g_httpuseragentfields[] =
|
2012-11-03 01:00:56 +01:00
|
|
|
"User-Agent: "
|
|
|
|
CONFIG_NSH_WGET_USERAGENT
|
|
|
|
"\r\n\r\n";
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-03-07 01:30:23 +01:00
|
|
|
static const char g_httpcrnl[] = "\r\n";
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-03-07 01:30:23 +01:00
|
|
|
static const char g_httpform[] = "Content-Type: "
|
|
|
|
"application/x-www-form-urlencoded";
|
|
|
|
static const char g_httpcontsize[] = "Content-Length: ";
|
|
|
|
static const char g_httpconn_close[] = "Connection: close";
|
2020-04-22 13:36:07 +02:00
|
|
|
#if 0
|
2022-03-07 01:30:23 +01:00
|
|
|
static const char g_httpconn[] = "Connection: Keep-Alive";
|
|
|
|
static const char g_httpcache[] = "Cache-Control: no-cache";
|
2020-04-22 13:36:07 +02:00
|
|
|
#endif
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: conn_send
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-08-10 04:30:32 +02:00
|
|
|
static ssize_t conn_send(struct webclient_context *ctx, struct conn_s *conn,
|
2020-07-12 01:54:28 +02:00
|
|
|
const void *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
return ctx->tls_ops->send(ctx->tls_ctx, conn->tls_conn, buffer, len);
|
|
|
|
}
|
2021-09-14 06:47:27 +02:00
|
|
|
|
|
|
|
while (true)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
|
|
|
ssize_t ret = send(conn->sockfd, buffer, len, 0);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
2021-09-14 06:47:27 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
conn->flags |= CONN_WANT_WRITE;
|
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: conn_recv
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-08-10 04:30:32 +02:00
|
|
|
static ssize_t conn_recv(struct webclient_context *ctx, struct conn_s *conn,
|
2020-07-12 01:54:28 +02:00
|
|
|
void *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
return ctx->tls_ops->recv(ctx->tls_ctx, conn->tls_conn, buffer, len);
|
|
|
|
}
|
2021-09-14 06:47:27 +02:00
|
|
|
|
|
|
|
while (true)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
|
|
|
ssize_t ret = recv(conn->sockfd, buffer, len, 0);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
2021-09-14 06:47:27 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
conn->flags |= CONN_WANT_READ;
|
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: conn_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-08-10 04:30:32 +02:00
|
|
|
static void conn_close(struct webclient_context *ctx, struct conn_s *conn)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
ctx->tls_ops->close(ctx->tls_ctx, conn->tls_conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close(conn->sockfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: webclient_static_body_func
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int webclient_static_body_func(FAR void *buffer,
|
|
|
|
FAR size_t *sizep,
|
|
|
|
FAR const void * FAR *datap,
|
2020-11-20 01:50:11 +01:00
|
|
|
size_t reqsize,
|
2020-07-12 01:54:28 +02:00
|
|
|
FAR void *ctx)
|
|
|
|
{
|
|
|
|
*datap = ctx;
|
2020-11-20 01:50:11 +01:00
|
|
|
*sizep = reqsize;
|
2020-07-12 01:54:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: append
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static char *append(char *dest, char *ep, const char *src)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (dest == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(src);
|
|
|
|
if (ep - dest < len + 1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dest, src, len);
|
|
|
|
dest[len] = '\0';
|
|
|
|
return dest + len;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_strcpy
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
#ifdef WGET_USE_URLENCODE
|
2011-03-19 22:04:13 +01:00
|
|
|
static char *wget_strcpy(char *dest, const char *src)
|
|
|
|
{
|
|
|
|
int len = strlen(src);
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
memcpy(dest, src, len);
|
|
|
|
dest[len] = '\0';
|
|
|
|
return dest + len;
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_urlencode_strcpy
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-06 17:59:45 +01:00
|
|
|
#ifdef WGET_USE_URLENCODE
|
2012-11-03 01:00:56 +01:00
|
|
|
static char *wget_urlencode_strcpy(char *dest, const char *src)
|
|
|
|
{
|
|
|
|
int len = strlen(src);
|
|
|
|
int d_len;
|
|
|
|
|
|
|
|
d_len = urlencode_len(src, len);
|
|
|
|
urlencode(src, len, dest, &d_len);
|
|
|
|
return dest + d_len;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-11 06:10:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_parseint
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
static int wget_parseint(const char *cp, uintmax_t *resultp, int base)
|
2021-08-11 06:10:39 +02:00
|
|
|
{
|
|
|
|
char *ep;
|
|
|
|
uintmax_t val;
|
|
|
|
|
|
|
|
errno = 0;
|
2022-03-07 04:48:59 +01:00
|
|
|
val = strtoumax(cp, &ep, base);
|
2021-08-11 06:10:39 +02:00
|
|
|
if (cp == ep)
|
|
|
|
{
|
|
|
|
return -EINVAL; /* not a number */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ep != '\0')
|
|
|
|
{
|
|
|
|
return -EINVAL; /* not a number */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(errno == ERANGE);
|
|
|
|
DEBUGASSERT(val == UINTMAX_MAX);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
*resultp = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_parsestatus
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
static inline int wget_parsestatus(struct webclient_context *ctx,
|
|
|
|
struct wget_s *ws)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int ndx;
|
|
|
|
char *dest;
|
|
|
|
|
|
|
|
offset = ws->offset;
|
|
|
|
ndx = ws->ndx;
|
|
|
|
|
|
|
|
while (offset < ws->datend)
|
|
|
|
{
|
2021-05-14 04:24:58 +02:00
|
|
|
bool got_nl;
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->line[ndx] = ws->buffer[offset++];
|
2021-05-14 04:24:58 +02:00
|
|
|
got_nl = ws->line[ndx] == ISO_NL;
|
|
|
|
if (got_nl || ndx == CONFIG_WEBCLIENT_MAXHTTPLINE - 1)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-05-14 04:24:58 +02:00
|
|
|
if (!got_nl)
|
|
|
|
{
|
|
|
|
nerr("ERROR: HTTP status line didn't fit "
|
|
|
|
"CONFIG_WEBCLIENT_MAXHTTPLINE: %.*s\n",
|
|
|
|
ndx, ws->line);
|
|
|
|
return -E2BIG;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
ws->line[ndx] = '\0';
|
|
|
|
if ((strncmp(ws->line, g_http10, strlen(g_http10)) == 0) ||
|
|
|
|
(strncmp(ws->line, g_http11, strlen(g_http11)) == 0))
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
unsigned long http_status;
|
|
|
|
char *ep;
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
dest = &(ws->line[9]);
|
|
|
|
ws->httpstatus = HTTPSTATUS_NONE;
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
errno = 0;
|
|
|
|
http_status = strtoul(dest, &ep, 10);
|
|
|
|
if (ep == dest)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ep != ' ')
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->http_status = http_status;
|
2021-05-14 05:19:24 +02:00
|
|
|
ninfo("Got HTTP status %lu\n", http_status);
|
2020-07-12 01:54:28 +02:00
|
|
|
if (ctx->http_reason != NULL)
|
|
|
|
{
|
|
|
|
strncpy(ctx->http_reason,
|
|
|
|
ep + 1,
|
|
|
|
ctx->http_reason_len - 1);
|
|
|
|
ctx->http_reason[ctx->http_reason_len - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for 2xx (Successful) */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
if ((http_status / 100) == 2)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
ws->httpstatus = HTTPSTATUS_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
/* Check for 3xx (Redirection)
|
2020-04-22 13:36:07 +02:00
|
|
|
* Location: header line will contain the new location.
|
2021-04-07 03:59:28 +02:00
|
|
|
*
|
|
|
|
* Note: the following 3xx are not redirects.
|
|
|
|
* 304 not modified
|
|
|
|
* 305 use proxy
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
2021-04-07 03:59:28 +02:00
|
|
|
else if ((http_status / 100) == 3 &&
|
|
|
|
(http_status != 304) &&
|
|
|
|
(http_status != 305))
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
ws->httpstatus = HTTPSTATUS_MOVED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-12 01:23:31 +01:00
|
|
|
return -ECONNABORTED;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
/* We're done parsing the status line, so start parsing
|
|
|
|
* the HTTP headers.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
ws->state = WEBCLIENT_STATE_HEADERS;
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->internal_flags &= ~WGET_FLAG_CHUNKED;
|
2021-05-14 03:57:57 +02:00
|
|
|
ndx = 0;
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ndx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->offset = offset;
|
|
|
|
ws->ndx = ndx;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-05-25 16:22:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: parseurl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-05-16 04:40:04 +02:00
|
|
|
static int parseurl(FAR const char *url, FAR struct wget_target_s *targ)
|
2020-05-25 16:22:55 +02:00
|
|
|
{
|
|
|
|
struct url_s url_s;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&url_s, 0, sizeof(url_s));
|
2022-05-16 04:40:04 +02:00
|
|
|
url_s.scheme = targ->scheme;
|
|
|
|
url_s.schemelen = sizeof(targ->scheme);
|
|
|
|
url_s.host = targ->hostname;
|
|
|
|
url_s.hostlen = sizeof(targ->hostname);
|
|
|
|
url_s.path = targ->filename;
|
|
|
|
url_s.pathlen = sizeof(targ->filename);
|
2020-05-25 16:22:55 +02:00
|
|
|
ret = netlib_parseurl(url, &url_s);
|
2020-05-28 10:19:16 +02:00
|
|
|
if (ret < 0)
|
2020-05-25 16:22:55 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url_s.port == 0)
|
|
|
|
{
|
2022-05-16 04:40:04 +02:00
|
|
|
if (!strcmp(targ->scheme, "https"))
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
2022-05-16 04:40:04 +02:00
|
|
|
targ->port = 443;
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-16 04:40:04 +02:00
|
|
|
targ->port = 80;
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
2020-05-25 16:22:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-16 04:40:04 +02:00
|
|
|
targ->port = url_s.port;
|
2020-05-25 16:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2020-04-22 10:29:02 +02:00
|
|
|
* Name: wget_parseheaders
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-07-21 17:05:18 +02:00
|
|
|
static inline int wget_parseheaders(struct webclient_context *ctx,
|
|
|
|
struct wget_s *ws)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int ndx;
|
2020-05-25 16:22:55 +02:00
|
|
|
int ret = OK;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
offset = ws->offset;
|
|
|
|
ndx = ws->ndx;
|
|
|
|
|
|
|
|
while (offset < ws->datend)
|
|
|
|
{
|
2021-05-14 04:09:32 +02:00
|
|
|
bool got_nl;
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->line[ndx] = ws->buffer[offset++];
|
2021-05-14 04:09:32 +02:00
|
|
|
got_nl = ws->line[ndx] == ISO_NL;
|
|
|
|
if (got_nl || ndx == CONFIG_WEBCLIENT_MAXHTTPLINE - 1)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-05-14 04:09:32 +02:00
|
|
|
bool found;
|
|
|
|
|
|
|
|
if (ws->skip_to_next_line)
|
|
|
|
{
|
|
|
|
if (got_nl)
|
|
|
|
{
|
|
|
|
ws->skip_to_next_line = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have an entire HTTP header line in ws->line, or
|
|
|
|
* our buffer is already full, so we start parsing it.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-14 04:09:32 +02:00
|
|
|
found = false;
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ndx > 0) /* Should always be true */
|
|
|
|
{
|
2021-05-14 04:09:32 +02:00
|
|
|
ninfo("Got HTTP header line%s: %.*s\n",
|
|
|
|
got_nl ? "" : " (truncated)",
|
|
|
|
ndx - 1, &ws->line[0]);
|
2021-07-21 17:05:18 +02:00
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
if (ws->line[0] == ISO_CR)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-22 13:36:07 +02:00
|
|
|
/* This was the last header line (i.e., and empty "\r\n"),
|
|
|
|
* so we are done with the headers and proceed with the
|
|
|
|
* actual data.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
if ((ws->internal_flags & WGET_FLAG_CHUNKED) != 0)
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_CHUNKED_HEADER;
|
|
|
|
ndx = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_DATA;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
goto exit;
|
2020-04-22 13:36:07 +02:00
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Truncate the trailing \r\n */
|
|
|
|
|
2021-05-14 04:09:32 +02:00
|
|
|
if (got_nl)
|
|
|
|
{
|
|
|
|
ndx--;
|
|
|
|
if (ws->line[ndx] != ISO_CR)
|
|
|
|
{
|
|
|
|
nerr("ERROR: unexpected EOL from the server\n");
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->line[ndx] = '\0';
|
|
|
|
|
|
|
|
if (!strchr(ws->line, ':'))
|
|
|
|
{
|
|
|
|
if (got_nl)
|
|
|
|
{
|
|
|
|
nerr("ERROR: invalid header possibly due to "
|
|
|
|
"small CONFIG_WEBCLIENT_MAXHTTPLINE\n");
|
|
|
|
return -E2BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nerr("ERROR: invalid header\n");
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-07-21 17:05:18 +02:00
|
|
|
if (ctx->header_callback)
|
|
|
|
{
|
|
|
|
ret = ctx->header_callback(&ws->line[0], !got_nl,
|
|
|
|
ctx->header_callback_arg);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Check for specific HTTP header fields. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_WEBCLIENT_GETMIMETYPE
|
2020-04-22 13:36:07 +02:00
|
|
|
if (strncasecmp(ws->line, g_httpcontenttype,
|
|
|
|
strlen(g_httpcontenttype)) == 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Found Content-type field. */
|
|
|
|
|
|
|
|
char *dest = strchr(ws->line, ';');
|
|
|
|
if (dest != NULL)
|
|
|
|
{
|
|
|
|
*dest = 0;
|
2014-09-30 01:10:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
strncpy(ws->mimetype, ws->line + strlen(g_httpcontenttype),
|
|
|
|
sizeof(ws->mimetype));
|
2021-05-14 04:09:32 +02:00
|
|
|
found = true;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2020-04-22 13:36:07 +02:00
|
|
|
if (strncasecmp(ws->line, g_httplocation,
|
|
|
|
strlen(g_httplocation)) == 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-05-25 16:22:55 +02:00
|
|
|
/* Parse the new host and filename from the URL.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-25 16:22:55 +02:00
|
|
|
ninfo("Redirect to location: '%s'\n",
|
|
|
|
ws->line + strlen(g_httplocation));
|
2022-05-16 04:40:04 +02:00
|
|
|
ret = parseurl(ws->line + strlen(g_httplocation),
|
|
|
|
&ws->target);
|
2020-04-22 13:36:07 +02:00
|
|
|
ninfo("New hostname='%s' filename='%s'\n",
|
2022-05-16 04:40:04 +02:00
|
|
|
ws->target.hostname, ws->target.filename);
|
2021-05-14 04:09:32 +02:00
|
|
|
found = true;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2021-08-11 06:10:39 +02:00
|
|
|
else if (strncasecmp(ws->line, g_httpcontsize,
|
|
|
|
strlen(g_httpcontsize)) == 0)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
if (got_nl)
|
|
|
|
{
|
|
|
|
ret = wget_parseint(ws->line + strlen(g_httpcontsize),
|
2022-03-07 04:48:59 +01:00
|
|
|
&ws->expected_resp_body_len, 10);
|
2021-08-11 06:10:39 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->internal_flags |=
|
|
|
|
WGET_FLAG_GOT_CONTENT_LENGTH;
|
|
|
|
ninfo("Content-Length %ju\n",
|
|
|
|
ws->expected_resp_body_len);
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 04:48:59 +01:00
|
|
|
else if (strncasecmp(ws->line, g_httptransferencoding,
|
|
|
|
strlen(g_httptransferencoding)) == 0)
|
|
|
|
{
|
|
|
|
FAR const char *encodings =
|
|
|
|
ws->line + strlen(g_httptransferencoding);
|
|
|
|
|
|
|
|
if (strcasecmp(encodings, "chunked"))
|
|
|
|
{
|
|
|
|
nerr("unknown encodings: '%s'\n", encodings);
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninfo("transfer encodings: '%s'\n", encodings);
|
|
|
|
ws->internal_flags |= WGET_FLAG_CHUNKED;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-14 04:09:32 +02:00
|
|
|
if (found && !got_nl)
|
|
|
|
{
|
|
|
|
/* We found something we might care.
|
|
|
|
* but we couldn't process it correctly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nerr("ERROR: truncated a header due to "
|
|
|
|
"small CONFIG_WEBCLIENT_MAXHTTPLINE\n");
|
|
|
|
return -E2BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're done parsing ws->line, so we reset the index.
|
|
|
|
*
|
|
|
|
* If we haven't seen the entire line yet (!got_nl),
|
|
|
|
* skip the rest of the line.
|
|
|
|
* Otherwise, start processing the next line.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ndx = 0;
|
2021-05-14 04:09:32 +02:00
|
|
|
ws->skip_to_next_line = !got_nl;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ndx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->offset = offset;
|
|
|
|
ws->ndx = ndx;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_parsechunkheader
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int wget_parsechunkheader(struct webclient_context *ctx,
|
|
|
|
struct wget_s *ws)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int ndx;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
offset = ws->offset;
|
|
|
|
ndx = ws->ndx;
|
|
|
|
|
|
|
|
while (offset < ws->datend)
|
|
|
|
{
|
|
|
|
bool got_nl;
|
|
|
|
|
|
|
|
ws->line[ndx] = ws->buffer[offset++];
|
|
|
|
got_nl = ws->line[ndx] == ISO_NL;
|
|
|
|
if (got_nl || ndx == CONFIG_WEBCLIENT_MAXHTTPLINE - 1)
|
|
|
|
{
|
|
|
|
bool found_extension = false;
|
|
|
|
|
|
|
|
/* We have an entire header line in ws->line, or
|
|
|
|
* our buffer is already full, so we start parsing it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ndx > 0) /* Should always be true */
|
|
|
|
{
|
|
|
|
FAR char *semicolon;
|
|
|
|
|
|
|
|
ninfo("Got chunk header line%s: %.*s\n",
|
|
|
|
got_nl ? "" : " (truncated)",
|
|
|
|
ndx - 1, &ws->line[0]);
|
|
|
|
|
|
|
|
if (ws->line[0] == ISO_CR)
|
|
|
|
{
|
|
|
|
nerr("ERROR: empty chunk header\n");
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncate the trailing \r\n */
|
|
|
|
|
|
|
|
if (got_nl)
|
|
|
|
{
|
|
|
|
ndx--;
|
|
|
|
if (ws->line[ndx] != ISO_CR)
|
|
|
|
{
|
|
|
|
nerr("ERROR: unexpected EOL from the server\n");
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->line[ndx] = '\0';
|
|
|
|
|
|
|
|
semicolon = strchr(ws->line, ';');
|
|
|
|
if (semicolon != NULL)
|
|
|
|
{
|
|
|
|
found_extension = true;
|
|
|
|
ninfo("Ignoring extentions in chunk header\n");
|
|
|
|
*semicolon = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!got_nl && !found_extension)
|
|
|
|
{
|
|
|
|
/* We found something we might care.
|
|
|
|
* but we couldn't process it correctly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nerr("ERROR: truncated a header due to "
|
|
|
|
"small CONFIG_WEBCLIENT_MAXHTTPLINE\n");
|
|
|
|
ret = -E2BIG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = wget_parseint(ws->line, &ws->chunk_len, 16);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->chunk_len != 0)
|
|
|
|
{
|
|
|
|
ninfo("Receiving a chunk with %ju bytes\n", ws->chunk_len);
|
|
|
|
ws->state = WEBCLIENT_STATE_CHUNKED_DATA;
|
|
|
|
ws->chunk_received = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_CHUNKED_TRAILER;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ndx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->offset = offset;
|
|
|
|
ws->ndx = ndx;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_parsechunkenddata
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int wget_parsechunkenddata(struct webclient_context *ctx,
|
|
|
|
struct wget_s *ws)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int ndx;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
offset = ws->offset;
|
|
|
|
ndx = ws->ndx;
|
|
|
|
|
|
|
|
while (offset < ws->datend)
|
|
|
|
{
|
|
|
|
ws->line[ndx] = ws->buffer[offset++];
|
|
|
|
if (ws->line[ndx] == ISO_NL)
|
|
|
|
{
|
|
|
|
if (ndx == 0)
|
|
|
|
{
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->line[ndx - 1] != ISO_CR)
|
|
|
|
{
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ndx != 1)
|
|
|
|
{
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->chunk_len == 0)
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_CHUNKED_TRAILER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_CHUNKED_HEADER;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->offset = offset;
|
|
|
|
ws->ndx = ndx;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_parsechunktrailer
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int wget_parsechunktrailer(struct webclient_context *ctx,
|
|
|
|
struct wget_s *ws)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int ndx;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
offset = ws->offset;
|
|
|
|
ndx = ws->ndx;
|
|
|
|
|
|
|
|
while (offset < ws->datend)
|
|
|
|
{
|
|
|
|
ws->line[ndx] = ws->buffer[offset++];
|
|
|
|
if (ws->line[ndx] == ISO_NL)
|
|
|
|
{
|
|
|
|
if (ndx == 0)
|
|
|
|
{
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->line[ndx - 1] != ISO_CR)
|
|
|
|
{
|
|
|
|
ret = -EPROTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ndx != 1)
|
|
|
|
{
|
|
|
|
/* Ignore all non empty lines. */
|
|
|
|
|
|
|
|
ndx = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->state = WEBCLIENT_STATE_WAIT_CLOSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->offset = offset;
|
2011-03-19 22:04:13 +01:00
|
|
|
ws->ndx = ndx;
|
2020-05-25 16:22:55 +02:00
|
|
|
return ret;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2015-07-11 16:15:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_gethostip
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-22 10:29:02 +02:00
|
|
|
* Call getaddrinfo() to get the IPv4 address associated with a hostname.
|
2015-07-11 16:15:29 +02:00
|
|
|
*
|
|
|
|
* Input Parameters
|
|
|
|
* hostname - The host name to use in the nslookup.
|
2020-04-22 10:29:02 +02:00
|
|
|
*
|
|
|
|
* Output Parameters
|
|
|
|
* dest - The location to return the IPv4 address.
|
2015-07-11 16:15:29 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-04-22 10:29:02 +02:00
|
|
|
* Zero (OK) on success; ERROR on failure.
|
2015-07-11 16:15:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
static int wget_gethostip(FAR char *hostname, FAR struct in_addr *dest)
|
2015-07-11 16:15:29 +02:00
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
FAR struct addrinfo hint;
|
|
|
|
FAR struct addrinfo *info;
|
|
|
|
FAR struct sockaddr_in *addr;
|
2015-07-11 16:15:29 +02:00
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
memset(&hint, 0, sizeof(hint));
|
|
|
|
hint.ai_family = AF_INET;
|
|
|
|
|
|
|
|
if (getaddrinfo(hostname, NULL, &hint, &info) != OK)
|
2015-07-11 16:15:29 +02:00
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
return ERROR;
|
2015-07-11 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
addr = (FAR struct sockaddr_in *)info->ai_addr;
|
|
|
|
memcpy(dest, &addr->sin_addr, sizeof(struct in_addr));
|
|
|
|
|
|
|
|
freeaddrinfo(info);
|
2015-07-11 16:15:29 +02:00
|
|
|
return OK;
|
2020-04-22 10:29:02 +02:00
|
|
|
#else
|
|
|
|
/* No host name support */
|
|
|
|
|
|
|
|
/* Convert strings to numeric IPv4 address */
|
|
|
|
|
|
|
|
int ret = inet_pton(AF_INET, hostname, dest);
|
|
|
|
|
|
|
|
/* The inet_pton() function returns 1 if the conversion succeeds. It will
|
|
|
|
* return 0 if the input is not a valid IPv4 dotted-decimal string or -1
|
|
|
|
* with errno set to EAFNOSUPPORT if the address family argument is
|
|
|
|
* unsupported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (ret > 0) ? OK : ERROR;
|
|
|
|
#endif
|
2015-07-11 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2020-07-12 01:54:28 +02:00
|
|
|
* Name: webclient_perform
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-07-12 01:54:28 +02:00
|
|
|
* 0: if the operation completed successfully;
|
|
|
|
* Negative errno: On a failure
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2012-10-06 16:50:37 +02:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
int webclient_perform(FAR struct webclient_context *ctx)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
struct wget_s *ws;
|
2014-09-30 01:10:49 +02:00
|
|
|
struct timeval tv;
|
2020-04-22 10:29:02 +02:00
|
|
|
char *dest;
|
2020-07-12 01:54:28 +02:00
|
|
|
char *ep;
|
2021-08-10 04:30:32 +02:00
|
|
|
struct conn_s *conn;
|
2020-07-12 01:54:28 +02:00
|
|
|
FAR const struct webclient_tls_ops *tls_ops = ctx->tls_ops;
|
|
|
|
FAR const char *method = ctx->method;
|
|
|
|
FAR void *tls_ctx = ctx->tls_ctx;
|
|
|
|
unsigned int i;
|
2020-04-22 10:29:02 +02:00
|
|
|
int len;
|
2014-02-11 02:11:56 +01:00
|
|
|
int ret;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-05-14 02:44:06 +02:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
|
|
|
DEBUGASSERT(ctx->state == WEBCLIENT_CONTEXT_STATE_INITIALIZED ||
|
|
|
|
(ctx->state == WEBCLIENT_CONTEXT_STATE_IN_PROGRESS &&
|
|
|
|
(ctx->flags & WEBCLIENT_FLAG_NON_BLOCKING) != 0));
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Initialize the state structure */
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ctx->ws == NULL)
|
2020-04-22 10:29:02 +02:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
ws = calloc(1, sizeof(struct wget_s));
|
|
|
|
if (!ws)
|
|
|
|
{
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2021-02-17 09:17:42 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
2020-04-22 10:29:02 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->buffer = ctx->buffer;
|
|
|
|
ws->buflen = ctx->buflen;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Parse the hostname (with optional port number) and filename
|
|
|
|
* from the URL.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-05-16 04:40:04 +02:00
|
|
|
ret = parseurl(ctx->url, &ws->target);
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Malformed URL: %s\n", ctx->url);
|
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2021-02-17 09:17:42 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->state = WEBCLIENT_STATE_SOCKET;
|
|
|
|
ctx->ws = ws;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws = ctx->ws;
|
|
|
|
|
2022-05-16 04:40:04 +02:00
|
|
|
ninfo("hostname='%s' filename='%s'\n", ws->target.hostname,
|
|
|
|
ws->target.filename);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* The following sequence may repeat indefinitely if we are redirected */
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
conn = &ws->conn;
|
2011-03-19 22:04:13 +01:00
|
|
|
do
|
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_SOCKET)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
2022-05-16 04:40:04 +02:00
|
|
|
if (!strcmp(ws->target.scheme, "https") && tls_ops != NULL)
|
2021-02-17 09:17:42 +01:00
|
|
|
{
|
|
|
|
conn->tls = true;
|
|
|
|
}
|
2022-05-16 04:40:04 +02:00
|
|
|
else if (!strcmp(ws->target.scheme, "http"))
|
2021-01-14 04:22:47 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
conn->tls = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-16 04:40:04 +02:00
|
|
|
nerr("ERROR: unsupported scheme: %s\n", ws->target.scheme);
|
2021-01-14 04:22:47 +01:00
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2021-01-14 04:22:47 +01:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Re-initialize portions of the state structure that could have
|
|
|
|
* been left from the previous time through the loop and should not
|
|
|
|
* persist with the new connection.
|
|
|
|
*/
|
2021-01-14 04:22:47 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->httpstatus = HTTPSTATUS_NONE;
|
|
|
|
ws->offset = 0;
|
|
|
|
ws->datend = 0;
|
|
|
|
ws->ndx = 0;
|
|
|
|
ws->redirected = 0;
|
2021-01-14 04:22:47 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
if (ctx->unix_socket_path != NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: TLS on AF_LOCAL socket is not implemented\n");
|
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2021-02-17 09:17:42 +01:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2021-01-14 04:22:47 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
int domain;
|
2021-01-14 04:22:47 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
if (ctx->unix_socket_path != NULL)
|
|
|
|
{
|
|
|
|
domain = PF_LOCAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
domain = PF_INET;
|
|
|
|
}
|
2021-01-14 04:22:47 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Create a socket */
|
|
|
|
|
|
|
|
conn->sockfd = socket(domain, SOCK_STREAM, 0);
|
|
|
|
if (conn->sockfd < 0)
|
2021-01-14 04:22:47 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
ret = -errno;
|
|
|
|
nerr("ERROR: socket failed: %d\n", errno);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2021-01-14 04:22:47 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->need_conn_close = true;
|
|
|
|
|
|
|
|
if ((ctx->flags & WEBCLIENT_FLAG_NON_BLOCKING) != 0)
|
|
|
|
{
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL, 0);
|
|
|
|
ret = fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
nerr("ERROR: F_SETFL failed: %d\n", ret);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2021-01-14 04:22:47 +01:00
|
|
|
}
|
2021-02-17 09:17:42 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set send and receive timeout values */
|
|
|
|
|
|
|
|
tv.tv_sec = ctx->timeout_sec;
|
|
|
|
tv.tv_usec = 0;
|
2021-01-14 04:22:47 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
setsockopt(conn->sockfd, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(FAR const void *)&tv, sizeof(struct timeval));
|
|
|
|
setsockopt(conn->sockfd, SOL_SOCKET, SO_SNDTIMEO,
|
|
|
|
(FAR const void *)&tv, sizeof(struct timeval));
|
|
|
|
}
|
2021-01-14 04:22:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->state = WEBCLIENT_STATE_CONNECT;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_CONNECT)
|
|
|
|
{
|
|
|
|
if (conn->tls)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
char port_str[sizeof("65535")];
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
if (ctx->unix_socket_path != NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: TLS on AF_LOCAL socket is not implemented\n");
|
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2021-02-17 09:17:42 +01:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-16 04:40:04 +02:00
|
|
|
snprintf(port_str, sizeof(port_str), "%u", ws->target.port);
|
|
|
|
ret = tls_ops->connect(tls_ctx, ws->target.hostname, port_str,
|
2021-02-17 09:17:42 +01:00
|
|
|
ctx->timeout_sec,
|
|
|
|
&conn->tls_conn);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
ws->need_conn_close = true;
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
2021-02-17 09:17:42 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
struct sockaddr_un server_un;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in server_in;
|
|
|
|
const struct sockaddr *server_address;
|
|
|
|
socklen_t server_address_len;
|
|
|
|
|
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
if (ctx->unix_socket_path != NULL)
|
|
|
|
{
|
|
|
|
memset(&server_un, 0, sizeof(server_un));
|
|
|
|
server_un.sun_family = AF_LOCAL;
|
|
|
|
strncpy(server_un.sun_path, ctx->unix_socket_path,
|
|
|
|
sizeof(server_un.sun_path));
|
|
|
|
#if !defined(__NuttX__) && !defined(__linux__)
|
|
|
|
server_un.sun_len = SUN_LEN(&server_un);
|
|
|
|
#endif
|
|
|
|
server_address = (const struct sockaddr *)&server_un;
|
|
|
|
server_address_len = sizeof(server_un);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Get the server address from the host name */
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
server_in.sin_family = AF_INET;
|
2022-05-16 04:40:04 +02:00
|
|
|
server_in.sin_port = htons(ws->target.port);
|
|
|
|
ret = wget_gethostip(ws->target.hostname,
|
|
|
|
&server_in.sin_addr);
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Could not resolve host (or malformed IP address) */
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
nwarn("WARNING: Failed to resolve hostname\n");
|
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2021-02-17 09:17:42 +01:00
|
|
|
return -EHOSTUNREACH;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
server_address = (const struct sockaddr *)&server_in;
|
|
|
|
server_address_len = sizeof(struct sockaddr_in);
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Connect to server. First we have to set some fields in the
|
|
|
|
* 'server' address structure. The system will assign me an
|
|
|
|
* arbitrary local port that is not in use.
|
|
|
|
*/
|
2020-07-12 01:54:28 +02:00
|
|
|
|
2021-09-14 06:47:27 +02:00
|
|
|
while (true)
|
2021-02-17 09:17:42 +01:00
|
|
|
{
|
2021-09-14 06:47:27 +02:00
|
|
|
ret = connect(conn->sockfd, server_address,
|
|
|
|
server_address_len);
|
|
|
|
if (ret == -1)
|
2021-02-17 09:17:42 +01:00
|
|
|
{
|
2021-09-14 06:47:27 +02:00
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
if (ret == -EINTR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == -EISCONN)
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
}
|
2021-02-17 09:17:42 +01:00
|
|
|
}
|
2021-09-14 06:47:27 +02:00
|
|
|
break;
|
2021-02-17 09:17:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
nerr("ERROR: connect failed: %d\n", errno);
|
|
|
|
goto errout_with_errno;
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->state = WEBCLIENT_STATE_PREPARE_REQUEST;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_PREPARE_REQUEST)
|
|
|
|
{
|
|
|
|
/* Send the request */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
dest = ws->buffer;
|
|
|
|
ep = ws->buffer + ws->buflen;
|
|
|
|
dest = append(dest, ep, method);
|
|
|
|
dest = append(dest, ep, " ");
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
#ifndef WGET_USE_URLENCODE
|
2022-05-16 04:40:04 +02:00
|
|
|
dest = append(dest, ep, ws->target.filename);
|
2012-11-03 01:00:56 +01:00
|
|
|
#else
|
2021-02-17 09:17:42 +01:00
|
|
|
/* TODO: should we use wget_urlencode_strcpy? */
|
2020-04-22 13:36:07 +02:00
|
|
|
|
2022-05-16 04:40:04 +02:00
|
|
|
dest = append(dest, ep, ws->target.filename);
|
2012-11-03 01:00:56 +01:00
|
|
|
#endif
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
dest = append(dest, ep, " ");
|
2022-03-07 01:29:55 +01:00
|
|
|
if (ctx->protocol_version == WEBCLIENT_PROTOCOL_VERSION_HTTP_1_0)
|
|
|
|
{
|
|
|
|
dest = append(dest, ep, g_http10);
|
|
|
|
}
|
|
|
|
else if (ctx->protocol_version ==
|
|
|
|
WEBCLIENT_PROTOCOL_VERSION_HTTP_1_1)
|
|
|
|
{
|
|
|
|
dest = append(dest, ep, g_http11);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
|
|
|
dest = append(dest, ep, g_httphost);
|
2022-05-16 04:40:04 +02:00
|
|
|
dest = append(dest, ep, ws->target.hostname);
|
2020-07-12 01:54:28 +02:00
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
for (i = 0; i < ctx->nheaders; i++)
|
|
|
|
{
|
|
|
|
dest = append(dest, ep, ctx->headers[i]);
|
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
|
|
|
}
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ctx->bodylen)
|
|
|
|
{
|
|
|
|
char post_size[sizeof("18446744073709551615")];
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
dest = append(dest, ep, g_httpcontsize);
|
|
|
|
sprintf(post_size, "%zu", ctx->bodylen);
|
|
|
|
dest = append(dest, ep, post_size);
|
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
|
2022-03-07 01:30:23 +01:00
|
|
|
if (ctx->protocol_version == WEBCLIENT_PROTOCOL_VERSION_HTTP_1_1)
|
|
|
|
{
|
|
|
|
/* We don't implement persistect connections. */
|
|
|
|
|
|
|
|
dest = append(dest, ep, g_httpconn_close);
|
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
dest = append(dest, ep, g_httpuseragentfields);
|
|
|
|
|
|
|
|
if (dest == NULL)
|
|
|
|
{
|
|
|
|
ret = -E2BIG;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = dest - ws->buffer;
|
|
|
|
|
|
|
|
ws->state = WEBCLIENT_STATE_SEND_REQUEST;
|
|
|
|
ws->state_offset = 0;
|
|
|
|
ws->state_len = len;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_SEND_REQUEST)
|
|
|
|
{
|
|
|
|
ssize_t ssz;
|
2020-07-12 01:54:28 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ssz = conn_send(ctx, conn,
|
|
|
|
ws->buffer + ws->state_offset,
|
|
|
|
ws->state_len);
|
|
|
|
if (ssz < 0)
|
|
|
|
{
|
|
|
|
ret = ssz;
|
|
|
|
nerr("ERROR: send failed: %d\n", -ret);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ssz >= 0)
|
|
|
|
{
|
|
|
|
ws->state_offset += ssz;
|
|
|
|
ws->state_len -= ssz;
|
|
|
|
if (ws->state_len == 0)
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_SEND_REQUEST_BODY;
|
|
|
|
ws->state_offset = 0;
|
|
|
|
ws->state_len = ctx->bodylen;
|
|
|
|
ws->data_buffer = NULL;
|
|
|
|
ninfo("Sending %zu bytes request body\n", ws->state_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_SEND_REQUEST_BODY)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
/* In this state,
|
|
|
|
*
|
|
|
|
* ws->data_buffer the data given by the user
|
|
|
|
* ws->data_offset the byte offset in the entire body
|
|
|
|
* ws->data_len the byte size of the data in ws->data_buffer
|
|
|
|
* ws->state_offset the send offset in ws->data_buffer
|
|
|
|
* ws->state_len the number of remaining bytes to send (total)
|
|
|
|
*/
|
2020-07-12 01:54:28 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state_len == 0)
|
|
|
|
{
|
|
|
|
ninfo("Finished sending request body\n");
|
|
|
|
ws->state = WEBCLIENT_STATE_STATUSLINE;
|
|
|
|
}
|
|
|
|
else if (ws->data_buffer == NULL)
|
2020-07-28 08:23:33 +02:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
FAR const void *input_buffer;
|
|
|
|
size_t input_buffer_size = ws->buflen;
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
size_t todo = ws->state_len;
|
2020-07-12 01:54:28 +02:00
|
|
|
if (input_buffer_size > todo)
|
|
|
|
{
|
|
|
|
input_buffer_size = todo;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_buffer = ws->buffer;
|
|
|
|
ret = ctx->body_callback(ws->buffer,
|
|
|
|
&input_buffer_size,
|
|
|
|
&input_buffer,
|
2020-11-20 01:50:11 +01:00
|
|
|
todo,
|
2020-07-12 01:54:28 +02:00
|
|
|
ctx->body_callback_arg);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: body_callback failed: %d\n", -ret);
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ninfo("Got %zu bytes body chunk / total remaining %zu bytes\n",
|
|
|
|
input_buffer_size, ws->state_len);
|
|
|
|
ws->data_buffer = input_buffer;
|
|
|
|
ws->data_len = input_buffer_size;
|
|
|
|
ws->state_offset = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t bytes_to_send = ws->data_len - ws->state_offset;
|
|
|
|
|
|
|
|
DEBUGASSERT(bytes_to_send <= ws->state_len);
|
|
|
|
ssize_t ssz = conn_send(ctx, conn,
|
|
|
|
ws->data_buffer + ws->state_offset,
|
|
|
|
bytes_to_send);
|
|
|
|
if (ssz < 0)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
ret = ssz;
|
|
|
|
nerr("ERROR: send failed: %d\n", -ret);
|
|
|
|
goto errout_with_errno;
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ninfo("Sent %zd bytes request body at offset %ju "
|
|
|
|
"in the %zu bytes chunk, "
|
|
|
|
"total remaining %zu bytes\n",
|
|
|
|
ssz,
|
|
|
|
(uintmax_t)ws->state_offset,
|
|
|
|
ws->data_len,
|
|
|
|
ws->state_len);
|
|
|
|
ws->state_len -= ssz;
|
|
|
|
ws->state_offset += ssz;
|
|
|
|
DEBUGASSERT(ws->state_offset <= ws->data_len);
|
|
|
|
if (ws->state_offset == ws->data_len)
|
|
|
|
{
|
|
|
|
ws->data_buffer = NULL;
|
|
|
|
}
|
2020-07-28 08:23:33 +02:00
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
2020-08-07 07:13:56 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Now loop to get the file sent in response to the GET. This
|
|
|
|
* loop continues until either we read the end of file (nbytes == 0)
|
|
|
|
* or until we detect that we have been redirected.
|
|
|
|
*/
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_STATUSLINE ||
|
|
|
|
ws->state == WEBCLIENT_STATE_HEADERS ||
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->state == WEBCLIENT_STATE_DATA ||
|
|
|
|
ws->state == WEBCLIENT_STATE_CHUNKED_HEADER ||
|
|
|
|
ws->state == WEBCLIENT_STATE_CHUNKED_DATA)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2022-03-07 04:48:59 +01:00
|
|
|
if (ws->datend - ws->offset == 0)
|
2021-01-14 02:08:49 +01:00
|
|
|
{
|
2022-03-07 04:48:59 +01:00
|
|
|
ssize_t ssz;
|
|
|
|
|
|
|
|
ninfo("Reading new data\n");
|
|
|
|
ssz = conn_recv(ctx, conn, ws->buffer, ws->buflen);
|
|
|
|
if (ssz < 0)
|
2021-02-17 09:17:42 +01:00
|
|
|
{
|
2022-03-07 04:48:59 +01:00
|
|
|
ret = ssz;
|
|
|
|
nerr("ERROR: recv failed: %d\n", -ret);
|
2021-02-17 09:17:42 +01:00
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2022-03-07 04:48:59 +01:00
|
|
|
else if (ssz == 0)
|
2021-08-11 06:10:39 +02:00
|
|
|
{
|
2022-03-07 04:48:59 +01:00
|
|
|
if (ws->state != WEBCLIENT_STATE_DATA &&
|
|
|
|
ws->state != WEBCLIENT_STATE_WAIT_CLOSE)
|
|
|
|
{
|
|
|
|
nerr("Connection lost unexpectedly\n");
|
|
|
|
ret = -ECONNABORTED;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ws->internal_flags &
|
|
|
|
WGET_FLAG_GOT_CONTENT_LENGTH) != 0 &&
|
|
|
|
ws->expected_resp_body_len !=
|
|
|
|
ws->received_body_len)
|
|
|
|
{
|
|
|
|
nerr("Unexpected response body length: "
|
|
|
|
"%ju != %ju\n",
|
|
|
|
ws->expected_resp_body_len,
|
|
|
|
ws->received_body_len);
|
|
|
|
ret = -EPROTO;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninfo("Connection lost\n");
|
|
|
|
ws->state = WEBCLIENT_STATE_CLOSE;
|
|
|
|
ws->redirected = 0;
|
|
|
|
break;
|
2021-08-11 06:10:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
ninfo("Got %zd bytes data\n", ssz);
|
|
|
|
ws->offset = 0;
|
|
|
|
ws->datend = ssz;
|
2021-02-17 09:17:42 +01:00
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Handle initial parsing of the status line */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_STATUSLINE)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
ret = wget_parsestatus(ctx, ws);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Parse the HTTP data */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_HEADERS)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
ret = wget_parseheaders(ctx, ws);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
/* Parse the chunk header */
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_CHUNKED_HEADER)
|
|
|
|
{
|
|
|
|
ret = wget_parsechunkheader(ctx, ws);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_CHUNKED_ENDDATA)
|
|
|
|
{
|
|
|
|
ret = wget_parsechunkenddata(ctx, ws);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_CHUNKED_TRAILER)
|
|
|
|
{
|
|
|
|
ret = wget_parsechunktrailer(ctx, ws);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_WAIT_CLOSE)
|
|
|
|
{
|
|
|
|
uintmax_t received = ws->datend - ws->offset;
|
|
|
|
if (received != 0)
|
|
|
|
{
|
|
|
|
nerr("Unexpected %ju bytes data received", received);
|
|
|
|
ret = -EPROTO;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Dispose of the data payload */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_DATA ||
|
|
|
|
ws->state == WEBCLIENT_STATE_CHUNKED_DATA)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->httpstatus != HTTPSTATUS_MOVED)
|
2021-01-14 04:25:05 +01:00
|
|
|
{
|
2022-03-07 04:48:59 +01:00
|
|
|
uintmax_t received = ws->datend - ws->offset;
|
|
|
|
FAR char *orig_buffer = ws->buffer;
|
|
|
|
int orig_buflen = ws->buflen;
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_CHUNKED_DATA)
|
|
|
|
{
|
|
|
|
uintmax_t chunk_left =
|
|
|
|
ws->chunk_len - ws->chunk_received;
|
|
|
|
|
|
|
|
if (received > chunk_left)
|
|
|
|
{
|
|
|
|
received = chunk_left;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->chunk_received += received;
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:10:39 +02:00
|
|
|
ninfo("Processing resp body %ju - %ju\n",
|
|
|
|
ws->received_body_len,
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->received_body_len + received);
|
|
|
|
ws->received_body_len += received;
|
2021-08-11 06:10:39 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/* Let the client decide what to do with the
|
|
|
|
* received file.
|
|
|
|
*/
|
|
|
|
|
2022-03-07 04:48:59 +01:00
|
|
|
if (received == 0)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
/* We don't have data to give to the client yet. */
|
|
|
|
}
|
|
|
|
else if (ctx->sink_callback)
|
|
|
|
{
|
|
|
|
ret = ctx->sink_callback(&ws->buffer, ws->offset,
|
2022-03-07 04:48:59 +01:00
|
|
|
ws->offset + received,
|
|
|
|
&ws->buflen,
|
2021-02-17 09:17:42 +01:00
|
|
|
ctx->sink_callback_arg);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-07 04:48:59 +01:00
|
|
|
ctx->callback(&ws->buffer, ws->offset,
|
|
|
|
ws->offset + received,
|
2021-02-17 09:17:42 +01:00
|
|
|
&ws->buflen, ctx->sink_callback_arg);
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
2022-03-07 04:48:59 +01:00
|
|
|
|
|
|
|
ws->offset += received;
|
|
|
|
|
|
|
|
/* The buffer swapping API doesn't work for
|
|
|
|
* HTTP 1.1 chunked transfer because the buffer here
|
|
|
|
* might already contain the next chunk header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ctx->protocol_version ==
|
|
|
|
WEBCLIENT_PROTOCOL_VERSION_HTTP_1_1)
|
|
|
|
{
|
|
|
|
if (orig_buffer != ws->buffer ||
|
|
|
|
orig_buflen != ws->buflen)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->state == WEBCLIENT_STATE_CHUNKED_DATA)
|
|
|
|
{
|
|
|
|
if (ws->chunk_len == ws->chunk_received)
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_CHUNKED_ENDDATA;
|
|
|
|
ws->ndx = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->nredirect++;
|
|
|
|
if (ws->nredirect > CONFIG_WEBCLIENT_MAX_REDIRECT)
|
|
|
|
{
|
|
|
|
nerr("ERROR: too many redirects (%u)\n",
|
|
|
|
ws->nredirect);
|
|
|
|
ret = -ELOOP;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
2020-05-25 16:21:07 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws->state = WEBCLIENT_STATE_CLOSE;
|
|
|
|
ws->redirected = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
if (ws->state == WEBCLIENT_STATE_CLOSE)
|
|
|
|
{
|
|
|
|
conn_close(ctx, conn);
|
|
|
|
ws->need_conn_close = false;
|
|
|
|
if (ws->redirected)
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_SOCKET;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws->state = WEBCLIENT_STATE_DONE;
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2021-02-17 09:17:42 +01:00
|
|
|
while (ws->state != WEBCLIENT_STATE_DONE);
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
errout_with_errno:
|
2021-02-17 09:17:42 +01:00
|
|
|
if ((ctx->flags & WEBCLIENT_FLAG_NON_BLOCKING) != 0 &&
|
|
|
|
(ret == -EAGAIN || ret == -EINPROGRESS || ret == -EALREADY))
|
|
|
|
{
|
|
|
|
if (ret == -EINPROGRESS || ret == -EALREADY)
|
|
|
|
{
|
|
|
|
conn->flags |= CONN_WANT_WRITE;
|
|
|
|
}
|
|
|
|
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_IN_PROGRESS);
|
2021-02-17 09:17:42 +01:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->need_conn_close)
|
|
|
|
{
|
|
|
|
conn_close(ctx, conn);
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_DONE);
|
2020-07-12 01:54:28 +02:00
|
|
|
return ret;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: webclient_abort
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is used to free the resources used by webclient_perform()
|
|
|
|
* in case of non blocking I/O.
|
|
|
|
*
|
|
|
|
* After webclient_perform() returned -EAGAIN, the application can either
|
|
|
|
* retry the operation by calling webclient_perform() again or abort
|
|
|
|
* the operation by calling this function.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void webclient_abort(FAR struct webclient_context *ctx)
|
|
|
|
{
|
2021-05-14 02:44:06 +02:00
|
|
|
_CHECK_STATE(ctx, WEBCLIENT_CONTEXT_STATE_IN_PROGRESS);
|
|
|
|
DEBUGASSERT((ctx->flags & WEBCLIENT_FLAG_NON_BLOCKING) != 0);
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
struct wget_s *ws = ctx->ws;
|
|
|
|
|
|
|
|
if (ws == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->need_conn_close)
|
|
|
|
{
|
2021-08-10 04:30:32 +02:00
|
|
|
struct conn_s *conn = &ws->conn;
|
2021-02-17 09:17:42 +01:00
|
|
|
|
|
|
|
conn_close(ctx, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ws);
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_ABORTED);
|
2021-02-17 09:17:42 +01:00
|
|
|
}
|
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: web_post_str
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-06 17:59:45 +01:00
|
|
|
#ifdef WGET_USE_URLENCODE
|
2012-11-03 01:00:56 +01:00
|
|
|
char *web_post_str(FAR char *buffer, int *size, FAR char *name,
|
|
|
|
FAR char *value)
|
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
char *dst = buffer;
|
|
|
|
buffer = wget_strcpy(buffer, name);
|
2012-11-03 01:00:56 +01:00
|
|
|
buffer = wget_strcpy(buffer, "=");
|
2020-04-22 10:29:02 +02:00
|
|
|
buffer = wget_urlencode_strcpy(buffer, value);
|
2012-11-03 01:00:56 +01:00
|
|
|
*size = buffer - dst;
|
|
|
|
return dst;
|
|
|
|
}
|
2012-11-06 17:59:45 +01:00
|
|
|
#endif
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: web_post_strlen
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-06 17:59:45 +01:00
|
|
|
#ifdef WGET_USE_URLENCODE
|
2012-11-03 01:00:56 +01:00
|
|
|
int web_post_strlen(FAR char *name, FAR char *value)
|
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
return strlen(name) + urlencode_len(value, strlen(value)) + 1;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
2012-11-06 17:59:45 +01:00
|
|
|
#endif
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: web_posts_str
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-06 17:59:45 +01:00
|
|
|
#ifdef WGET_USE_URLENCODE
|
2012-11-03 01:00:56 +01:00
|
|
|
char *web_posts_str(FAR char *buffer, int *size, FAR char **name,
|
|
|
|
FAR char **value, int len)
|
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
char *dst = buffer;
|
2012-11-03 01:00:56 +01:00
|
|
|
int wlen;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
{
|
2020-04-22 13:36:07 +02:00
|
|
|
buffer = wget_strcpy(buffer, "&");
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wlen = *size;
|
|
|
|
buffer = web_post_str(buffer, &wlen, name[i], value[i]);
|
|
|
|
buffer += wlen;
|
|
|
|
}
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
*size = buffer - dst;
|
2012-11-03 01:00:56 +01:00
|
|
|
return dst;
|
|
|
|
}
|
2012-11-06 17:59:45 +01:00
|
|
|
#endif
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: web_posts_strlen
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-06 17:59:45 +01:00
|
|
|
#ifdef WGET_USE_URLENCODE
|
2012-11-03 01:00:56 +01:00
|
|
|
int web_posts_strlen(FAR char **name, FAR char **value, int len)
|
|
|
|
{
|
|
|
|
int wlen = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
wlen += web_post_strlen(name[i], value[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return wlen + len - 1;
|
|
|
|
}
|
2012-11-06 17:59:45 +01:00
|
|
|
#endif
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Obtain the requested file from an HTTP server using the GET method.
|
|
|
|
*
|
|
|
|
* Input Parameters
|
|
|
|
* url - A pointer to a string containing either the full URL to
|
|
|
|
* the file to get (e.g., http://www.nutt.org/index.html, or
|
|
|
|
* http://192.168.23.1:80/index.html).
|
|
|
|
* buffer - A user provided buffer to receive the file data (also
|
|
|
|
* used for the outgoing GET request
|
|
|
|
* buflen - The size of the user provided buffer
|
|
|
|
* callback - As data is obtained from the host, this function is
|
|
|
|
* to dispose of each block of file data as it is received.
|
2020-04-22 10:29:02 +02:00
|
|
|
* arg - User argument passed to callback.
|
2012-11-03 01:00:56 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: if the GET operation completed successfully;
|
|
|
|
* -1: On a failure with errno set appropriately
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int wget(FAR const char *url, FAR char *buffer, int buflen,
|
|
|
|
wget_callback_t callback, FAR void *arg)
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
struct webclient_context ctx;
|
|
|
|
int ret;
|
|
|
|
webclient_set_defaults(&ctx);
|
|
|
|
ctx.method = "GET";
|
|
|
|
ctx.url = url;
|
|
|
|
ctx.buffer = buffer;
|
|
|
|
ctx.buflen = buflen;
|
|
|
|
ctx.callback = callback;
|
|
|
|
ctx.sink_callback_arg = arg;
|
|
|
|
ret = webclient_perform(&ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errno = -ret;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-11-06 17:59:45 +01:00
|
|
|
* Name: wget_post
|
2012-11-03 01:00:56 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int wget_post(FAR const char *url, FAR const char *posts, FAR char *buffer,
|
|
|
|
int buflen, wget_callback_t callback, FAR void *arg)
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
static const char *headers = g_httpform;
|
|
|
|
struct webclient_context ctx;
|
|
|
|
int ret;
|
|
|
|
webclient_set_defaults(&ctx);
|
|
|
|
ctx.method = "POST";
|
|
|
|
ctx.url = url;
|
|
|
|
ctx.buffer = buffer;
|
|
|
|
ctx.buflen = buflen;
|
|
|
|
ctx.callback = callback;
|
|
|
|
ctx.sink_callback_arg = arg;
|
|
|
|
ctx.headers = &headers;
|
|
|
|
ctx.nheaders = 1;
|
|
|
|
webclient_set_static_body(&ctx, posts, strlen(posts));
|
|
|
|
ret = webclient_perform(&ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errno = -ret;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: webclient_set_defaults
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void webclient_set_defaults(FAR struct webclient_context *ctx)
|
|
|
|
{
|
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
2022-03-07 01:29:17 +01:00
|
|
|
ctx->protocol_version = WEBCLIENT_PROTOCOL_VERSION_HTTP_1_0;
|
2020-07-12 01:54:28 +02:00
|
|
|
ctx->method = "GET";
|
2021-06-15 03:06:17 +02:00
|
|
|
ctx->timeout_sec = CONFIG_WEBCLIENT_TIMEOUT;
|
2021-05-14 02:44:06 +02:00
|
|
|
_SET_STATE(ctx, WEBCLIENT_CONTEXT_STATE_INITIALIZED);
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: webclient_set_static_body
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void webclient_set_static_body(FAR struct webclient_context *ctx,
|
|
|
|
FAR const void *body,
|
|
|
|
size_t bodylen)
|
|
|
|
{
|
2021-05-14 02:44:06 +02:00
|
|
|
_CHECK_STATE(ctx, WEBCLIENT_CONTEXT_STATE_INITIALIZED);
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
ctx->body_callback = webclient_static_body_func;
|
|
|
|
ctx->body_callback_arg = (void *)body; /* discard const */
|
|
|
|
ctx->bodylen = bodylen;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
2021-02-17 09:17:42 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: webclient_get_poll_info
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function retrieves the information necessary
|
|
|
|
* to wait for events when using non blocking I/O.
|
|
|
|
*
|
|
|
|
* When using WEBCLIENT_FLAG_NON_BLOCKING, webclient_perform() can
|
|
|
|
* return -EAGAIN when it would otherwise block for I/O.
|
|
|
|
* In that case, the application can use this function to
|
|
|
|
* get the information necessary to wait for the I/O events
|
|
|
|
* using poll()/select(), by populating the the given
|
|
|
|
* webclient_poll_info structure with the information.
|
|
|
|
*
|
|
|
|
* The following is an example to use this function to handle EAGAIN.
|
|
|
|
*
|
|
|
|
* retry:
|
|
|
|
* ret = webclient_perform(&ctx);
|
|
|
|
* if (ret == -EAGAIN)
|
|
|
|
* {
|
|
|
|
* struct webclient_poll_info info;
|
|
|
|
* struct pollfd pfd;
|
|
|
|
*
|
|
|
|
* ret = webclient_get_poll_info(&ctx, &info);
|
|
|
|
* if (ret != 0)
|
|
|
|
* {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* memset(&pfd, 0, sizeof(pfd));
|
|
|
|
* pfd.fd = info.fd;
|
|
|
|
* if ((info.flags & WEBCLIENT_POLL_INFO_WANT_READ) != 0)
|
|
|
|
* {
|
|
|
|
* pfd.events |= POLLIN;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* if ((info.flags & WEBCLIENT_POLL_INFO_WANT_WRITE) != 0)
|
|
|
|
* {
|
|
|
|
* pfd.events |= POLLOUT;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ret = poll(&pfd, 1, -1);
|
|
|
|
* if (ret != 0)
|
|
|
|
* {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* goto retry;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int webclient_get_poll_info(FAR struct webclient_context *ctx,
|
|
|
|
FAR struct webclient_poll_info *info)
|
|
|
|
{
|
|
|
|
struct wget_s *ws;
|
2021-08-10 04:30:32 +02:00
|
|
|
struct conn_s *conn;
|
2021-02-17 09:17:42 +01:00
|
|
|
|
2021-05-14 02:44:06 +02:00
|
|
|
_CHECK_STATE(ctx, WEBCLIENT_CONTEXT_STATE_IN_PROGRESS);
|
|
|
|
DEBUGASSERT((ctx->flags & WEBCLIENT_FLAG_NON_BLOCKING) != 0);
|
|
|
|
|
2021-02-17 09:17:42 +01:00
|
|
|
ws = ctx->ws;
|
|
|
|
if (ws == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = &ws->conn;
|
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
return ctx->tls_ops->get_poll_info(ctx->tls_ctx, conn->tls_conn, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->fd = conn->sockfd;
|
|
|
|
info->flags = conn->flags & (CONN_WANT_READ | CONN_WANT_WRITE);
|
|
|
|
conn->flags &= ~(CONN_WANT_READ | CONN_WANT_WRITE);
|
|
|
|
return 0;
|
|
|
|
}
|