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
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#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 WEBCLIENT_STATE_STATUSLINE 0
|
|
|
|
#define WEBCLIENT_STATE_HEADERS 1
|
|
|
|
#define WEBCLIENT_STATE_DATA 2
|
|
|
|
#define WEBCLIENT_STATE_CLOSE 3
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct wget_s
|
|
|
|
{
|
|
|
|
/* Internal status */
|
|
|
|
|
|
|
|
uint8_t state;
|
|
|
|
uint8_t httpstatus;
|
|
|
|
|
|
|
|
uint16_t port; /* The port number to use in the connection */
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
#ifdef CONFIG_WEBCLIENT_GETMIMETYPE
|
|
|
|
char mimetype[CONFIG_WEBCLIENT_MAXMIMESIZE];
|
|
|
|
#endif
|
2020-05-25 16:22:55 +02:00
|
|
|
char scheme[sizeof("https") + 1];
|
2011-03-19 22:04:13 +01:00
|
|
|
char hostname[CONFIG_WEBCLIENT_MAXHOSTNAME];
|
|
|
|
char filename[CONFIG_WEBCLIENT_MAXFILENAME];
|
|
|
|
};
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
struct conn
|
|
|
|
{
|
|
|
|
bool tls;
|
|
|
|
int sockfd;
|
|
|
|
struct webclient_tls_connection *tls_conn;
|
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char g_http10[] = "HTTP/1.0";
|
|
|
|
static const char g_http11[] = "HTTP/1.1";
|
|
|
|
#ifdef CONFIG_WEBCLIENT_GETMIMETYPE
|
|
|
|
static const char g_httpcontenttype[] = "content-type: ";
|
|
|
|
#endif
|
|
|
|
static const char g_httphost[] = "host: ";
|
|
|
|
static const char g_httplocation[] = "location: ";
|
|
|
|
|
|
|
|
static const char g_httpuseragentfields[] =
|
|
|
|
"Connection: close\r\n"
|
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
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
static const char g_httpcrnl[] = "\r\n";
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
static const char g_httpform[] = "Content-Type: "
|
|
|
|
"application/x-www-form-urlencoded";
|
|
|
|
static const char g_httpcontsize[] = "Content-Length: ";
|
|
|
|
#if 0
|
|
|
|
static const char g_httpconn[] = "Connection: Keep-Alive";
|
|
|
|
static const char g_httpcache[] = "Cache-Control: no-cache";
|
|
|
|
#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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t conn_send(struct webclient_context *ctx, struct conn *conn,
|
|
|
|
const void *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
return ctx->tls_ops->send(ctx->tls_ctx, conn->tls_conn, buffer, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ssize_t ret = send(conn->sockfd, buffer, len, 0);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: conn_send_all
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int conn_send_all(struct webclient_context *ctx, struct conn *conn,
|
|
|
|
const void *buffer, size_t len)
|
|
|
|
{
|
|
|
|
size_t left = len;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
ret = conn_send(ctx, conn, buffer, left);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer += ret;
|
|
|
|
left -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: conn_recv
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t conn_recv(struct webclient_context *ctx, struct conn *conn,
|
|
|
|
void *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (conn->tls)
|
|
|
|
{
|
|
|
|
return ctx->tls_ops->recv(ctx->tls_ctx, conn->tls_conn, buffer, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ssize_t ret = recv(conn->sockfd, buffer, len, 0);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: conn_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void conn_close(struct webclient_context *ctx, struct conn *conn)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ws->line[ndx] = ws->buffer[offset];
|
2020-04-22 13:36:07 +02:00
|
|
|
if (ws->line[ndx] == ISO_NL)
|
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;
|
2021-05-14 03:57:57 +02:00
|
|
|
ndx = 0;
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset++;
|
|
|
|
ndx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->offset = offset;
|
|
|
|
ws->ndx = ndx;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-05-25 16:22:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: parseurl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int parseurl(FAR const char *url, FAR struct wget_s *ws)
|
|
|
|
{
|
|
|
|
struct url_s url_s;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&url_s, 0, sizeof(url_s));
|
|
|
|
url_s.scheme = ws->scheme;
|
|
|
|
url_s.schemelen = sizeof(ws->scheme);
|
|
|
|
url_s.host = ws->hostname;
|
|
|
|
url_s.hostlen = sizeof(ws->hostname);
|
|
|
|
url_s.path = ws->filename;
|
|
|
|
url_s.pathlen = sizeof(ws->filename);
|
|
|
|
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)
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
if (!strcmp(ws->scheme, "https"))
|
|
|
|
{
|
|
|
|
ws->port = 443;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws->port = 80;
|
|
|
|
}
|
2020-05-25 16:22:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws->port = url_s.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int wget_parseheaders(struct wget_s *ws)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2011-03-19 22:04:13 +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]);
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
ws->state = WEBCLIENT_STATE_DATA;
|
|
|
|
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
|
|
|
|
|
|
|
/* 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));
|
|
|
|
ret = parseurl(ws->line + strlen(g_httplocation), ws);
|
2020-04-22 13:36:07 +02:00
|
|
|
ninfo("New hostname='%s' filename='%s'\n",
|
|
|
|
ws->hostname, ws->filename);
|
2021-05-14 04:09:32 +02:00
|
|
|
found = true;
|
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++;
|
|
|
|
}
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2014-07-08 15:29:38 +02:00
|
|
|
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;
|
2011-03-19 22:04:13 +01:00
|
|
|
bool redirected;
|
2020-05-25 16:21:07 +02:00
|
|
|
unsigned int nredirect = 0;
|
2020-04-22 10:29:02 +02:00
|
|
|
char *dest;
|
2020-07-12 01:54:28 +02:00
|
|
|
char *ep;
|
|
|
|
struct conn conn;
|
|
|
|
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
|
|
|
|
|
|
|
/* Initialize the state structure */
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
ws = calloc(1, sizeof(struct wget_s));
|
|
|
|
if (!ws)
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
return -errno;
|
2020-04-22 10:29:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
ws->buffer = ctx->buffer;
|
|
|
|
ws->buflen = ctx->buflen;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-04-22 13:36:07 +02:00
|
|
|
/* Parse the hostname (with optional port number) and filename
|
|
|
|
* from the URL.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
ret = parseurl(ctx->url, ws);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
nwarn("WARNING: Malformed URL: %s\n", ctx->url);
|
2020-04-22 10:29:02 +02:00
|
|
|
free(ws);
|
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
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
ninfo("hostname='%s' filename='%s'\n", ws->hostname, ws->filename);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* The following sequence may repeat indefinitely if we are redirected */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
if (!strcmp(ws->scheme, "https") && tls_ops != NULL)
|
|
|
|
{
|
|
|
|
conn.tls = true;
|
|
|
|
}
|
|
|
|
else if (!strcmp(ws->scheme, "http"))
|
|
|
|
{
|
|
|
|
conn.tls = false;
|
|
|
|
}
|
|
|
|
else
|
2020-05-25 16:22:55 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: unsupported scheme: %s\n", ws->scheme);
|
|
|
|
free(ws);
|
2020-07-12 01:54:28 +02:00
|
|
|
return -ENOTSUP;
|
2020-05-25 16:22:55 +02:00
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +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.
|
|
|
|
*/
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
ws->httpstatus = HTTPSTATUS_NONE;
|
|
|
|
ws->offset = 0;
|
|
|
|
ws->datend = 0;
|
|
|
|
ws->ndx = 0;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
if (conn.tls)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
char port_str[sizeof("65535")];
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-01-14 04:22:47 +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);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
snprintf(port_str, sizeof(port_str), "%u", ws->port);
|
|
|
|
ret = tls_ops->connect(tls_ctx, ws->hostname, port_str,
|
|
|
|
CONFIG_WEBCLIENT_TIMEOUT, &conn.tls_conn);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
else
|
|
|
|
{
|
2021-01-14 04:22:47 +01:00
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
struct sockaddr_un server_un;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in server_in;
|
|
|
|
int domain;
|
|
|
|
const struct sockaddr *server_address;
|
|
|
|
socklen_t server_address_len;
|
|
|
|
|
|
|
|
#if defined(CONFIG_WEBCLIENT_NET_LOCAL)
|
|
|
|
if (ctx->unix_socket_path != NULL)
|
|
|
|
{
|
|
|
|
domain = PF_LOCAL;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
domain = PF_INET;
|
|
|
|
|
|
|
|
/* Get the server address from the host name */
|
|
|
|
|
|
|
|
server_in.sin_family = AF_INET;
|
|
|
|
server_in.sin_port = htons(ws->port);
|
|
|
|
ret = wget_gethostip(ws->hostname, &server_in.sin_addr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Could not resolve host (or malformed IP address) */
|
|
|
|
|
|
|
|
nwarn("WARNING: Failed to resolve hostname\n");
|
2021-01-20 02:03:59 +01:00
|
|
|
free(ws);
|
|
|
|
return -EHOSTUNREACH;
|
2021-01-14 04:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
server_address = (const struct sockaddr *)&server_in;
|
|
|
|
server_address_len = sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
/* Create a socket */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-01-14 04:22:47 +01:00
|
|
|
conn.sockfd = socket(domain, SOCK_STREAM, 0);
|
2020-07-12 01:54:28 +02:00
|
|
|
if (conn.sockfd < 0)
|
|
|
|
{
|
|
|
|
/* socket failed. It will set the errno appropriately */
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
nerr("ERROR: socket failed: %d\n", errno);
|
|
|
|
free(ws);
|
|
|
|
return -errno;
|
|
|
|
}
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
/* Set send and receive timeout values */
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
tv.tv_sec = CONFIG_WEBCLIENT_TIMEOUT;
|
|
|
|
tv.tv_usec = 0;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-07-12 01:54:28 +02: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));
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-07-12 01:54:28 +02: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.
|
|
|
|
*/
|
|
|
|
|
2021-01-14 04:22:47 +01:00
|
|
|
ret = connect(conn.sockfd, server_address, server_address_len);
|
2020-07-12 01:54:28 +02:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
2021-05-02 07:55:52 +02:00
|
|
|
close(conn.sockfd);
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: connect failed: %d\n", errno);
|
2020-07-12 01:54:28 +02:00
|
|
|
free(ws);
|
|
|
|
return ret;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
/* Send the request */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
dest = ws->buffer;
|
2020-07-12 01:54:28 +02:00
|
|
|
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
|
2020-07-12 01:54:28 +02:00
|
|
|
dest = append(dest, ep, ws->filename);
|
2012-11-03 01:00:56 +01:00
|
|
|
#else
|
2020-04-22 13:36:07 +02:00
|
|
|
/* TODO: should we use wget_urlencode_strcpy? */
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
dest = append(dest, ep, ws->filename);
|
2012-11-03 01:00:56 +01:00
|
|
|
#endif
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
dest = append(dest, ep, " ");
|
|
|
|
dest = append(dest, ep, g_http10);
|
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
|
|
|
dest = append(dest, ep, g_httphost);
|
|
|
|
dest = append(dest, ep, ws->hostname);
|
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
for (i = 0; i < ctx->nheaders; i++)
|
2012-11-03 01:00:56 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
dest = append(dest, ep, ctx->headers[i]);
|
|
|
|
dest = append(dest, ep, g_httpcrnl);
|
|
|
|
}
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
if (ctx->bodylen)
|
|
|
|
{
|
|
|
|
char post_size[sizeof("18446744073709551615")];
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2020-07-12 01:54:28 +02: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);
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
dest = append(dest, ep, g_httpuseragentfields);
|
|
|
|
|
|
|
|
if (dest == NULL)
|
2012-11-03 01:00:56 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
ret = -E2BIG;
|
|
|
|
goto errout_with_errno;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
len = dest - ws->buffer;
|
|
|
|
|
|
|
|
ret = conn_send_all(ctx, &conn, ws->buffer, len);
|
2012-11-03 01:00:56 +01:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
if (ret >= 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
size_t sent = 0;
|
|
|
|
|
|
|
|
while (sent < ctx->bodylen)
|
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;
|
|
|
|
|
|
|
|
size_t todo = ctx->bodylen - sent;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = conn_send_all(ctx, &conn, input_buffer,
|
|
|
|
input_buffer_size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sent += input_buffer_size;
|
2020-07-28 08:23:33 +02:00
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
}
|
2020-08-07 07:13:56 +02:00
|
|
|
|
2020-07-12 01:54:28 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: send failed: %d\n", -ret);
|
|
|
|
goto errout_with_errno;
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
ws->state = WEBCLIENT_STATE_STATUSLINE;
|
2011-03-19 22:04:13 +01:00
|
|
|
redirected = false;
|
2020-04-22 13:36:07 +02:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
ws->datend = conn_recv(ctx, &conn, ws->buffer, ws->buflen);
|
2020-04-22 10:29:02 +02:00
|
|
|
if (ws->datend < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
ret = ws->datend;
|
|
|
|
nerr("ERROR: recv failed: %d\n", -ret);
|
|
|
|
goto errout_with_errno;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2020-04-22 10:29:02 +02:00
|
|
|
else if (ws->datend == 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-01-14 02:08:49 +01:00
|
|
|
if (ws->state != WEBCLIENT_STATE_DATA)
|
|
|
|
{
|
|
|
|
nerr("Connection lost unexpectedly\n");
|
|
|
|
ret = -ECONNABORTED;
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Connection lost\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle initial parsing of the status line */
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
ws->offset = 0;
|
|
|
|
if (ws->state == WEBCLIENT_STATE_STATUSLINE)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-07-12 01:54:28 +02:00
|
|
|
ret = wget_parsestatus(ctx, ws);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the HTTP data */
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
if (ws->state == WEBCLIENT_STATE_HEADERS)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
ret = wget_parseheaders(ws);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispose of the data payload */
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
if (ws->state == WEBCLIENT_STATE_DATA)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-22 10:29:02 +02:00
|
|
|
if (ws->httpstatus != HTTPSTATUS_MOVED)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-04-22 13:36:07 +02:00
|
|
|
/* Let the client decide what to do with the
|
|
|
|
* received file.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-01-14 04:25:05 +01:00
|
|
|
if (ws->offset == ws->datend)
|
|
|
|
{
|
|
|
|
/* We don't have data to give to the client yet. */
|
|
|
|
}
|
|
|
|
else if (ctx->sink_callback)
|
2020-07-12 01:54:28 +02:00
|
|
|
{
|
|
|
|
ret = ctx->sink_callback(&ws->buffer, ws->offset,
|
|
|
|
ws->datend, &ws->buflen,
|
|
|
|
ctx->sink_callback_arg);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx->callback(&ws->buffer, ws->offset, ws->datend,
|
|
|
|
&ws->buflen, ctx->sink_callback_arg);
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
redirected = true;
|
2020-05-25 16:21:07 +02:00
|
|
|
nredirect++;
|
|
|
|
if (nredirect > CONFIG_WEBCLIENT_MAX_REDIRECT)
|
|
|
|
{
|
|
|
|
nerr("ERROR: too many redirects (%u)\n", nredirect);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 01:54:28 +02:00
|
|
|
|
|
|
|
conn_close(ctx, &conn);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
while (redirected);
|
2014-09-30 01:10:49 +02:00
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
free(ws);
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
2020-07-12 01:54:28 +02:00
|
|
|
ret = -errno;
|
|
|
|
errout_with_errno:
|
|
|
|
conn_close(ctx, &conn);
|
|
|
|
|
2020-04-22 10:29:02 +02:00
|
|
|
free(ws);
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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));
|
|
|
|
ctx->method = "GET";
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: webclient_set_static_body
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void webclient_set_static_body(FAR struct webclient_context *ctx,
|
|
|
|
FAR const void *body,
|
|
|
|
size_t bodylen)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|