Add support for wget POST interface; from Darcy Gong
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5301 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
parent
c2fd94127d
commit
93870670fa
@ -397,3 +397,5 @@
|
|||||||
* apps/examples/wgetjson: Test example contributed by Darcy Gong
|
* apps/examples/wgetjson: Test example contributed by Darcy Gong
|
||||||
* apps/examples/cxxtest: A test for the uClibc++ library provided by
|
* apps/examples/cxxtest: A test for the uClibc++ library provided by
|
||||||
Qiang Yu and the RGMP team.
|
Qiang Yu and the RGMP team.
|
||||||
|
* apps/netutils/webclient, apps/netutils.codes, and apps/examples/wgetjson:
|
||||||
|
Add support for wget POST interface. Contributed by Darcy Gong.
|
||||||
|
@ -20,4 +20,8 @@ config EXAMPLES_WGETJSON_URL
|
|||||||
string "wget URL"
|
string "wget URL"
|
||||||
default "http://10.0.0.1/wgetjson/json_cmd.php"
|
default "http://10.0.0.1/wgetjson/json_cmd.php"
|
||||||
|
|
||||||
|
config EXAMPLES_WGETPOST_URL
|
||||||
|
string "wget_post URL"
|
||||||
|
default "http://10.0.0.1/wgetjson/post_cmd.php"
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
3
examples/wgetjson/webserver/wgetjson/post_cmd.php
Normal file
3
examples/wgetjson/webserver/wgetjson/post_cmd.php
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
echo json_encode($_POST);
|
||||||
|
?>
|
@ -58,13 +58,19 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef CONFIG_EXAMPLES_WGETJSON_MAXSIZE
|
#ifndef CONFIG_EXAMPLES_WGETJSON_MAXSIZE
|
||||||
# define CONFIG_EXAMPLES_WGETJSON_MAXSIZE 1024
|
# define CONFIG_EXAMPLES_WGETJSON_MAXSIZE 1024
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL
|
#ifndef CONFIG_EXAMPLES_WGETJSON_URL
|
||||||
# define CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL "http://10.0.0.1/wgetjson/json_cmd.php"
|
# define CONFIG_EXAMPLES_WGETJSON_URL "http://10.0.0.1/wgetjson/json_cmd.php"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_EXAMPLES_WGETPOST_URL
|
||||||
|
# define CONFIG_EXAMPLES_WGETPOST_URL "http://10.0.0.1/wgetjson/post_cmd.php"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MULTI_POST_NDATA 3
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Types
|
* Private Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -85,6 +91,21 @@ static bool g_has_json = false;
|
|||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void wgetjson_postdebug_callback(FAR char **buffer, int offset,
|
||||||
|
int datend, FAR int *buflen,
|
||||||
|
FAR void *arg)
|
||||||
|
{
|
||||||
|
int len = datend - offset;
|
||||||
|
if (len <= 0)
|
||||||
|
{
|
||||||
|
printf("Callback No Data!\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
((*buffer)[datend]) = '\0';
|
||||||
|
printf("Callback Data(Length:%d):\n%s\n", len, &((*buffer)[offset]));
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: wgetjson_callback
|
* Name: wgetjson_callback
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -271,20 +292,102 @@ static int wgetjson_json_parse(char *text)
|
|||||||
int wgetjson_main(int argc, char *argv[])
|
int wgetjson_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *buffer = NULL;
|
char *buffer = NULL;
|
||||||
char *url = CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL;
|
int buffer_len = 512;
|
||||||
int ret;
|
char *url = CONFIG_EXAMPLES_WGETJSON_URL;
|
||||||
|
int ret = -1;
|
||||||
|
int option;
|
||||||
|
bool is_post = false;
|
||||||
|
bool is_post_multi = false;
|
||||||
|
bool badarg=false;
|
||||||
|
bool is_debug=false;
|
||||||
|
char *post_buff = NULL;
|
||||||
|
int post_buff_len = 0;
|
||||||
|
char *post_single_name = "type";
|
||||||
|
char *post_single_value = "string";
|
||||||
|
char *post_multi_names[MULTI_POST_NDATA] = {"name", "gender", "country"};
|
||||||
|
char *post_multi_values[MULTI_POST_NDATA] = {"darcy", "man", "china"};
|
||||||
|
wget_callback_t wget_cb = wgetjson_callback;
|
||||||
|
|
||||||
buffer = malloc(512);
|
while ((option = getopt(argc, argv, ":pPD")) != ERROR)
|
||||||
|
{
|
||||||
|
switch (option)
|
||||||
|
{
|
||||||
|
case 'p':
|
||||||
|
is_post = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'P':
|
||||||
|
is_post = true;
|
||||||
|
is_post_multi = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'D':
|
||||||
|
is_debug = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ':':
|
||||||
|
badarg = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '?':
|
||||||
|
default:
|
||||||
|
badarg = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (badarg)
|
||||||
|
{
|
||||||
|
printf("usage: wgetjson -p(single post) -P(multi post) -D(debug wget callback)\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_debug)
|
||||||
|
{
|
||||||
|
wget_cb = wgetjson_postdebug_callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_post)
|
||||||
|
{
|
||||||
|
buffer_len = 512*2;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = malloc(buffer_len);
|
||||||
wgetjson_json_release();
|
wgetjson_json_release();
|
||||||
|
|
||||||
printf("URL: %s\n", url);
|
printf("URL: %s\n", url);
|
||||||
|
|
||||||
ret = wget(url, buffer, 512, wgetjson_callback, NULL);
|
if (is_post)
|
||||||
|
{
|
||||||
|
url = CONFIG_EXAMPLES_WGETPOST_URL;
|
||||||
|
if (is_post_multi)
|
||||||
|
{
|
||||||
|
post_buff_len = web_posts_strlen(post_multi_names, post_multi_values, MULTI_POST_NDATA);
|
||||||
|
post_buff = malloc(post_buff_len);
|
||||||
|
web_posts_str(post_buff, &post_buff_len, post_multi_names, post_multi_values, MULTI_POST_NDATA);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
post_buff_len = web_post_strlen(post_single_name, post_single_value);
|
||||||
|
post_buff = malloc(post_buff_len);
|
||||||
|
web_post_str(post_buff, &post_buff_len, post_single_name, post_single_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (post_buff)
|
||||||
|
{
|
||||||
|
ret = wget_post(url, post_buff, buffer, buffer_len, wget_cb, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = wget(url, buffer, buffer_len, wget_cb , NULL);
|
||||||
|
}
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
printf("get json size: %d\n",g_json_bufflen);
|
printf("get json size: %d\n",g_json_bufflen);
|
||||||
}
|
}
|
||||||
else
|
else if (!is_debug)
|
||||||
{
|
{
|
||||||
g_has_json = false;
|
g_has_json = false;
|
||||||
if (wgetjson_json_parse(g_json_buff) == OK && g_has_json)
|
if (wgetjson_json_parse(g_json_buff) == OK && g_has_json)
|
||||||
@ -301,5 +404,10 @@ int wgetjson_main(int argc, char *argv[])
|
|||||||
|
|
||||||
wgetjson_json_release();
|
wgetjson_json_release();
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
if (post_buff)
|
||||||
|
{
|
||||||
|
free(post_buff);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,74 +1,76 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/include/netutils/urldecode.h
|
* apps/include/netutils/urldecode.h
|
||||||
*
|
*
|
||||||
* This file is part of the NuttX RTOS:
|
* This file is part of the NuttX RTOS:
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||||
* Author: Darcy Gong
|
* Author: Darcy Gong
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
* are met:
|
* are met:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer.
|
* notice, this list of conditions and the following disclaimer.
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
* 3. Neither the name of the Institute nor the names of its contributors
|
||||||
* may be used to endorse or promote products derived from this software
|
* may be used to endorse or promote products derived from this software
|
||||||
* without specific prior written permission.
|
* without specific prior written permission.
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
* 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
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef __APPS_INCLUDE_NETUTILS_URLDECODE_H
|
#ifndef __APPS_INCLUDE_NETUTILS_URLDECODE_H
|
||||||
#define __APPS_INCLUDE_NETUTILS_URLDECODE_H
|
#define __APPS_INCLUDE_NETUTILS_URLDECODE_H
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Included Files
|
* Included Files
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifdef CONFIG_CODECS_URLCODE_NEWMEMORY
|
#ifdef CONFIG_CODECS_URLCODE_NEWMEMORY
|
||||||
char *url_encode(char *str);
|
char *url_encode(char *str);
|
||||||
char *url_decode(char *str);
|
char *url_decode(char *str);
|
||||||
#endif /* CONFIG_CODECS_URLCODE_NEWMEMORY */
|
#endif /* CONFIG_CODECS_URLCODE_NEWMEMORY */
|
||||||
|
|
||||||
#ifdef CONFIG_CODECS_URLCODE
|
#ifdef CONFIG_CODECS_URLCODE
|
||||||
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len);
|
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len);
|
||||||
char *urldecode(const char *src, const int src_len, char *dest, int *dest_len);
|
char *urldecode(const char *src, const int src_len, char *dest, int *dest_len);
|
||||||
#endif /* CONFIG_CODECS_URLCODE */
|
int urlencode_len(const char *src, const int src_len);
|
||||||
|
int urldecode_len(const char *src, const int src_len);
|
||||||
#ifdef CONFIG_CODECS_AVR_URLCODE
|
#endif /* CONFIG_CODECS_URLCODE */
|
||||||
void urlrawdecode(char *urlbuf);
|
|
||||||
void urlrawencode(char *str,char *urlbuf);
|
#ifdef CONFIG_CODECS_AVR_URLCODE
|
||||||
#endif /* CONFIG_CODECS_AVR_URLCODE */
|
void urlrawdecode(char *urlbuf);
|
||||||
|
void urlrawencode(char *str,char *urlbuf);
|
||||||
#ifdef __cplusplus
|
#endif /* CONFIG_CODECS_AVR_URLCODE */
|
||||||
}
|
|
||||||
#endif
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
#endif /* __APPS_INCLUDE_NETUTILS_URLDECODE_H */
|
#endif
|
||||||
|
|
||||||
|
#endif /* __APPS_INCLUDE_NETUTILS_URLDECODE_H */
|
||||||
|
|
||||||
|
@ -109,6 +109,13 @@ extern "C" {
|
|||||||
#define EXTERN extern
|
#define EXTERN extern
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
EXTERN char *web_post_str(FAR char *buffer, int *size, FAR char *name,
|
||||||
|
FAR char *value);
|
||||||
|
EXTERN char *web_posts_str(FAR char *buffer, int *size, FAR char **name,
|
||||||
|
FAR char **value, int len);
|
||||||
|
EXTERN int web_post_strlen(FAR char *name, FAR char *value);
|
||||||
|
EXTERN int web_posts_strlen(FAR char **name, FAR char **value, int len);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: wget
|
* Name: wget
|
||||||
*
|
*
|
||||||
@ -141,6 +148,11 @@ extern "C" {
|
|||||||
EXTERN int wget(FAR const char *url, FAR char *buffer, int buflen,
|
EXTERN int wget(FAR const char *url, FAR char *buffer, int buflen,
|
||||||
wget_callback_t callback, FAR void *arg);
|
wget_callback_t callback, FAR void *arg);
|
||||||
|
|
||||||
|
|
||||||
|
EXTERN int wget_post(FAR const char *url, FAR const char *posts,
|
||||||
|
FAR char *buffer, int buflen, wget_callback_t callback,
|
||||||
|
FAR void *arg);
|
||||||
|
|
||||||
#undef EXTERN
|
#undef EXTERN
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -350,6 +350,73 @@ char *urldecode(const char *src, const int src_len, char *dest, int *dest_len)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: urlencode_len
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef CONFIG_CODECS_URLCODE
|
||||||
|
int urlencode_len(const char *src, const int src_len)
|
||||||
|
{
|
||||||
|
const unsigned char *pSrc;
|
||||||
|
const unsigned char *pEnd;
|
||||||
|
int len = 0;
|
||||||
|
|
||||||
|
pEnd = (unsigned char *)src + src_len;
|
||||||
|
for (pSrc = (unsigned char *)src; pSrc < pEnd; pSrc++)
|
||||||
|
{
|
||||||
|
if ((*pSrc >= '0' && *pSrc <= '9') ||
|
||||||
|
(*pSrc >= 'a' && *pSrc <= 'z') ||
|
||||||
|
(*pSrc >= 'A' && *pSrc <= 'Z') ||
|
||||||
|
(*pSrc == '_' || *pSrc == '-' || *pSrc == '.' || *pSrc == '~' || *pSrc == ' '))
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len+=3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: urldecode_len
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef CONFIG_CODECS_URLCODE
|
||||||
|
int urldecode_len(const char *src, const int src_len)
|
||||||
|
{
|
||||||
|
const unsigned char *pSrc;
|
||||||
|
const unsigned char *pEnd;
|
||||||
|
int len = 0;
|
||||||
|
unsigned char cHigh;
|
||||||
|
unsigned char cLow;
|
||||||
|
|
||||||
|
pSrc = (unsigned char *)src;
|
||||||
|
pEnd = (unsigned char *)src + src_len;
|
||||||
|
while (pSrc < pEnd)
|
||||||
|
{
|
||||||
|
if (*pSrc == '%' && pSrc + 2 < pEnd)
|
||||||
|
{
|
||||||
|
cHigh = *(pSrc + 1);
|
||||||
|
cLow = *(pSrc + 2);
|
||||||
|
|
||||||
|
if (IS_HEX_CHAR(cHigh) && IS_HEX_CHAR(cLow))
|
||||||
|
{
|
||||||
|
pSrc += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
len++;
|
||||||
|
pSrc++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: urlrawdecode
|
* Name: urlrawdecode
|
||||||
*
|
*
|
||||||
|
@ -10,4 +10,9 @@ config NETUTILS_WEBCLIENT
|
|||||||
Enable support for the uIP web client.
|
Enable support for the uIP web client.
|
||||||
|
|
||||||
if NETUTILS_WEBCLIENT
|
if NETUTILS_WEBCLIENT
|
||||||
|
|
||||||
|
config NSH_WGET_USERAGENT
|
||||||
|
string "wget Usert Agent"
|
||||||
|
default "NuttX/6.xx.x (; http://www.nuttx.org/)"
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
@ -69,9 +69,31 @@
|
|||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
#include <nuttx/version.h>
|
||||||
#include <apps/netutils/uiplib.h>
|
#include <apps/netutils/uiplib.h>
|
||||||
#include <apps/netutils/webclient.h>
|
#include <apps/netutils/webclient.h>
|
||||||
|
|
||||||
|
#if defined(CONFIG_NETUTILS_CODECS)
|
||||||
|
# if defined(CONFIG_CODECS_URLCODE)
|
||||||
|
# define WGET_USE_URLENCODE 1
|
||||||
|
# include <apps/netutils/urldecode.h>
|
||||||
|
# endif
|
||||||
|
# if defined(CONFIG_CODECS_BASE64)
|
||||||
|
# include <apps/netutils/base64.h>
|
||||||
|
# endif
|
||||||
|
#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
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Definitions
|
* Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -92,6 +114,9 @@
|
|||||||
#define ISO_cr 0x0d
|
#define ISO_cr 0x0d
|
||||||
#define ISO_space 0x20
|
#define ISO_space 0x20
|
||||||
|
|
||||||
|
#define WGET_MODE_GET 0
|
||||||
|
#define WGET_MODE_POST 1
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Types
|
* Private Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -136,10 +161,13 @@ static const char g_httpcontenttype[] = "content-type: ";
|
|||||||
static const char g_httphost[] = "host: ";
|
static const char g_httphost[] = "host: ";
|
||||||
static const char g_httplocation[] = "location: ";
|
static const char g_httplocation[] = "location: ";
|
||||||
static const char g_httpget[] = "GET ";
|
static const char g_httpget[] = "GET ";
|
||||||
|
static const char g_httppost[] = "POST ";
|
||||||
|
|
||||||
static const char g_httpuseragentfields[] =
|
static const char g_httpuseragentfields[] =
|
||||||
"Connection: close\r\n"
|
"Connection: close\r\n"
|
||||||
"User-Agent: NuttX/0.4.x (; http://www.nuttx.org/)\r\n\r\n";
|
"User-Agent: "
|
||||||
|
CONFIG_NSH_WGET_USERAGENT
|
||||||
|
"\r\n\r\n";
|
||||||
|
|
||||||
static const char g_http200[] = "200 ";
|
static const char g_http200[] = "200 ";
|
||||||
static const char g_http301[] = "301 ";
|
static const char g_http301[] = "301 ";
|
||||||
@ -147,6 +175,11 @@ static const char g_http302[] = "302 ";
|
|||||||
|
|
||||||
static const char g_httpcrnl[] = "\r\n";
|
static const char g_httpcrnl[] = "\r\n";
|
||||||
|
|
||||||
|
static const char g_httpform[] = "Content-Type: application/x-www-form-urlencoded";
|
||||||
|
static const char g_httpcontsize[] = "Content-Length: ";
|
||||||
|
//static const char g_httpconn[] = "Connection: Keep-Alive";
|
||||||
|
//static const char g_httpcache[] = "Cache-Control: no-cache";
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -162,11 +195,28 @@ static const char g_httpcrnl[] = "\r\n";
|
|||||||
static char *wget_strcpy(char *dest, const char *src)
|
static char *wget_strcpy(char *dest, const char *src)
|
||||||
{
|
{
|
||||||
int len = strlen(src);
|
int len = strlen(src);
|
||||||
|
|
||||||
memcpy(dest, src, len);
|
memcpy(dest, src, len);
|
||||||
dest[len] = '\0';
|
dest[len] = '\0';
|
||||||
return dest + len;
|
return dest + len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: wget_urlencode_strcpy
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef WGET_USE_URLENCODE
|
||||||
|
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
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: wget_parsestatus
|
* Name: wget_parsestatus
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -320,11 +370,7 @@ exit:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Name: wget_base
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: wget
|
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Obtain the requested file from an HTTP server using the GET method.
|
* Obtain the requested file from an HTTP server using the GET method.
|
||||||
@ -344,6 +390,7 @@ exit:
|
|||||||
* buflen - The size of the user provided buffer
|
* buflen - The size of the user provided buffer
|
||||||
* callback - As data is obtained from the host, this function is
|
* callback - As data is obtained from the host, this function is
|
||||||
* to dispose of each block of file data as it is received.
|
* to dispose of each block of file data as it is received.
|
||||||
|
* mode - Indicates GET or POST modes
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* 0: if the GET operation completed successfully;
|
* 0: if the GET operation completed successfully;
|
||||||
@ -351,15 +398,16 @@ exit:
|
|||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int wget(FAR const char *url, FAR char *buffer, int buflen,
|
static int wget_base(FAR const char *url, FAR char *buffer, int buflen,
|
||||||
wget_callback_t callback, FAR void *arg)
|
wget_callback_t callback, FAR void *arg,
|
||||||
|
FAR const char *posts, uint8_t mode)
|
||||||
{
|
{
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
struct wget_s ws;
|
struct wget_s ws;
|
||||||
bool redirected;
|
bool redirected;
|
||||||
char *dest;
|
char *dest,post_size[8];
|
||||||
int sockfd;
|
int sockfd;
|
||||||
int len;
|
int len,post_len;
|
||||||
int ret = OK;
|
int ret = OK;
|
||||||
|
|
||||||
/* Initialize the state structure */
|
/* Initialize the state structure */
|
||||||
@ -380,6 +428,7 @@ int wget(FAR const char *url, FAR char *buffer, int buflen,
|
|||||||
set_errno(-ret);
|
set_errno(-ret);
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
nvdbg("hostname='%s' filename='%s'\n", ws.hostname, ws.filename);
|
nvdbg("hostname='%s' filename='%s'\n", ws.hostname, ws.filename);
|
||||||
|
|
||||||
/* The following sequence may repeat indefinitely if we are redirected */
|
/* The following sequence may repeat indefinitely if we are redirected */
|
||||||
@ -435,19 +484,53 @@ int wget(FAR const char *url, FAR char *buffer, int buflen,
|
|||||||
|
|
||||||
/* Send the GET request */
|
/* Send the GET request */
|
||||||
|
|
||||||
dest = ws.buffer;
|
dest = ws.buffer;
|
||||||
dest = wget_strcpy(dest, g_httpget);
|
if (mode == WGET_MODE_POST)
|
||||||
dest = wget_strcpy(dest, ws.filename);
|
{
|
||||||
*dest++ = ISO_space;
|
dest = wget_strcpy(dest, g_httppost);
|
||||||
dest = wget_strcpy(dest, g_http10);
|
}
|
||||||
dest = wget_strcpy(dest, g_httpcrnl);
|
else
|
||||||
dest = wget_strcpy(dest, g_httphost);
|
{
|
||||||
dest = wget_strcpy(dest, ws.hostname);
|
dest = wget_strcpy(dest, g_httpget);
|
||||||
dest = wget_strcpy(dest, g_httpcrnl);
|
}
|
||||||
dest = wget_strcpy(dest, g_httpuseragentfields);
|
|
||||||
len = dest - buffer;
|
|
||||||
|
|
||||||
ret = send(sockfd, buffer, len, 0);
|
#ifndef WGET_USE_URLENCODE
|
||||||
|
dest = wget_strcpy(dest, ws.filename);
|
||||||
|
#else
|
||||||
|
//dest = wget_urlencode_strcpy(dest, ws.filename);
|
||||||
|
dest = wget_strcpy(dest, ws.filename);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
*dest++ = ISO_space;
|
||||||
|
dest = wget_strcpy(dest, g_http10);
|
||||||
|
dest = wget_strcpy(dest, g_httpcrnl);
|
||||||
|
dest = wget_strcpy(dest, g_httphost);
|
||||||
|
dest = wget_strcpy(dest, ws.hostname);
|
||||||
|
dest = wget_strcpy(dest, g_httpcrnl);
|
||||||
|
|
||||||
|
if (mode == WGET_MODE_POST)
|
||||||
|
{
|
||||||
|
dest = wget_strcpy(dest, g_httpform);
|
||||||
|
dest = wget_strcpy(dest, g_httpcrnl);
|
||||||
|
dest = wget_strcpy(dest, g_httpcontsize);
|
||||||
|
|
||||||
|
/* Post content size */
|
||||||
|
|
||||||
|
post_len = strlen((char *)posts);
|
||||||
|
sprintf(post_size,"%d", post_len);
|
||||||
|
dest = wget_strcpy(dest, post_size);
|
||||||
|
dest = wget_strcpy(dest, g_httpcrnl);
|
||||||
|
}
|
||||||
|
|
||||||
|
dest = wget_strcpy(dest, g_httpuseragentfields);
|
||||||
|
if (mode == WGET_MODE_POST)
|
||||||
|
{
|
||||||
|
dest = wget_strcpy(dest, (char *)posts);
|
||||||
|
}
|
||||||
|
|
||||||
|
len = dest - buffer;
|
||||||
|
|
||||||
|
ret = send(sockfd, buffer, len, 0);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
ndbg("send failed: %d\n", errno);
|
ndbg("send failed: %d\n", errno);
|
||||||
@ -528,3 +611,119 @@ errout:
|
|||||||
close(sockfd);
|
close(sockfd);
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: web_post_str
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
char *web_post_str(FAR char *buffer, int *size, FAR char *name,
|
||||||
|
FAR char *value)
|
||||||
|
{
|
||||||
|
char *dst=buffer;
|
||||||
|
buffer = wget_strcpy(buffer,name);
|
||||||
|
buffer = wget_strcpy(buffer, "=");
|
||||||
|
buffer = wget_urlencode_strcpy(buffer,value);
|
||||||
|
*size = buffer - dst;
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: web_post_strlen
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int web_post_strlen(FAR char *name, FAR char *value)
|
||||||
|
{
|
||||||
|
return strlen(name) + urlencode_len(value,strlen(value)) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: web_posts_str
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
char *web_posts_str(FAR char *buffer, int *size, FAR char **name,
|
||||||
|
FAR char **value, int len)
|
||||||
|
{
|
||||||
|
char *dst=buffer;
|
||||||
|
int wlen;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
buffer = wget_strcpy(buffer,"&");
|
||||||
|
}
|
||||||
|
|
||||||
|
wlen = *size;
|
||||||
|
buffer = web_post_str(buffer, &wlen, name[i], value[i]);
|
||||||
|
buffer += wlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
*size=buffer-dst;
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: web_posts_strlen
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: wget
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Obtain the requested file from an HTTP server using the GET method.
|
||||||
|
*
|
||||||
|
* Note: If the function is passed a host name, it must already be in
|
||||||
|
* the resolver cache in order for the function to connect to the web
|
||||||
|
* server. It is therefore up to the calling module to implement the
|
||||||
|
* resolver calls and the signal handler used for reporting a resolv
|
||||||
|
* query answer.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
return wget_base(url, buffer, buflen, callback, arg, NULL, WGET_MODE_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: web_posts_strlen
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int wget_post(FAR const char *url, FAR const char *posts, FAR char *buffer,
|
||||||
|
int buflen, wget_callback_t callback, FAR void *arg)
|
||||||
|
{
|
||||||
|
return wget_base(url, buffer, buflen, callback, arg, posts, WGET_MODE_POST);
|
||||||
|
}
|
||||||
|
@ -47,7 +47,7 @@
|
|||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
|
||||||
#include <apps/netutils/uiplib.h>
|
#include <apps/netutils/uiplib.h>
|
||||||
#if defined(CONFIG_NSH_DHCPC)
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
||||||
# include <apps/netutils/resolv.h>
|
# include <apps/netutils/resolv.h>
|
||||||
# include <apps/netutils/dhcpc.h>
|
# include <apps/netutils/dhcpc.h>
|
||||||
#endif
|
#endif
|
||||||
@ -135,7 +135,7 @@ int nsh_netinit(void)
|
|||||||
resolv_init();
|
resolv_init();
|
||||||
#if defined(CONFIG_NSH_DNS)
|
#if defined(CONFIG_NSH_DNS)
|
||||||
addr.s_addr = HTONL(CONFIG_NSH_DNSIPADDR);
|
addr.s_addr = HTONL(CONFIG_NSH_DNSIPADDR);
|
||||||
resolv_conf(&addr.s_addr);
|
resolv_conf(&addr);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user