Merge pull request #18 from chenyong111/master

完善日志信息,完善 TLS 处理方式
master
朱天龙 (Armink) 7 years ago committed by GitHub
commit 426db43365
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because it is too large Load Diff

@ -1,6 +1,20 @@
/* /*
* File : webclient.h * File : webclient.h
* COPYRIGHT (C) 2011-2018, Shanghai Real-Thread Technology Co., Ltd * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
@ -9,35 +23,79 @@
* 2015-11-14 aozima add content_length_remainder. * 2015-11-14 aozima add content_length_remainder.
* 2017-12-23 aozima update gethostbyname to getaddrinfo. * 2017-12-23 aozima update gethostbyname to getaddrinfo.
* 2018-01-04 aozima add ipv6 address support. * 2018-01-04 aozima add ipv6 address support.
* 2018-07-26 chenyong modify log information
*/ */
#ifndef __WEBCLIENT_H__ #ifndef __WEBCLIENT_H__
#define __WEBCLIENT_H__ #define __WEBCLIENT_H__
#include <stddef.h>
#include <rtthread.h> #include <rtthread.h>
#ifdef WEBCLIENT_USING_TLS #ifdef WEBCLIENT_USING_TLS
#include <tls_client.h> #include <tls_client.h>
#endif #endif
#undef DBG_SECTION_NAME
#undef DBG_LEVEL
#undef DBG_COLOR
#undef DBG_ENABLE
#define DBG_ENABLE
#define DBG_SECTION_NAME "WEB"
#ifdef WEBCLIENT_DEBUG
#define DBG_LEVEL DBG_LOG
#else
#define DBG_LEVEL DBG_INFO
#endif /* WEBCLIENT_DEBUG */
#define DBG_COLOR
#include <rtdbg.h>
#ifndef web_malloc
#define web_malloc rt_malloc
#endif
#ifndef web_calloc
#define web_calloc rt_calloc
#endif
#ifndef web_realloc
#define web_realloc rt_realloc
#endif
#ifndef web_free
#define web_free rt_free
#endif
#ifndef web_strdup
#define web_strdup rt_strdup
#endif
#define WEBCLIENT_SW_VERSION "2.0.0"
#define WEBCLIENT_SW_VERSION_NUM 0x20000
#ifndef WEBCLIENT_HEADER_BUFSZ
#define WEBCLIENT_HEADER_BUFSZ 4096 #define WEBCLIENT_HEADER_BUFSZ 4096
#endif
#ifndef WEBCLIENT_RESPONSE_BUFSZ
#define WEBCLIENT_RESPONSE_BUFSZ 4096 #define WEBCLIENT_RESPONSE_BUFSZ 4096
#define WEBCLIENT_TLS_READ_BUFFER 4096 #endif
//typedef unsigned int size_t; #if defined(WEBCLIENT_USING_TLS) && !defined(WEBCLIENT_TLS_READ_BUFFER)
#define WEBCLIENT_TLS_READ_BUFFER 4096
#endif
enum WEBCLIENT_STATUS enum WEBCLIENT_STATUS
{ {
WEBCLIENT_OK, WEBCLIENT_OK,
WEBCLIENT_ERROR,
WEBCLIENT_TIMEOUT,
WEBCLIENT_NOMEM, WEBCLIENT_NOMEM,
WEBCLIENT_NOSOCKET, WEBCLIENT_NOSOCKET,
WEBCLIENT_NOBUFFER, WEBCLIENT_NOBUFFER,
WEBCLIENT_CONNECT_FAILED, WEBCLIENT_CONNECT_FAILED,
WEBCLIENT_DISCONNECT, WEBCLIENT_DISCONNECT,
WEBCLIENT_FILE_ERROR, WEBCLIENT_FILE_ERROR,
WEBCLIENT_TIMEOUT,
}; };
enum WEBCLIENT_METHOD enum WEBCLIENT_METHOD
@ -75,23 +133,30 @@ struct webclient_session
/* HTTP request */ /* HTTP request */
char *request; char *request;
/* private for webclient session. */
/* position of reading */ /* position of reading */
unsigned int position; unsigned int position;
/* remainder of content reading */ /* remainder of content reading */
size_t content_length_remainder; size_t content_length_remainder;
int header_sz;
int resp_sz;
#ifdef WEBCLIENT_USING_TLS #ifdef WEBCLIENT_USING_TLS
/* mbedtls session struct*/ /* mbedtls connect session */
MbedTLSSession *tls_session; MbedTLSSession *tls_session;
#endif #endif
}; };
struct webclient_session *webclient_open(const char *URI); struct webclient_session *webclient_create(size_t header_sz, size_t resp_sz);
struct webclient_session *webclient_open_position(const char *URI, int position);
struct webclient_session *webclient_open_header(const char *URI, int method, const char *header, size_t header_sz); int webclient_get(struct webclient_session *session, const char *URI, const char *header);
int webclient_get_position(struct webclient_session *session, const char *URI, int position);
int webclient_post(struct webclient_session *session, const char *URI,
const char *header, const char *post_data);
int webclient_post_header(struct webclient_session *session, const char *URI, const char *header);
int webclient_close(struct webclient_session *session); int webclient_close(struct webclient_session *session);
int webclient_set_timeout(struct webclient_session *session, int millisecond); int webclient_set_timeout(struct webclient_session *session, int millisecond);
@ -99,21 +164,8 @@ int webclient_set_timeout(struct webclient_session *session, int millisecond);
int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size); int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size);
int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size); int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size);
int webclient_send_header(struct webclient_session *session, int method,
const char *header, size_t header_sz);
int webclient_connect(struct webclient_session *session, const char *URI);
int webclient_handle_response(struct webclient_session *session);
/* hight level APIs for HTTP client */
int webclient_response(struct webclient_session *session, void **response); int webclient_response(struct webclient_session *session, void **response);
struct webclient_session *webclient_open_custom(const char *URI, int method, int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **result);
const char *header, size_t header_sz,
const char *data, size_t data_sz);
int webclient_transfer(const char *URI, const char *header, size_t header_sz,
const char *data, size_t data_sz,
char *result, size_t result_sz);
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
int webclient_get_file(const char *URI, const char *filename); int webclient_get_file(const char *URI, const char *filename);

@ -1,48 +1,70 @@
/*
* File : webclient_file.c
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-07-26 chenyong modify log information
*/
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <rtthread.h> #include <rtthread.h>
#include "webclient.h" #include "webclient.h"
#include "webclient_internal.h"
#ifdef RT_USING_FINSH
#include <finsh.h>
#endif
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
#include <dfs_posix.h> #include <dfs_posix.h>
int webclient_get_file(const char* URI, const char* filename) int webclient_get_file(const char* URI, const char* filename)
{ {
int fd = -1; int fd = -1, rc = WEBCLIENT_OK;
size_t offset; size_t offset;
size_t length, total_length = 0; size_t length, total_length = 0;
rt_uint8_t* ptr = NULL; unsigned char *ptr = RT_NULL;
struct webclient_session* session = NULL; struct webclient_session* session = RT_NULL;
session = webclient_open(URI); session = webclient_create(WEBCLIENT_HEADER_BUFSZ, WEBCLIENT_RESPONSE_BUFSZ);
if (session == NULL) if(session == RT_NULL)
{ {
rt_kprintf("open website failed.\n"); rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
if (session->response != 200)
if (webclient_get(session, URI, RT_NULL) != 200)
{ {
rt_kprintf("wrong response: %d\n", session->response); LOG_E("get file failed, wrong response: %d.", session->response);
rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
if (fd < 0) if (fd < 0)
{ {
rt_kprintf("open file failed\n"); LOG_E("get file failed, open file(%s) error.", filename);
rc = -WEBCLIENT_ERROR;
goto __exit; goto __exit;
} }
ptr = web_malloc(WEBCLIENT_RESPONSE_BUFSZ); ptr = (unsigned char *) web_malloc(WEBCLIENT_RESPONSE_BUFSZ);
if (ptr == NULL) if (ptr == RT_NULL)
{ {
rt_kprintf("out of memory\n"); LOG_E("get file failed, no memory for response buffer.");
rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
@ -58,9 +80,11 @@ int webclient_get_file(const char* URI, const char* filename)
rt_kprintf(">"); rt_kprintf(">");
} }
else else
{
break; break;
} }
} }
}
else else
{ {
for (offset = 0; offset < session->content_length;) for (offset = 0; offset < session->content_length;)
@ -76,7 +100,9 @@ int webclient_get_file(const char* URI, const char* filename)
rt_kprintf(">"); rt_kprintf(">");
} }
else else
{
break; break;
}
offset += length; offset += length;
} }
@ -84,15 +110,26 @@ int webclient_get_file(const char* URI, const char* filename)
if (total_length) if (total_length)
{ {
rt_kprintf("\nSave %d bytes\n", total_length); LOG_D("save %d bytes.", total_length);
} }
__exit: __exit:
if (fd >= 0) close(fd); if (fd >= 0)
if (session != NULL) webclient_close(session); {
if (ptr != NULL) web_free(ptr); close(fd);
}
return 0; if (session != RT_NULL)
{
webclient_close(session);
}
if (ptr != RT_NULL)
{
web_free(ptr);
}
return rc;
} }
int webclient_post_file(const char* URI, const char* filename, int webclient_post_file(const char* URI, const char* filename,
@ -101,13 +138,14 @@ int webclient_post_file(const char* URI, const char* filename,
size_t length; size_t length;
char boundary[60]; char boundary[60];
int fd = -1, rc = WEBCLIENT_OK; int fd = -1, rc = WEBCLIENT_OK;
char *header = NULL, *header_ptr; char *header = RT_NULL, *header_ptr;
unsigned char *buffer = NULL, *buffer_ptr; unsigned char *buffer = RT_NULL, *buffer_ptr;
struct webclient_session* session = NULL; struct webclient_session* session = RT_NULL;
fd = open(filename, O_RDONLY, 0); fd = open(filename, O_RDONLY, 0);
if (fd < 0) if (fd < 0)
{ {
LOG_D("post file failed, open file(%s) error.", filename);
rc = -WEBCLIENT_FILE_ERROR; rc = -WEBCLIENT_FILE_ERROR;
goto __exit; goto __exit;
} }
@ -116,36 +154,25 @@ int webclient_post_file(const char* URI, const char* filename,
length = lseek(fd, 0, SEEK_END); length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
buffer = web_malloc(WEBCLIENT_RESPONSE_BUFSZ); buffer = (unsigned char *) web_malloc(WEBCLIENT_RESPONSE_BUFSZ);
if (buffer == NULL) if (buffer == RT_NULL)
{ {
LOG_D("post file failed, no memory for response buffer.");
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
session = (struct webclient_session*) web_malloc(sizeof(struct webclient_session));
if (!session)
{
rc = -WEBCLIENT_NOMEM;
goto __exit;
}
memset(session, 0x0, sizeof(struct webclient_session));
rc = webclient_connect(session, URI);
if (rc < 0)
goto __exit;
header = (char *) web_malloc(WEBCLIENT_HEADER_BUFSZ); header = (char *) web_malloc(WEBCLIENT_HEADER_BUFSZ);
if (header == NULL) if (header == RT_NULL)
{ {
LOG_D("post file failed, no memory for header buffer.");
rc = -WEBCLIENT_NOMEM; rc = -WEBCLIENT_NOMEM;
goto __exit; goto __exit;
} }
header_ptr = header; header_ptr = header;
/* build boundary */ /* build boundary */
rt_snprintf(boundary, sizeof(boundary), "----------------------------%012d", rt_snprintf(boundary, sizeof(boundary), "----------------------------%012d", rt_tick_get());
rt_tick_get());
/* build encapsulated mime_multipart information*/ /* build encapsulated mime_multipart information*/
buffer_ptr = buffer; buffer_ptr = buffer;
@ -169,11 +196,19 @@ int webclient_post_file(const char* URI, const char* filename,
header_ptr += rt_snprintf(header_ptr, header_ptr += rt_snprintf(header_ptr,
WEBCLIENT_HEADER_BUFSZ - (header_ptr - header), WEBCLIENT_HEADER_BUFSZ - (header_ptr - header),
"Content-Type: multipart/form-data; boundary=%s\r\n", boundary); "Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
/* send header */
rc = webclient_send_header(session, WEBCLIENT_POST, header, session = webclient_create(WEBCLIENT_HEADER_BUFSZ, WEBCLIENT_RESPONSE_BUFSZ);
header_ptr - header); if(session == RT_NULL)
{
rc = -WEBCLIENT_NOMEM;
goto __exit;
}
rc = webclient_post_header(session, URI, header);
if( rc< 0) if( rc< 0)
{
goto __exit; goto __exit;
}
/* send mime_multipart */ /* send mime_multipart */
webclient_write(session, buffer, buffer_ptr - buffer); webclient_write(session, buffer, buffer_ptr - buffer);
@ -183,7 +218,10 @@ int webclient_post_file(const char* URI, const char* filename,
{ {
length = read(fd, buffer, WEBCLIENT_RESPONSE_BUFSZ); length = read(fd, buffer, WEBCLIENT_RESPONSE_BUFSZ);
if (length <= 0) if (length <= 0)
{
break; break;
}
webclient_write(session, buffer, length); webclient_write(session, buffer, length);
} }
@ -192,25 +230,45 @@ int webclient_post_file(const char* URI, const char* filename,
webclient_write(session, buffer, strlen(boundary) + 6); webclient_write(session, buffer, strlen(boundary) + 6);
__exit: __exit:
if (fd >= 0) close(fd); if (fd >= 0)
if (session != NULL) webclient_close(session); {
if (buffer != NULL) web_free(buffer); close(fd);
if (header != NULL) web_free(header); }
if (session != RT_NULL)
{
webclient_close(session);
}
if (buffer != RT_NULL)
{
web_free(buffer);
}
if (header != RT_NULL)
{
web_free(header);
}
return 0; return 0;
} }
int wget(int argc, char** argv) int wget(int argc, char** argv)
{ {
if (argc != 3) if (argc != 3)
{ {
rt_kprintf("wget URI filename\n"); LOG_E("wget [URI] [filename] -get file by URI.");
return 0; return -1;
} }
webclient_get_file(argv[1], argv[2]); webclient_get_file(argv[1], argv[2]);
return 0; return 0;
} }
#ifdef FINSH_USING_MSH
#include <finsh.h>
MSH_CMD_EXPORT(wget, web download file); MSH_CMD_EXPORT(wget, web download file);
#endif /* FINSH_USING_MSH */
#endif #endif /* RT_USING_DFS */

@ -1,11 +0,0 @@
#ifndef WEBCLIENT_INTERNAL_H__
#define WEBCLIENT_INTERNAL_H__
#include <rtthread.h>
#define web_malloc rt_malloc
#define web_free rt_free
#define web_realloc rt_realloc
#define web_calloc rt_calloc
#endif
Loading…
Cancel
Save